x/json2: add Encoder struct, add prettify_json_str

pull/13654/head
Ned Palacios 2022-03-04 13:52:25 +08:00
parent 63b41e67fa
commit 5a565bdbb2
1 changed files with 185 additions and 127 deletions

View File

@ -3,54 +3,125 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module json2 module json2
import io
import strings import strings
fn write_value(v Any, i int, len int, mut wr strings.Builder) { // Encoder encodes the an `Any` type into JSON representation.
str := v.json_str() // It provides parameters in order to change the end result.
if v is string { pub struct Encoder {
wr.write_string('"$str"') newline byte
} else { newline_spaces_count int
wr.write_string(str) escape_utf32 bool = true
}
// byte array versions of the most common tokens/chars
// to avoid reallocations
const null_in_bytes = 'null'.bytes()
const true_in_bytes = 'true'.bytes()
const false_in_bytes = 'false'.bytes()
const zero_in_bytes = [byte(`0`)]
const comma_bytes = [byte(`,`)]
const colon_bytes = [byte(`:`)]
const space_bytes = [byte(` `)]
const unicode_escape_chars = [byte(`\\`), `u`]
const quote_bytes = [byte(`"`)]
const escaped_chars = [(r'\b').bytes(), (r'\f').bytes(), (r'\n').bytes(),
(r'\r').bytes(), (r'\t').bytes()]
// encode_value encodes an `Any` value to the specific writer.
pub fn (e &Encoder) encode_value(f Any, mut wr io.Writer) ? {
e.encode_value_with_level(f, 1, mut wr) ?
}
fn (e &Encoder) encode_newline(level int, mut wr io.Writer) ? {
if e.newline != 0 {
wr.write([e.newline]) ?
for j := 0; j < level * e.newline_spaces_count; j++ {
wr.write(json2.space_bytes) ?
}
}
}
fn (e &Encoder) encode_value_with_level(f Any, level int, mut wr io.Writer) ? {
match f {
string {
e.encode_string(f, mut wr) ?
}
bool {
if f == true {
wr.write(json2.true_in_bytes) ?
} else {
wr.write(json2.false_in_bytes) ?
}
}
int, u64, i64 {
wr.write(f.str().bytes()) ?
}
f32, f64 {
$if !nofloat ? {
str_float := f.str().bytes()
wr.write(str_float) ?
if str_float[str_float.len - 1] == `.` {
wr.write(json2.zero_in_bytes) ?
} }
if i >= len - 1 {
return return
} }
wr.write_byte(`,`) wr.write(json2.zero_in_bytes) ?
} }
map[string]Any {
// str returns the string representation of the `map[string]Any`. wr.write([byte(`{`)]) ?
[manualfree]
pub fn (flds map[string]Any) str() string {
mut wr := strings.new_builder(200)
wr.write_byte(`{`)
mut i := 0 mut i := 0
for k, v in flds { for k, v in f {
wr.write_string('"$k":') e.encode_newline(level, mut wr) ?
write_value(v, i, flds.len, mut wr) e.encode_string(k, mut wr) ?
wr.write(json2.colon_bytes) ?
if e.newline != 0 {
wr.write(json2.space_bytes) ?
}
e.encode_value_with_level(v, level + 1, mut wr) ?
if i < f.len - 1 {
wr.write(json2.comma_bytes) ?
}
i++ i++
} }
wr.write_byte(`}`) e.encode_newline(level - 1, mut wr) ?
defer { wr.write([byte(`}`)]) ?
unsafe { wr.free() } }
[]Any {
wr.write([byte(`[`)]) ?
for i, v in f {
e.encode_newline(level, mut wr) ?
e.encode_value_with_level(v, level + 1, mut wr) ?
if i < f.len - 1 {
wr.write(json2.comma_bytes) ?
}
}
e.encode_newline(level - 1, mut wr) ?
wr.write([byte(`]`)]) ?
}
Null {
wr.write(json2.null_in_bytes) ?
}
} }
res := wr.str()
return res
} }
// str returns the string representation of the `[]Any`. // str returns the JSON string representation of the `map[string]Any` type.
[manualfree] pub fn (f map[string]Any) str() string {
pub fn (flds []Any) str() string { return Any(f).json_str()
mut wr := strings.new_builder(200) }
wr.write_byte(`[`)
for i, v in flds { // str returns the JSON string representation of the `[]Any` type.
write_value(v, i, flds.len, mut wr) pub fn (f []Any) str() string {
} return Any(f).json_str()
wr.write_byte(`]`)
defer {
unsafe { wr.free() }
}
res := wr.str()
return res
} }
// str returns the string representation of the `Any` type. Use the `json_str` method // str returns the string representation of the `Any` type. Use the `json_str` method
@ -64,113 +135,101 @@ pub fn (f Any) str() string {
} }
// json_str returns the JSON string representation of the `Any` type. // json_str returns the JSON string representation of the `Any` type.
[manualfree]
pub fn (f Any) json_str() string { pub fn (f Any) json_str() string {
match f { mut sb := strings.new_builder(4096)
string { defer {
return json_string(f) unsafe { sb.free() }
}
bool, int, u64, i64 {
return f.str()
}
f32 {
$if !nofloat ? {
str_f32 := f.str()
if str_f32.ends_with('.') {
return '${str_f32}0'
}
return str_f32
}
return '0'
}
f64 {
$if !nofloat ? {
str_f64 := f.str()
if str_f64.ends_with('.') {
return '${str_f64}0'
}
return str_f64
}
return '0'
}
map[string]Any {
return f.str()
}
[]Any {
return f.str()
}
Null {
return 'null'
}
} }
mut enc := Encoder{}
enc.encode_value(f, mut sb) or { return '' }
return sb.str()
} }
// char_len_list is a modified version of builtin.utf8_str_len // prettify_json_str returns the pretty-formatted JSON string representation of the `Any` type.
// that returns an array of character lengths. (e.g "t✔" => [1,2]) [manualfree]
fn char_len_list(s string) []int { pub fn (f Any) prettify_json_str() string {
mut l := 1 mut sb := strings.new_builder(4096)
mut ls := []int{} defer {
for i := 0; i < s.len; i++ { unsafe { sb.free() }
c := s[i] }
mut enc := Encoder{
newline: `\n`
newline_spaces_count: 4
}
enc.encode_value(f, mut sb) or { return '' }
return sb.str()
}
// CharLengthIterator is an iterator that generates a char
// length value of every iteration based on the given text.
// (e.g.: "t✔" => [t => 1, => 2])
struct CharLengthIterator {
text string
mut:
idx int
}
fn (mut iter CharLengthIterator) next() ?int {
if iter.idx >= iter.text.len {
return none
}
defer {
iter.idx++
}
mut len := 1
c := iter.text[iter.idx]
if (c & (1 << 7)) != 0 { if (c & (1 << 7)) != 0 {
for t := byte(1 << 6); (c & t) != 0; t >>= 1 { for t := byte(1 << 6); (c & t) != 0; t >>= 1 {
l++ len++
i++ iter.idx++
} }
} }
ls << l return len
l = 1
}
return ls
} }
const escaped_chars = [r'\b', r'\f', r'\n', r'\r', r'\t'] // encode_string returns the JSON spec-compliant version of the string.
// json_string returns the JSON spec-compliant version of the string.
[manualfree] [manualfree]
fn json_string(s string) string { fn (e &Encoder) encode_string(s string, mut wr io.Writer) ? {
// not the best implementation but will revisit it soon mut char_lens := CharLengthIterator{
char_lens := char_len_list(s) text: s
mut sb := strings.new_builder(s.len) }
mut i := 0 mut i := 0
defer { wr.write(json2.quote_bytes) ?
unsafe {
char_lens.free()
// freeing string builder on defer after
// returning .str() still isn't working :(
// sb.free()
}
}
for char_len in char_lens { for char_len in char_lens {
if char_len == 1 { if char_len == 1 {
chr := s[i] chr := s[i]
if chr in important_escapable_chars { if chr in important_escapable_chars {
for j := 0; j < important_escapable_chars.len; j++ { for j := 0; j < important_escapable_chars.len; j++ {
if chr == important_escapable_chars[j] { if chr == important_escapable_chars[j] {
sb.write_string(json2.escaped_chars[j]) wr.write(json2.escaped_chars[j]) ?
break break
} }
} }
} else if chr == `"` || chr == `/` || chr == `\\` { } else if chr == `"` || chr == `/` || chr == `\\` {
sb.write_string('\\' + chr.ascii_str()) wr.write([byte(`\\`), chr]) ?
} else if int(chr) < 0x20 { } else if int(chr) < 0x20 {
hex_code := chr.hex() hex_code := chr.hex().bytes()
sb.write_string('\\u00$hex_code') wr.write(json2.unicode_escape_chars) ? // \u
wr.write(json2.zero_in_bytes) ? // \u0
wr.write(json2.zero_in_bytes) ? // \u00
wr.write(hex_code) ? // \u00xxxx
} else { } else {
sb.write_byte(chr) wr.write([byte(chr)]) ?
} }
} else { } else {
slice := s[i..i + char_len] slice := s[i..i + char_len]
hex_code := slice.utf32_code().hex() hex_code := slice.utf32_code().hex().bytes()
if hex_code.len < 4 { if !e.escape_utf32 || hex_code.len < 4 {
// an utf8 codepoint // an utf8 codepoint or an unescape utf32
sb.write_string(slice) wr.write(slice.bytes()) ?
} else if hex_code.len == 4 { } else if hex_code.len == 4 {
sb.write_string('\\u$hex_code') wr.write(json2.unicode_escape_chars) ?
wr.write(hex_code) ?
} else { } else {
// TODO: still figuring out what // TODO: still figuring out what
// to do with more than 4 chars // to do with more than 4 chars
sb.write_byte(` `) wr.write(json2.space_bytes) ?
} }
unsafe { unsafe {
slice.free() slice.free()
@ -179,7 +238,6 @@ fn json_string(s string) string {
} }
i += char_len i += char_len
} }
str := sb.str()
unsafe { sb.free() } wr.write(json2.quote_bytes) ?
return str
} }