1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json: fix struct field default value support (#14304)

This commit is contained in:
StunxFS
2022-05-20 04:22:17 -04:00
committed by GitHub
parent ca00b59b3f
commit 11bdb04d0c
5 changed files with 103 additions and 25 deletions

View File

@@ -27,6 +27,25 @@ fn test_simple() ? {
assert y.title == .worker
}
const currency_id = 'cconst'
struct Price {
net f64
currency_id string [json: currencyId] = currency_id
}
fn test_field_with_default_expr() ? {
data := '[{"net":1},{"net":2,"currencyId":"cjson"}]'
prices := json.decode([]Price, data)?
assert prices == [Price{
net: 1
currency_id: 'cconst'
}, Price{
net: 2
currency_id: 'cjson'
}]
}
fn test_decode_top_level_array() {
s := '[{"name":"Peter", "age": 29}, {"name":"Bob", "age":31}]'
x := json.decode([]Employee, s) or { panic(err) }
@@ -454,3 +473,11 @@ fn test_encode_sumtype_defined_ahead() {
println(ret)
assert ret == '{"value":0,"_type":"GPScale"}'
}
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]}'
}