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
|
|
|
|
|
2022-11-18 12:09:24 +03:00
|
|
|
import strings
|
2022-12-05 17:58:44 +03:00
|
|
|
import time
|
2022-11-18 12:09:24 +03:00
|
|
|
|
2020-09-10 13:05:40 +03:00
|
|
|
// Decodes a JSON string into an `Any` type. Returns an option.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn raw_decode(src string) !Any {
|
2020-10-09 17:11:55 +03:00
|
|
|
mut p := new_parser(src, true)
|
|
|
|
return p.decode()
|
|
|
|
}
|
2020-09-10 13:05:40 +03:00
|
|
|
|
2020-10-09 17:11:55 +03:00
|
|
|
// Same with `raw_decode`, but skips the type conversion for certain types when decoding a certain value.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn fast_raw_decode(src string) !Any {
|
2020-10-09 17:11:55 +03:00
|
|
|
mut p := new_parser(src, false)
|
|
|
|
return p.decode()
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
2020-11-29 16:54:45 +03:00
|
|
|
|
|
|
|
// decode is a generic function that decodes a JSON string into the target type.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn decode[T](src string) !T {
|
2022-12-05 17:58:44 +03:00
|
|
|
res := raw_decode(src)!.as_map()
|
2020-09-10 13:05:40 +03:00
|
|
|
mut typ := T{}
|
2022-12-05 17:58:44 +03:00
|
|
|
$for field in T.fields {
|
2023-01-15 23:09:02 +03:00
|
|
|
mut json_name := field.name
|
|
|
|
for attr in field.attrs {
|
|
|
|
if attr.contains('json: ') {
|
|
|
|
json_name = attr.replace('json: ', '')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 12:45:01 +03:00
|
|
|
$if field.is_enum {
|
|
|
|
typ.$(field.name) = if key := res[field.name] {
|
|
|
|
key.int()
|
|
|
|
} else {
|
|
|
|
res[json_name]!.int()
|
|
|
|
}
|
|
|
|
} $else $if field.typ is u8 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.u64()
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is u16 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.u64()
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is u32 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.u64()
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is u64 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.u64()
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is int {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.int()
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is i8 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.int()
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is i16 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.int()
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is i32 {
|
2023-02-24 11:54:45 +03:00
|
|
|
typ.$(field.name) = i32(res[field.name]!.int())
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is i64 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.i64()
|
2023-02-24 11:54:45 +03:00
|
|
|
} $else $if field.typ is ?u8 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?u8(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?i8 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?i8(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?u16 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?u16(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?i16 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?i16(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?u32 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?u32(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?i32 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?i32(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?u64 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?u64(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?i64 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?i64(res[json_name]!.i64())
|
|
|
|
}
|
|
|
|
} $else $if field.typ is ?int {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = ?int(res[json_name]!.i64())
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is f32 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.f32()
|
2023-02-24 11:54:45 +03:00
|
|
|
} $else $if field.typ is ?f32 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = res[json_name]!.f32()
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is f64 {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.f64()
|
2023-02-24 11:54:45 +03:00
|
|
|
} $else $if field.typ is ?f64 {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = res[json_name]!.f64()
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is bool {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.bool()
|
2023-02-24 11:54:45 +03:00
|
|
|
} $else $if field.typ is ?bool {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = res[json_name]!.bool()
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is string {
|
2023-01-15 23:09:02 +03:00
|
|
|
typ.$(field.name) = res[json_name]!.str()
|
2023-02-24 11:54:45 +03:00
|
|
|
} $else $if field.typ is ?string {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = res[json_name]!.str()
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else $if field.typ is time.Time {
|
2022-12-12 11:06:29 +03:00
|
|
|
typ.$(field.name) = res[field.name]!.to_time()!
|
2023-02-24 11:54:45 +03:00
|
|
|
} $else $if field.typ is ?time.Time {
|
|
|
|
if json_name in res {
|
|
|
|
typ.$(field.name) = res[field.name]!.to_time()!
|
|
|
|
}
|
2023-01-11 11:18:45 +03:00
|
|
|
} $else $if field.is_array {
|
|
|
|
// typ.$(field.name) = res[field.name]!.arr()
|
|
|
|
} $else $if field.is_struct {
|
|
|
|
} $else $if field.is_alias {
|
|
|
|
} $else $if field.is_map {
|
2022-12-05 17:58:44 +03:00
|
|
|
} $else {
|
|
|
|
return error("The type of `${field.name}` can't be decoded. Please open an issue at https://github.com/vlang/v/issues/new/choose")
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 13:05:40 +03:00
|
|
|
return typ
|
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// encode is a generic function that encodes a type into a JSON string.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn encode[T](val T) string {
|
2023-02-01 17:52:58 +03:00
|
|
|
$if T is $Array {
|
|
|
|
$compile_error('Cannot use `json.encode` to encode array. Try `json.encode_array` instead')
|
|
|
|
}
|
2022-11-18 12:09:24 +03:00
|
|
|
mut sb := strings.new_builder(64)
|
2023-02-01 17:52:58 +03:00
|
|
|
|
2022-11-18 12:09:24 +03:00
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
|
|
|
}
|
2023-02-01 17:52:58 +03:00
|
|
|
|
|
|
|
default_encoder.encode_value(val, mut sb) or {
|
|
|
|
dump(err)
|
|
|
|
default_encoder.encode_value[Null](null, mut sb) or {}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode_array is a generic function that encodes a array into a JSON string.
|
|
|
|
pub fn encode_array[T](val []T) string {
|
|
|
|
mut sb := strings.new_builder(64)
|
|
|
|
|
|
|
|
defer {
|
|
|
|
unsafe { sb.free() }
|
2022-11-18 12:09:24 +03:00
|
|
|
}
|
2023-02-01 17:52:58 +03:00
|
|
|
|
|
|
|
default_encoder.encode_array(val, 1, mut sb) or {
|
|
|
|
dump(err)
|
|
|
|
default_encoder.encode_value[Null](null, mut sb) or {}
|
|
|
|
}
|
|
|
|
|
2022-11-18 12:09:24 +03:00
|
|
|
return sb.str()
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
2020-11-29 16:54:45 +03:00
|
|
|
|
2022-12-10 21:44:23 +03:00
|
|
|
// encode_pretty ...
|
|
|
|
pub fn encode_pretty[T](typed_data T) string {
|
|
|
|
encoded := encode(typed_data)
|
|
|
|
raw_decoded := raw_decode(encoded) or { 0 }
|
|
|
|
return raw_decoded.prettify_json_str()
|
|
|
|
}
|
|
|
|
|
2023-01-18 19:55:04 +03:00
|
|
|
// i8 - TODO
|
|
|
|
pub fn (f Any) i8() i8 {
|
|
|
|
match f {
|
|
|
|
i8 {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
i16, int, i64, u8, u16, u32, u64, f32, f64, bool {
|
|
|
|
return i8(f)
|
|
|
|
}
|
|
|
|
string {
|
|
|
|
return f.i8()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// i16 - TODO
|
|
|
|
pub fn (f Any) i16() i16 {
|
|
|
|
match f {
|
|
|
|
i16 {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
i8, int, i64, u8, u16, u32, u64, f32, f64, bool {
|
|
|
|
return i16(f)
|
|
|
|
}
|
|
|
|
string {
|
|
|
|
return f.i16()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// int uses `Any` as an integer.
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (f Any) int() int {
|
2020-11-25 14:09:40 +03:00
|
|
|
match f {
|
2022-12-05 17:58:44 +03:00
|
|
|
int {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
i8, i16, i64, u8, u16, u32, u64, f32, f64, bool {
|
|
|
|
return int(f)
|
|
|
|
}
|
|
|
|
string {
|
|
|
|
return f.int()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0
|
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// i64 uses `Any` as a 64-bit integer.
|
2020-10-09 17:11:55 +03:00
|
|
|
pub fn (f Any) i64() i64 {
|
2020-11-25 14:09:40 +03:00
|
|
|
match f {
|
2022-12-05 17:58:44 +03:00
|
|
|
i64 {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
i8, i16, int, u8, u16, u32, u64, f32, f64, bool {
|
|
|
|
return i64(f)
|
|
|
|
}
|
|
|
|
string {
|
|
|
|
return f.i64()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0
|
|
|
|
}
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
|
2021-03-25 18:53:39 +03:00
|
|
|
// u64 uses `Any` as a 64-bit unsigned integer.
|
|
|
|
pub fn (f Any) u64() u64 {
|
|
|
|
match f {
|
2022-12-05 17:58:44 +03:00
|
|
|
u64 {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
u8, u16, u32, i8, i16, int, i64, f32, f64, bool {
|
|
|
|
return u64(f)
|
|
|
|
}
|
|
|
|
string {
|
|
|
|
return f.u64()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0
|
|
|
|
}
|
2021-03-25 18:53:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// f32 uses `Any` as a 32-bit float.
|
2020-10-09 17:11:55 +03:00
|
|
|
pub fn (f Any) f32() f32 {
|
2020-11-25 14:09:40 +03:00
|
|
|
match f {
|
2022-12-05 17:58:44 +03:00
|
|
|
f32 {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
bool, i8, i16, int, i64, u8, u16, u32, u64, f64 {
|
|
|
|
return f32(f)
|
|
|
|
}
|
|
|
|
string {
|
|
|
|
return f.f32()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0.0
|
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-19 12:52:17 +03:00
|
|
|
// f64 uses `Any` as a 64-bit float.
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (f Any) f64() f64 {
|
2020-11-25 14:09:40 +03:00
|
|
|
match f {
|
2022-12-05 17:58:44 +03:00
|
|
|
f64 {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
i8, i16, int, i64, u8, u16, u32, u64, f32 {
|
|
|
|
return f64(f)
|
|
|
|
}
|
|
|
|
string {
|
|
|
|
return f.f64()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 0.0
|
|
|
|
}
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
|
|
|
}
|
2020-11-29 16:54:45 +03:00
|
|
|
|
2022-11-19 12:52:17 +03:00
|
|
|
// bool uses `Any` as a bool.
|
|
|
|
pub fn (f Any) bool() bool {
|
|
|
|
match f {
|
2022-12-05 17:58:44 +03:00
|
|
|
bool {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
string {
|
2023-01-18 19:55:04 +03:00
|
|
|
if f == 'false' {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if f == 'true' {
|
|
|
|
return true
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
if f.len > 0 {
|
2023-01-18 19:55:04 +03:00
|
|
|
return f != '0' && f != '0.0'
|
2022-12-05 17:58:44 +03:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i8, i16, int, i64 {
|
|
|
|
return i64(f) != 0
|
|
|
|
}
|
|
|
|
u8, u16, u32, u64 {
|
|
|
|
return u64(f) != 0
|
|
|
|
}
|
|
|
|
f32, f64 {
|
|
|
|
return f64(f) != 0.0
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return false
|
|
|
|
}
|
2022-11-19 12:52:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// arr uses `Any` as an array.
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (f Any) arr() []Any {
|
|
|
|
if f is []Any {
|
2020-11-24 15:58:29 +03:00
|
|
|
return f
|
2020-10-09 17:11:55 +03:00
|
|
|
} else if f is map[string]Any {
|
2020-09-10 13:05:40 +03:00
|
|
|
mut arr := []Any{}
|
2020-11-24 15:58:29 +03:00
|
|
|
for _, v in f {
|
2020-09-10 13:05:40 +03:00
|
|
|
arr << v
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
return [f]
|
|
|
|
}
|
2020-09-29 04:15:00 +03:00
|
|
|
|
2022-11-19 12:52:17 +03:00
|
|
|
// as_map uses `Any` as a map.
|
|
|
|
pub fn (f Any) as_map() map[string]Any {
|
|
|
|
if f is map[string]Any {
|
|
|
|
return f
|
|
|
|
} else if f is []Any {
|
|
|
|
mut mp := map[string]Any{}
|
|
|
|
for i, fi in f {
|
|
|
|
mp['${i}'] = fi
|
|
|
|
}
|
|
|
|
return mp
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
'0': f
|
2020-09-29 04:15:00 +03:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 11:06:29 +03:00
|
|
|
|
|
|
|
// to_time uses `Any` as a time.Time.
|
|
|
|
pub fn (f Any) to_time() !time.Time {
|
|
|
|
match f {
|
|
|
|
time.Time {
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
i64 {
|
|
|
|
return time.unix(f)
|
|
|
|
}
|
|
|
|
string {
|
2022-12-20 17:34:04 +03:00
|
|
|
if f.len == 10 && f[4] == `-` && f[7] == `-` {
|
|
|
|
// just a date in the format `2001-01-01`
|
|
|
|
return time.parse_iso8601(f)!
|
|
|
|
}
|
2022-12-12 11:06:29 +03:00
|
|
|
is_rfc3339 := f.len == 24 && f[23] == `Z` && f[10] == `T`
|
|
|
|
if is_rfc3339 {
|
|
|
|
return time.parse_rfc3339(f)!
|
|
|
|
}
|
|
|
|
mut is_unix_timestamp := true
|
|
|
|
for c in f {
|
|
|
|
if c == `-` || (c >= `0` && c <= `9`) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
is_unix_timestamp = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if is_unix_timestamp {
|
|
|
|
return time.unix(f.i64())
|
|
|
|
}
|
|
|
|
// TODO - parse_iso8601
|
|
|
|
// TODO - parse_rfc2822
|
|
|
|
return time.parse(f)!
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return error('not a time value: ${f} of type: ${f.type_name()}')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-18 19:55:04 +03:00
|
|
|
|
|
|
|
fn map_from[T](t T) map[string]Any {
|
|
|
|
mut m := map[string]Any{}
|
|
|
|
$if T is $Struct {
|
|
|
|
$for field in T.fields {
|
|
|
|
value := t.$(field.name)
|
|
|
|
|
|
|
|
$if field.is_array {
|
|
|
|
mut arr := []Any{}
|
|
|
|
for variable in value {
|
|
|
|
arr << Any(variable)
|
|
|
|
}
|
|
|
|
m[field.name] = arr
|
|
|
|
arr.clear()
|
|
|
|
} $else $if field.is_struct {
|
|
|
|
m[field.name] = map_from(value)
|
|
|
|
} $else $if field.is_map {
|
|
|
|
// TODO
|
|
|
|
} $else $if field.is_alias {
|
|
|
|
// TODO
|
|
|
|
} $else $if field.is_option {
|
|
|
|
// TODO
|
|
|
|
} $else {
|
|
|
|
// TODO improve memory usage when convert
|
|
|
|
$if field.typ is string {
|
|
|
|
m[field.name] = value.str()
|
|
|
|
} $else $if field.typ is bool {
|
|
|
|
m[field.name] = t.$(field.name).str().bool()
|
|
|
|
} $else $if field.typ is i8 {
|
|
|
|
m[field.name] = t.$(field.name).str().i8()
|
|
|
|
} $else $if field.typ is i16 {
|
|
|
|
m[field.name] = t.$(field.name).str().i16()
|
|
|
|
} $else $if field.typ is int {
|
|
|
|
m[field.name] = t.$(field.name).str().int()
|
|
|
|
} $else $if field.typ is i64 {
|
|
|
|
m[field.name] = t.$(field.name).str().i64()
|
|
|
|
} $else $if field.typ is f32 {
|
|
|
|
m[field.name] = t.$(field.name).str().f32()
|
|
|
|
} $else $if field.typ is f64 {
|
|
|
|
m[field.name] = t.$(field.name).str().f64()
|
|
|
|
} $else $if field.typ is u8 {
|
|
|
|
m[field.name] = t.$(field.name).str().u8()
|
|
|
|
} $else $if field.typ is u16 {
|
|
|
|
m[field.name] = t.$(field.name).str().u16()
|
|
|
|
} $else $if field.typ is u32 {
|
|
|
|
m[field.name] = t.$(field.name).str().u32()
|
|
|
|
} $else $if field.typ is u64 {
|
|
|
|
m[field.name] = t.$(field.name).str().u64()
|
|
|
|
} $else {
|
|
|
|
// return error("The type of `${field.name}` can't be decoded. Please open an issue at https://github.com/vlang/v/issues/new/choose")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|