mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
json: fix encode/decode support for generic structs (#6489)
This commit is contained in:
parent
05dcdfd267
commit
1aec041371
6 changed files with 62 additions and 10 deletions
|
@ -218,7 +218,7 @@ fn test_nested_type() {
|
|||
assert data2.countries[i].cities[j].name == data.countries[i].cities[j].name
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for key, user in data.users {
|
||||
assert data2.users[key].age == user.age
|
||||
assert data2.users[key].nums == user.nums
|
||||
|
@ -235,6 +235,25 @@ fn test_nested_type() {
|
|||
}
|
||||
}
|
||||
|
||||
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) or {
|
||||
exit(1)
|
||||
}
|
||||
|
||||
assert foo_dec.name == 'bar'
|
||||
assert foo_dec.data == 12
|
||||
}
|
||||
|
||||
fn test_errors() {
|
||||
invalid_array := fn () {
|
||||
data := '{"countries":[{"cities":[{"name":"London"},{"name":"Manchester"}],"name":"UK"},{"cities":{"name":"Donlon"},"name":"KU"}],"users":{"Foo":{"age":10,"nums":[1,2,3],"lastName":"Johnson","IsRegistered":true,"type":0,"pet_animals":"little foo"},"Boo":{"age":20,"nums":[5,3,1],"lastName":"Smith","IsRegistered":false,"type":4,"pet_animals":"little boo"}},"extra":{"2":{"n1":2,"n2":4,"n3":8,"n4":16},"3":{"n1":3,"n2":9,"n3":27,"n4":81}}}'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue