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
|
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
pub const (
|
|
|
|
null = Null{}
|
|
|
|
)
|
|
|
|
|
2020-09-10 13:05:40 +03:00
|
|
|
pub interface Serializable {
|
|
|
|
from_json(f Any)
|
|
|
|
to_json() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decodes a JSON string into an `Any` type. Returns an option.
|
|
|
|
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.
|
|
|
|
pub fn fast_raw_decode(src string) ?Any {
|
|
|
|
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.
|
|
|
|
pub fn decode<T>(src string) ?T {
|
|
|
|
res := raw_decode(src) ?
|
2020-09-10 13:05:40 +03:00
|
|
|
mut typ := T{}
|
|
|
|
typ.from_json(res)
|
|
|
|
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.
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn encode<T>(typ T) string {
|
|
|
|
return typ.to_json()
|
|
|
|
}
|
2020-11-29 16:54:45 +03:00
|
|
|
|
|
|
|
// as_map uses `Any` as a map.
|
2020-09-10 13:05:40 +03:00
|
|
|
pub fn (f Any) as_map() map[string]Any {
|
2020-10-09 17:11:55 +03:00
|
|
|
if f is map[string]Any {
|
2020-11-24 15:58:29 +03:00
|
|
|
return f
|
2020-10-09 17:11:55 +03:00
|
|
|
} else if f is []Any {
|
2020-11-29 16:54:45 +03:00
|
|
|
mut mp := map[string]Any{}
|
2020-11-24 15:58:29 +03:00
|
|
|
for i, fi in f {
|
2020-10-09 17:11:55 +03:00
|
|
|
mp['$i'] = fi
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
2020-10-09 17:11:55 +03:00
|
|
|
return mp
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
2020-11-29 16:54:45 +03:00
|
|
|
return {
|
|
|
|
'0': f
|
|
|
|
}
|
2020-09-10 13:05:40 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-29 16:54:45 +03:00
|
|
|
int { return f }
|
|
|
|
i64, f32, f64, bool { return int(f) }
|
2020-10-09 17:11:55 +03:00
|
|
|
else { return 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-29 16:54:45 +03:00
|
|
|
i64 { return f }
|
|
|
|
int, f32, f64, bool { return i64(f) }
|
2020-09-10 13:05:40 +03:00
|
|
|
else { return 0 }
|
|
|
|
}
|
|
|
|
}
|
2020-10-09 17:11:55 +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 {
|
2020-11-24 15:58:29 +03:00
|
|
|
f32 { return f }
|
2020-11-29 16:54:45 +03:00
|
|
|
int, i64, f64 { return f32(f) }
|
2020-10-09 17:11:55 +03:00
|
|
|
else { return 0.0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// f64 uses `Any` as a 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 {
|
2020-11-24 15:58:29 +03:00
|
|
|
f64 { return f }
|
2020-11-29 16:54:45 +03:00
|
|
|
int, i64, f32 { return f64(f) }
|
2020-09-10 13:05:40 +03:00
|
|
|
else { return 0.0 }
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2020-11-29 16:54:45 +03:00
|
|
|
// bool uses `Any` as a bool
|
2020-09-29 04:15:00 +03:00
|
|
|
pub fn (f Any) bool() bool {
|
2020-11-25 14:09:40 +03:00
|
|
|
match f {
|
2020-11-24 15:58:29 +03:00
|
|
|
bool { return f }
|
|
|
|
string { return f.bool() }
|
2020-09-29 04:15:00 +03:00
|
|
|
else { return false }
|
|
|
|
}
|
|
|
|
}
|