mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
x.json2.decoder2: fix array decoding in struct is field, when array is initialized (#24422)
This commit is contained in:
parent
d9afebc277
commit
a412f53e62
2 changed files with 24 additions and 0 deletions
|
@ -618,6 +618,9 @@ fn (mut decoder Decoder) decode_value[T](mut val T) ! {
|
|||
decoder.decode_map(mut val)!
|
||||
return
|
||||
} $else $if T.unaliased_typ is $array {
|
||||
unsafe {
|
||||
val.len = 0
|
||||
}
|
||||
decoder.decode_array(mut val)!
|
||||
// return to avoid the next increment of the current node
|
||||
// this is because the current node is already incremented in the decode_array function
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import x.json2 as json
|
||||
import x.json2.decoder2
|
||||
|
||||
struct Bar {
|
||||
b []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
}
|
||||
|
||||
struct Foo {
|
||||
Bar
|
||||
a []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
}
|
||||
|
||||
fn test_main() {
|
||||
str := json.encode(Foo{})
|
||||
assert decoder2.decode[Foo](str)!.str() == 'Foo{
|
||||
Bar: Bar{
|
||||
b: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
}
|
||||
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||
}'
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue