2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 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) {
|
|
|
|
str := v.str()
|
2020-11-25 14:09:40 +03:00
|
|
|
if v is string {
|
|
|
|
wr.write('"$str"')
|
|
|
|
} else {
|
2020-11-15 15:58:17 +03:00
|
|
|
wr.write(str)
|
|
|
|
}
|
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`.
|
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 {
|
|
|
|
wr.write('"$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`.
|
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
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// str returns the string representation of the `Any` type.
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (f Any) str() string {
|
2020-11-25 14:09:40 +03:00
|
|
|
match f {
|
2020-11-29 16:54:45 +03:00
|
|
|
string {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
int {
|
|
|
|
return f.str()
|
|
|
|
}
|
|
|
|
i64 {
|
|
|
|
return f.str()
|
|
|
|
}
|
|
|
|
f32 {
|
|
|
|
str_f32 := f.str()
|
|
|
|
return if str_f32.ends_with('.') {
|
|
|
|
str_f32 + '0'
|
|
|
|
} else {
|
|
|
|
str_f32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f64 {
|
|
|
|
str_f64 := f.str()
|
|
|
|
return if str_f64.ends_with('.') {
|
|
|
|
str_f64 + '0'
|
|
|
|
} else {
|
|
|
|
str_f64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool {
|
|
|
|
return f.str()
|
|
|
|
}
|
|
|
|
map[string]Any {
|
|
|
|
return f.str()
|
|
|
|
}
|
|
|
|
Null {
|
|
|
|
return 'null'
|
|
|
|
}
|
2020-09-10 13:05:40 +03:00
|
|
|
else {
|
|
|
|
if f is []Any {
|
2020-11-24 15:58:29 +03:00
|
|
|
return f.str()
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|