2022-11-20 12:18:14 +03:00
|
|
|
import x.json2 as json
|
2020-09-10 13:05:40 +03:00
|
|
|
|
|
|
|
enum JobTitle {
|
|
|
|
manager
|
|
|
|
executive
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Employee {
|
2020-11-29 16:54:45 +03:00
|
|
|
pub mut:
|
2020-09-10 13:05:40 +03:00
|
|
|
name string
|
|
|
|
age int
|
|
|
|
salary f32
|
|
|
|
title JobTitle
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:11:55 +03:00
|
|
|
fn test_fast_raw_decode() {
|
|
|
|
s := '{"name":"Peter","age":28,"salary":95000.5,"title":2}'
|
2022-11-20 12:18:14 +03:00
|
|
|
o := json.fast_raw_decode(s) or {
|
2020-10-09 17:11:55 +03:00
|
|
|
assert false
|
2022-11-20 12:18:14 +03:00
|
|
|
json.Any(json.null)
|
2020-10-09 17:11:55 +03:00
|
|
|
}
|
|
|
|
str := o.str()
|
|
|
|
assert str == '{"name":"Peter","age":"28","salary":"95000.5","title":"2"}'
|
|
|
|
}
|
|
|
|
|
2020-11-15 15:58:17 +03:00
|
|
|
fn test_character_unescape() {
|
2021-02-26 09:36:02 +03:00
|
|
|
message := r'{
|
|
|
|
"newline": "new\nline",
|
|
|
|
"tab": "\ttab",
|
|
|
|
"backslash": "back\\slash",
|
|
|
|
"quotes": "\"quotes\"",
|
|
|
|
"slash":"\/dev\/null"
|
|
|
|
}'
|
2022-11-20 12:18:14 +03:00
|
|
|
mut obj := json.raw_decode(message) or {
|
2021-02-26 09:36:02 +03:00
|
|
|
println(err)
|
2020-11-15 15:58:17 +03:00
|
|
|
assert false
|
2021-02-26 09:36:02 +03:00
|
|
|
return
|
2020-11-15 15:58:17 +03:00
|
|
|
}
|
|
|
|
lines := obj.as_map()
|
2021-09-10 16:07:39 +03:00
|
|
|
assert lines['newline'] or { 0 }.str() == 'new\nline'
|
|
|
|
assert lines['tab'] or { 0 }.str() == '\ttab'
|
|
|
|
assert lines['backslash'] or { 0 }.str() == 'back\\slash'
|
|
|
|
assert lines['quotes'] or { 0 }.str() == '"quotes"'
|
|
|
|
assert lines['slash'] or { 0 }.str() == '/dev/null'
|
2020-11-15 15:58:17 +03:00
|
|
|
}
|
2023-01-18 19:55:04 +03:00
|
|
|
|
|
|
|
struct StructType[T] {
|
|
|
|
mut:
|
|
|
|
val T
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_struct_with_bool_to_map() {
|
|
|
|
array_of_struct := [StructType[bool]{
|
|
|
|
val: true
|
|
|
|
}, StructType[bool]{
|
|
|
|
val: false
|
|
|
|
}]
|
|
|
|
|
|
|
|
mut array_of_map := []json.Any{}
|
|
|
|
|
|
|
|
for variable in array_of_struct {
|
|
|
|
array_of_map << json.map_from(variable)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert array_of_map.str() == '[{"val":true},{"val":false}]'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_struct_with_string_to_map() {
|
|
|
|
array_of_struct := [StructType[string]{
|
|
|
|
val: 'true'
|
|
|
|
}, StructType[string]{
|
|
|
|
val: 'false'
|
|
|
|
}]
|
|
|
|
|
|
|
|
mut array_of_map := []json.Any{}
|
|
|
|
|
|
|
|
for variable in array_of_struct {
|
|
|
|
array_of_map << json.map_from(variable)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert array_of_map.str() == '[{"val":"true"},{"val":"false"}]'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_struct_with_array_to_map() {
|
|
|
|
array_of_struct := [StructType[[]bool]{
|
|
|
|
val: [false, true]
|
|
|
|
}, StructType[[]bool]{
|
|
|
|
val: [true, false]
|
|
|
|
}]
|
|
|
|
|
|
|
|
mut array_of_map := []json.Any{}
|
|
|
|
|
|
|
|
for variable in array_of_struct {
|
|
|
|
array_of_map << json.map_from(variable)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert array_of_map.str() == '[{"val":[false,true]},{"val":[true,false]}]'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_struct_with_number_to_map() {
|
|
|
|
assert json.map_from(StructType[string]{'3'}).str() == '{"val":"3"}'
|
|
|
|
assert json.map_from(StructType[bool]{true}).str() == '{"val":true}'
|
|
|
|
assert json.map_from(StructType[i8]{3}).str() == '{"val":3}'
|
|
|
|
assert json.map_from(StructType[i16]{3}).str() == '{"val":3}'
|
|
|
|
assert json.map_from(StructType[int]{3}).str() == '{"val":3}'
|
|
|
|
assert json.map_from(StructType[i64]{3}).str() == '{"val":3}'
|
|
|
|
assert json.map_from(StructType[i8]{-3}).str() == '{"val":-3}'
|
|
|
|
assert json.map_from(StructType[i16]{i16(-3)}).str() == '{"val":-3}'
|
|
|
|
assert json.map_from(StructType[int]{-3}).str() == '{"val":-3}'
|
|
|
|
assert json.map_from(StructType[i64]{-3}).str() == '{"val":-3}'
|
|
|
|
assert json.map_from(StructType[f32]{3.0}).str() == '{"val":3.0}'
|
|
|
|
assert json.map_from(StructType[f64]{3.0}).str() == '{"val":3.0}'
|
|
|
|
assert json.map_from(StructType[u8]{3}).str() == '{"val":3}'
|
|
|
|
assert json.map_from(StructType[u16]{3}).str() == '{"val":3}'
|
|
|
|
assert json.map_from(StructType[u32]{3}).str() == '{"val":3}'
|
|
|
|
assert json.map_from(StructType[u64]{3}).str() == '{"val":3}'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_struct_with_struct_to_map() {
|
|
|
|
assert json.map_from(StructType[StructType[string]]{StructType[string]{'3'}}).str() == '{"val":{"val":"3"}}'
|
|
|
|
assert json.map_from(StructType[StructType[int]]{StructType[int]{3}}).str() == '{"val":{"val":3}}'
|
|
|
|
}
|