Skip to content

Commit 5eecd04

Browse files
authored
json: fix default struct field initialization with long array (#23355)
1 parent f821c65 commit 5eecd04

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import json
2+
3+
struct Bar {
4+
b []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
5+
}
6+
7+
struct Foo {
8+
Bar
9+
a []int = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
10+
}
11+
12+
fn test_main() {
13+
str := json.encode(Foo{})
14+
assert json.decode(Foo, str)!.str() == 'Foo{
15+
Bar: Bar{
16+
b: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
17+
}
18+
a: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
19+
}'
20+
}

‎vlib/v/gen/c/json.v‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -848,8 +848,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
848848
if field.has_default_expr {
849849
dec.writeln('\t} else {')
850850
default_str := g.expr_string_opt(field.typ, field.default_expr)
851-
lines := default_str.count('\n')
852-
if lines > 1 {
851+
if default_str.count(';\n') > 1 {
853852
dec.writeln(default_str.all_before_last('\n'))
854853
dec.writeln('\t\t${prefix}${op}${c_name(field.name)} = ${default_str.all_after_last('\n')};')
855854
} else {

0 commit comments

Comments
 (0)