2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-09-10 12:05:40 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module json2
|
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
import io
|
2020-09-10 12:05:40 +02:00
|
|
|
import strings
|
2020-10-09 16:11:55 +02:00
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
// Encoder encodes the an `Any` type into JSON representation.
|
|
|
|
// It provides parameters in order to change the end result.
|
|
|
|
pub struct Encoder {
|
2022-04-15 15:24:02 +02:00
|
|
|
newline u8
|
2022-03-04 12:39:23 +01:00
|
|
|
newline_spaces_count int
|
|
|
|
escape_unicode bool = true
|
2020-10-09 16:11:55 +02:00
|
|
|
}
|
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
// byte array versions of the most common tokens/chars
|
|
|
|
// to avoid reallocations
|
|
|
|
const null_in_bytes = 'null'.bytes()
|
2020-10-09 16:11:55 +02:00
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
const true_in_bytes = 'true'.bytes()
|
|
|
|
|
|
|
|
const false_in_bytes = 'false'.bytes()
|
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
const zero_in_bytes = [u8(`0`)]
|
2022-03-04 12:39:23 +01:00
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
const comma_bytes = [u8(`,`)]
|
2022-03-04 12:39:23 +01:00
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
const colon_bytes = [u8(`:`)]
|
2022-03-04 12:39:23 +01:00
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
const space_bytes = [u8(` `)]
|
2022-03-04 12:39:23 +01:00
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
const unicode_escape_chars = [u8(`\\`), `u`]
|
2022-03-04 12:39:23 +01:00
|
|
|
|
2022-04-15 13:58:56 +02:00
|
|
|
const quote_bytes = [u8(`"`)]
|
2022-03-04 12:39:23 +01:00
|
|
|
|
|
|
|
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) ? {
|
2022-05-13 05:56:21 +02:00
|
|
|
e.encode_value_with_level(f, 1, mut wr)?
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
2020-10-09 16:11:55 +02:00
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
fn (e &Encoder) encode_newline(level int, mut wr io.Writer) ? {
|
|
|
|
if e.newline != 0 {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write([e.newline])?
|
2022-03-04 12:39:23 +01:00
|
|
|
for j := 0; j < level * e.newline_spaces_count; j++ {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.space_bytes)?
|
2022-03-04 12:39:23 +01:00
|
|
|
}
|
2021-03-01 10:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
fn (e &Encoder) encode_value_with_level(f Any, level int, mut wr io.Writer) ? {
|
2020-11-25 12:09:40 +01:00
|
|
|
match f {
|
2020-11-29 14:54:45 +01:00
|
|
|
string {
|
2022-05-13 05:56:21 +02:00
|
|
|
e.encode_string(f, mut wr)?
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
2022-03-04 12:39:23 +01:00
|
|
|
bool {
|
|
|
|
if f == true {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.true_in_bytes)?
|
2022-03-04 12:39:23 +01:00
|
|
|
} else {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.false_in_bytes)?
|
2021-03-22 15:45:29 +01:00
|
|
|
}
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
2022-03-04 12:39:23 +01:00
|
|
|
int, u64, i64 {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(f.str().bytes())?
|
2022-03-04 12:39:23 +01:00
|
|
|
}
|
|
|
|
f32, f64 {
|
2022-01-10 11:42:41 +01:00
|
|
|
$if !nofloat ? {
|
2022-03-04 12:39:23 +01:00
|
|
|
str_float := f.str().bytes()
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(str_float)?
|
2022-03-04 12:39:23 +01:00
|
|
|
if str_float[str_float.len - 1] == `.` {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.zero_in_bytes)?
|
2022-01-10 11:42:41 +01:00
|
|
|
}
|
2022-03-04 12:39:23 +01:00
|
|
|
return
|
2021-03-22 15:45:29 +01:00
|
|
|
}
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.zero_in_bytes)?
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
|
|
|
map[string]Any {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write([u8(`{`)])?
|
2022-03-04 12:39:23 +01:00
|
|
|
mut i := 0
|
|
|
|
for k, v in f {
|
2022-05-13 05:56:21 +02:00
|
|
|
e.encode_newline(level, mut wr)?
|
|
|
|
e.encode_string(k, mut wr)?
|
|
|
|
wr.write(json2.colon_bytes)?
|
2022-03-04 12:39:23 +01:00
|
|
|
if e.newline != 0 {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.space_bytes)?
|
2022-03-04 12:39:23 +01:00
|
|
|
}
|
2022-05-13 05:56:21 +02:00
|
|
|
e.encode_value_with_level(v, level + 1, mut wr)?
|
2022-03-04 12:39:23 +01:00
|
|
|
if i < f.len - 1 {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.comma_bytes)?
|
2022-03-04 12:39:23 +01:00
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
2022-05-13 05:56:21 +02:00
|
|
|
e.encode_newline(level - 1, mut wr)?
|
|
|
|
wr.write([u8(`}`)])?
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
2021-02-26 07:36:02 +01:00
|
|
|
[]Any {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write([u8(`[`)])?
|
2022-03-04 12:39:23 +01:00
|
|
|
for i, v in f {
|
2022-05-13 05:56:21 +02:00
|
|
|
e.encode_newline(level, mut wr)?
|
|
|
|
e.encode_value_with_level(v, level + 1, mut wr)?
|
2022-03-04 12:39:23 +01:00
|
|
|
if i < f.len - 1 {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.comma_bytes)?
|
2022-03-04 12:39:23 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-13 05:56:21 +02:00
|
|
|
e.encode_newline(level - 1, mut wr)?
|
|
|
|
wr.write([u8(`]`)])?
|
2021-02-26 07:36:02 +01:00
|
|
|
}
|
2020-11-29 14:54:45 +01:00
|
|
|
Null {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.null_in_bytes)?
|
2020-11-29 14:54:45 +01:00
|
|
|
}
|
2020-09-10 12:05:40 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-01 10:22:36 +01:00
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
// str returns the JSON string representation of the `map[string]Any` type.
|
|
|
|
pub fn (f map[string]Any) str() string {
|
|
|
|
return Any(f).json_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
// str returns the JSON string representation of the `[]Any` type.
|
|
|
|
pub fn (f []Any) str() string {
|
|
|
|
return Any(f).json_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
pub fn (f Any) str() string {
|
|
|
|
if f is string {
|
|
|
|
return f
|
|
|
|
} else {
|
|
|
|
return f.json_str()
|
2021-03-01 10:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
// json_str returns the JSON string representation of the `Any` type.
|
|
|
|
[manualfree]
|
|
|
|
pub fn (f Any) json_str() string {
|
|
|
|
mut sb := strings.new_builder(4096)
|
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
|
|
|
mut enc := Encoder{}
|
|
|
|
enc.encode_value(f, mut sb) or { return '' }
|
|
|
|
return sb.str()
|
|
|
|
}
|
2021-03-01 10:22:36 +01:00
|
|
|
|
2022-03-04 12:39:23 +01:00
|
|
|
// prettify_json_str returns the pretty-formatted JSON string representation of the `Any` type.
|
2021-03-01 10:22:36 +01:00
|
|
|
[manualfree]
|
2022-03-04 12:39:23 +01:00
|
|
|
pub fn (f Any) prettify_json_str() string {
|
|
|
|
mut sb := strings.new_builder(4096)
|
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2021-03-01 10:22:36 +01:00
|
|
|
defer {
|
2022-03-04 12:39:23 +01:00
|
|
|
iter.idx++
|
|
|
|
}
|
|
|
|
mut len := 1
|
|
|
|
c := iter.text[iter.idx]
|
|
|
|
if (c & (1 << 7)) != 0 {
|
2022-04-15 13:58:56 +02:00
|
|
|
for t := u8(1 << 6); (c & t) != 0; t >>= 1 {
|
2022-03-04 12:39:23 +01:00
|
|
|
len++
|
|
|
|
iter.idx++
|
2021-03-01 10:22:36 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-04 12:39:23 +01:00
|
|
|
return len
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode_string returns the JSON spec-compliant version of the string.
|
|
|
|
[manualfree]
|
|
|
|
fn (e &Encoder) encode_string(s string, mut wr io.Writer) ? {
|
|
|
|
mut char_lens := CharLengthIterator{
|
|
|
|
text: s
|
|
|
|
}
|
|
|
|
mut i := 0
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.quote_bytes)?
|
2021-03-01 10:22:36 +01:00
|
|
|
for char_len in char_lens {
|
|
|
|
if char_len == 1 {
|
|
|
|
chr := s[i]
|
2021-03-22 15:45:29 +01:00
|
|
|
if chr in important_escapable_chars {
|
|
|
|
for j := 0; j < important_escapable_chars.len; j++ {
|
|
|
|
if chr == important_escapable_chars[j] {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.escaped_chars[j])?
|
2021-03-01 10:22:36 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if chr == `"` || chr == `/` || chr == `\\` {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write([u8(`\\`), chr])?
|
2021-11-18 06:34:00 +01:00
|
|
|
} else if int(chr) < 0x20 {
|
2022-03-04 12:39:23 +01:00
|
|
|
hex_code := chr.hex().bytes()
|
2022-05-13 05:56:21 +02:00
|
|
|
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
|
2021-03-01 10:22:36 +01:00
|
|
|
} else {
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write([u8(chr)])?
|
2021-03-01 10:22:36 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-22 15:45:29 +01:00
|
|
|
slice := s[i..i + char_len]
|
2022-03-04 12:39:23 +01:00
|
|
|
hex_code := slice.utf32_code().hex().bytes()
|
|
|
|
if !e.escape_unicode || hex_code.len < 4 {
|
|
|
|
// unescaped non-ASCII char
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(slice.bytes())?
|
2021-06-29 10:40:44 +02:00
|
|
|
} else if hex_code.len == 4 {
|
2022-03-04 12:39:23 +01:00
|
|
|
// a unicode endpoint
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.unicode_escape_chars)?
|
|
|
|
wr.write(hex_code)?
|
2021-03-01 10:22:36 +01:00
|
|
|
} else {
|
|
|
|
// TODO: still figuring out what
|
|
|
|
// to do with more than 4 chars
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.space_bytes)?
|
2021-03-01 10:22:36 +01:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
slice.free()
|
|
|
|
hex_code.free()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += char_len
|
|
|
|
}
|
2022-03-04 12:39:23 +01:00
|
|
|
|
2022-05-13 05:56:21 +02:00
|
|
|
wr.write(json2.quote_bytes)?
|
2021-03-01 10:22:36 +01:00
|
|
|
}
|