2022-11-20 12:18:14 +03:00
|
|
|
import x.json2 as json
|
2022-12-05 17:58:44 +03:00
|
|
|
// import time
|
2022-11-20 12:18:14 +03:00
|
|
|
|
|
|
|
enum JobTitle {
|
|
|
|
manager
|
|
|
|
executive
|
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Employee {
|
|
|
|
pub mut:
|
2022-12-05 17:58:44 +03:00
|
|
|
name string
|
|
|
|
age int
|
|
|
|
salary f32
|
|
|
|
// title JobTitle //! FIXME - decode
|
|
|
|
// sub_employee SubEmployee //! FIXME - decode
|
2022-11-20 12:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SubEmployee {
|
|
|
|
pub mut:
|
|
|
|
name string
|
|
|
|
age int
|
|
|
|
salary f32
|
2022-12-05 17:58:44 +03:00
|
|
|
// title JobTitle //! FIXME - decode
|
2022-11-20 12:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn test_simple() {
|
|
|
|
sub_employee := SubEmployee{
|
|
|
|
name: 'João'
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
x := Employee{'Peter', 28, 95000.5}
|
2022-11-26 19:23:26 +03:00
|
|
|
s := json.encode[Employee](x)
|
2022-12-05 17:58:44 +03:00
|
|
|
assert s == '{"name":"Peter","age":28,"salary":95000.5}'
|
|
|
|
|
|
|
|
y := json.decode[Employee](s) or {
|
|
|
|
println(err)
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert y.name == 'Peter'
|
|
|
|
assert y.age == 28
|
|
|
|
assert y.salary == 95000.5
|
|
|
|
// assert y.title == .worker //! FIXME
|
|
|
|
// assert y.sub_employee.name == 'Peter'
|
|
|
|
// assert y.sub_employee.age == 0
|
|
|
|
// assert y.sub_employee.salary == 0.0
|
|
|
|
// assert y.sub_employee.title == .worker //! FIXME
|
2022-11-20 12:18:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// const currency_id = 'cconst'
|
|
|
|
|
|
|
|
// struct Price {
|
|
|
|
// net f64
|
|
|
|
// currency_id string [json: currencyId] = currency_id
|
|
|
|
// }
|
|
|
|
|
|
|
|
// struct User2 {
|
|
|
|
// mut:
|
|
|
|
// age int
|
|
|
|
// nums []int
|
|
|
|
// reg_date time.Time
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // User struct needs to be `pub mut` for now in order to access and manipulate values
|
|
|
|
struct User {
|
|
|
|
pub mut:
|
|
|
|
age int
|
|
|
|
nums []int
|
|
|
|
last_name string [json: lastName]
|
|
|
|
is_registered bool [json: IsRegistered]
|
|
|
|
typ int [json: 'type']
|
|
|
|
pets string [json: 'pet_animals'; raw]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut u User) foo() string {
|
|
|
|
return json.encode(u)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_user() {
|
|
|
|
mut usr := User{
|
|
|
|
age: 10
|
|
|
|
nums: [1, 2, 3]
|
|
|
|
last_name: 'Johnson'
|
|
|
|
is_registered: true
|
|
|
|
typ: 0
|
|
|
|
pets: 'foo'
|
|
|
|
}
|
|
|
|
expected := '{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true,"type":0,"pet_animals":"foo"}'
|
2022-11-26 19:23:26 +03:00
|
|
|
out := json.encode[User](usr)
|
2022-11-20 12:18:14 +03:00
|
|
|
// println(out)
|
|
|
|
assert out == expected
|
|
|
|
// Test json.encode on mutable pointers
|
|
|
|
assert usr.foo() == expected
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:58:44 +03:00
|
|
|
struct Color {
|
|
|
|
pub mut:
|
|
|
|
space string
|
|
|
|
point string [raw]
|
|
|
|
}
|
2022-11-20 12:18:14 +03:00
|
|
|
|
|
|
|
fn test_encode_map() {
|
|
|
|
expected := '{"one":1,"two":2,"three":3,"four":4}'
|
|
|
|
numbers := {
|
|
|
|
'one': json.Any(1)
|
|
|
|
'two': json.Any(2)
|
|
|
|
'three': json.Any(3)
|
|
|
|
'four': json.Any(4)
|
|
|
|
}
|
|
|
|
// numbers := {
|
|
|
|
// 'one': 1
|
|
|
|
// 'two': 2
|
|
|
|
// 'three': 3
|
|
|
|
// 'four': 4
|
|
|
|
// }
|
|
|
|
// out := json.encode(numbers)
|
|
|
|
out := numbers.str()
|
|
|
|
assert out == expected
|
|
|
|
}
|
|
|
|
|
|
|
|
type ID = string
|
|
|
|
type GG = int
|
|
|
|
|
|
|
|
struct Message {
|
|
|
|
id ID
|
|
|
|
ij GG
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_encode_alias_struct() {
|
|
|
|
expected := '{"id":"118499178790780929","ij":999998888}'
|
|
|
|
msg := Message{'118499178790780929', 999998888}
|
2022-11-26 19:23:26 +03:00
|
|
|
out := json.encode[Message](msg)
|
2022-11-20 12:18:14 +03:00
|
|
|
assert out == expected
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StByteArray {
|
|
|
|
ba []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_byte_array() {
|
|
|
|
assert json.encode(StByteArray{ ba: [byte(1), 2, 3, 4, 5] }) == '{"ba":[1,2,3,4,5]}'
|
|
|
|
}
|
2022-12-05 17:58:44 +03:00
|
|
|
|
|
|
|
struct Bar {
|
|
|
|
x string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar[T](payload string) !Bar { // ?T doesn't work currently
|
|
|
|
result := json.decode[T](payload)!
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_generic() {
|
|
|
|
result := bar[Bar]('{"x":"test"}') or { Bar{} }
|
|
|
|
assert result.x == 'test'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_raw_json_field() {
|
|
|
|
color := json.decode[Color]('{"space": "YCbCr", "point": {"Y": 123}}') or {
|
|
|
|
assert false
|
|
|
|
Color{}
|
|
|
|
}
|
|
|
|
assert color.point == '{"Y":123}'
|
|
|
|
assert color.space == 'YCbCr'
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Foo[T] {
|
|
|
|
pub:
|
|
|
|
name string
|
|
|
|
data T
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_generic_struct() {
|
|
|
|
foo_int := Foo[int]{'bar', 12}
|
|
|
|
foo_enc := json.encode(foo_int)
|
|
|
|
assert foo_enc == '{"name":"bar","data":12}'
|
|
|
|
foo_dec := json.decode[Foo[int]](foo_enc)!
|
|
|
|
assert foo_dec.name == 'bar'
|
|
|
|
assert foo_dec.data == 12
|
|
|
|
}
|