2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-09-10 13:05:40 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module json2
|
|
|
|
|
|
|
|
import strings
|
2020-10-09 17:11:55 +03:00
|
|
|
|
|
|
|
fn write_value(v Any, i int, len int, mut wr strings.Builder) {
|
2021-03-01 12:22:36 +03:00
|
|
|
str := v.json_str()
|
2020-11-25 14:09:40 +03:00
|
|
|
if v is string {
|
2021-02-22 14:18:11 +03:00
|
|
|
wr.write_string('"$str"')
|
2020-11-25 14:09:40 +03:00
|
|
|
} else {
|
2021-02-22 14:18:11 +03:00
|
|
|
wr.write_string(str)
|
2020-11-15 15:58:17 +03:00
|
|
|
}
|
2020-11-29 16:54:45 +03:00
|
|
|
if i >= len - 1 {
|
|
|
|
return
|
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
wr.write_b(`,`)
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// str returns the string representation of the `map[string]Any`.
|
2021-11-28 19:31:41 +03:00
|
|
|
[manualfree]
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (flds map[string]Any) str() string {
|
|
|
|
mut wr := strings.new_builder(200)
|
2020-10-09 17:11:55 +03:00
|
|
|
wr.write_b(`{`)
|
2020-09-10 13:05:40 +03:00
|
|
|
mut i := 0
|
|
|
|
for k, v in flds {
|
2021-02-22 14:18:11 +03:00
|
|
|
wr.write_string('"$k":')
|
2020-10-09 17:11:55 +03:00
|
|
|
write_value(v, i, flds.len, mut wr)
|
2020-09-10 13:05:40 +03:00
|
|
|
i++
|
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
wr.write_b(`}`)
|
|
|
|
defer {
|
2021-02-17 03:19:26 +03:00
|
|
|
unsafe { wr.free() }
|
2020-10-09 17:11:55 +03:00
|
|
|
}
|
|
|
|
res := wr.str()
|
|
|
|
return res
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// str returns the string representation of the `[]Any`.
|
2021-11-28 19:31:41 +03:00
|
|
|
[manualfree]
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (flds []Any) str() string {
|
|
|
|
mut wr := strings.new_builder(200)
|
2020-10-09 17:11:55 +03:00
|
|
|
wr.write_b(`[`)
|
2020-09-10 13:05:40 +03:00
|
|
|
for i, v in flds {
|
2020-10-09 17:11:55 +03:00
|
|
|
write_value(v, i, flds.len, mut wr)
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
wr.write_b(`]`)
|
|
|
|
defer {
|
2021-02-17 03:19:26 +03:00
|
|
|
unsafe { wr.free() }
|
2020-10-09 17:11:55 +03:00
|
|
|
}
|
|
|
|
res := wr.str()
|
|
|
|
return res
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
|
2021-03-01 12:22:36 +03:00
|
|
|
// str returns the string representation of the `Any` type. Use the `json_str` method
|
|
|
|
// if you want to use the escaped str() version of the `Any` type.
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (f Any) str() string {
|
2021-03-01 12:22:36 +03:00
|
|
|
if f is string {
|
|
|
|
return f
|
|
|
|
} else {
|
|
|
|
return f.json_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// json_str returns the JSON string representation of the `Any` type.
|
|
|
|
pub fn (f Any) json_str() string {
|
2020-11-25 14:09:40 +03:00
|
|
|
match f {
|
2020-11-29 16:54:45 +03:00
|
|
|
string {
|
2021-03-01 12:22:36 +03:00
|
|
|
return json_string(f)
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
2021-12-15 13:20:05 +03:00
|
|
|
bool, int, u64, i64 {
|
2020-11-29 16:54:45 +03:00
|
|
|
return f.str()
|
|
|
|
}
|
|
|
|
f32 {
|
|
|
|
str_f32 := f.str()
|
2021-03-22 17:45:29 +03:00
|
|
|
if str_f32.ends_with('.') {
|
|
|
|
return '${str_f32}0'
|
|
|
|
}
|
|
|
|
return str_f32
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
f64 {
|
|
|
|
str_f64 := f.str()
|
2021-03-22 17:45:29 +03:00
|
|
|
if str_f64.ends_with('.') {
|
|
|
|
return '${str_f64}0'
|
|
|
|
}
|
|
|
|
return str_f64
|
2020-11-29 16:54:45 +03:00
|
|
|
}
|
|
|
|
map[string]Any {
|
|
|
|
return f.str()
|
|
|
|
}
|
2021-02-26 09:36:02 +03:00
|
|
|
[]Any {
|
|
|
|
return f.str()
|
|
|
|
}
|
2020-11-29 16:54:45 +03:00
|
|
|
Null {
|
|
|
|
return 'null'
|
|
|
|
}
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-01 12:22:36 +03:00
|
|
|
|
|
|
|
// char_len_list is a modified version of builtin.utf8_str_len
|
|
|
|
// that returns an array of character lengths. (e.g "t✔" => [1,2])
|
|
|
|
fn char_len_list(s string) []int {
|
|
|
|
mut l := 1
|
|
|
|
mut ls := []int{}
|
|
|
|
for i := 0; i < s.len; i++ {
|
|
|
|
c := s[i]
|
|
|
|
if (c & (1 << 7)) != 0 {
|
|
|
|
for t := byte(1 << 6); (c & t) != 0; t >>= 1 {
|
|
|
|
l++
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ls << l
|
|
|
|
l = 1
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
|
|
|
const escaped_chars = [r'\b', r'\f', r'\n', r'\r', r'\t']
|
|
|
|
|
|
|
|
// json_string returns the JSON spec-compliant version of the string.
|
|
|
|
[manualfree]
|
|
|
|
fn json_string(s string) string {
|
|
|
|
// not the best implementation but will revisit it soon
|
|
|
|
char_lens := char_len_list(s)
|
|
|
|
mut sb := strings.new_builder(s.len)
|
|
|
|
mut i := 0
|
|
|
|
defer {
|
|
|
|
unsafe {
|
|
|
|
char_lens.free()
|
|
|
|
// freeing string builder on defer after
|
|
|
|
// returning .str() still isn't working :(
|
|
|
|
// sb.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for char_len in char_lens {
|
|
|
|
if char_len == 1 {
|
|
|
|
chr := s[i]
|
2021-03-22 17:45:29 +03:00
|
|
|
if chr in important_escapable_chars {
|
|
|
|
for j := 0; j < important_escapable_chars.len; j++ {
|
|
|
|
if chr == important_escapable_chars[j] {
|
|
|
|
sb.write_string(json2.escaped_chars[j])
|
2021-03-01 12:22:36 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if chr == `"` || chr == `/` || chr == `\\` {
|
|
|
|
sb.write_string('\\' + chr.ascii_str())
|
2021-11-18 08:34:00 +03:00
|
|
|
} else if int(chr) < 0x20 {
|
|
|
|
hex_code := chr.hex()
|
|
|
|
sb.write_string('\\u00$hex_code')
|
2021-03-01 12:22:36 +03:00
|
|
|
} else {
|
|
|
|
sb.write_b(chr)
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-22 17:45:29 +03:00
|
|
|
slice := s[i..i + char_len]
|
2021-03-01 12:22:36 +03:00
|
|
|
hex_code := slice.utf32_code().hex()
|
2021-06-29 11:40:44 +03:00
|
|
|
if hex_code.len < 4 {
|
|
|
|
// an utf8 codepoint
|
|
|
|
sb.write_string(slice)
|
|
|
|
} else if hex_code.len == 4 {
|
2021-03-01 12:22:36 +03:00
|
|
|
sb.write_string('\\u$hex_code')
|
|
|
|
} else {
|
|
|
|
// TODO: still figuring out what
|
|
|
|
// to do with more than 4 chars
|
|
|
|
sb.write_b(` `)
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
slice.free()
|
|
|
|
hex_code.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += char_len
|
|
|
|
}
|
|
|
|
str := sb.str()
|
|
|
|
unsafe { sb.free() }
|
|
|
|
return str
|
|
|
|
}
|