x.json2.decoder2: fix array decoding in struct is field, when array is initialized (#24422)

This commit is contained in:
Hitalo Souza 2025-05-06 10:32:53 +01:00 committed by GitHub
parent d9afebc277
commit a412f53e62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View file

@ -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

View file

@ -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]
}'
}