Skip to content

Commit a412f53

Browse files
authored
x.json2.decoder2: fix array decoding in struct is field, when array is initialized (#24422)
1 parent d9afebc commit a412f53

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

vlib/x/json2/decoder2/decode.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,9 @@ fn (mut decoder Decoder) decode_value[T](mut val T) ! {
618618
decoder.decode_map(mut val)!
619619
return
620620
} $else $if T.unaliased_typ is $array {
621+
unsafe {
622+
val.len = 0
623+
}
621624
decoder.decode_array(mut val)!
622625
// return to avoid the next increment of the current node
623626
// this is because the current node is already incremented in the decode_array function
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import x.json2 as json
2+
import x.json2.decoder2
3+
4+
struct Bar {
5+
b []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
6+
}
7+
8+
struct Foo {
9+
Bar
10+
a []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
11+
}
12+
13+
fn test_main() {
14+
str := json.encode(Foo{})
15+
assert decoder2.decode[Foo](str)!.str() == 'Foo{
16+
Bar: Bar{
17+
b: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
18+
}
19+
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
20+
}'
21+
}

0 commit comments

Comments
 (0)