Skip to content

Commit a66c4c9

Browse files
authored
json: fix decode codegen for []&type (fix #23007) (#23010)
1 parent f2c281e commit a66c4c9

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import json
2+
3+
struct Test {
4+
a string
5+
}
6+
7+
fn test_main() {
8+
x := json.decode([]&Test, '[{"a":"a"}]') or { exit(1) }
9+
assert x[0].a == 'a'
10+
}

vlib/v/gen/c/json.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size
10211021
s = '${styp} val = ${fn_name}((cJSON *)jsval); '
10221022
} else if is_array_fixed_val {
10231023
s = '
1024-
${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval);
1024+
${result_name}_${styp.replace('*', '_ptr')} val2 = ${fn_name} ((cJSON *)jsval);
10251025
if(val2.is_error) {
10261026
${array_free_str}
10271027
return *(${result_name}_${ret_styp}*)&val2;
@@ -1030,7 +1030,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size
10301030
memcpy(&val, (${styp}*)val2.data, sizeof(${styp}));'
10311031
} else {
10321032
s = '
1033-
${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval);
1033+
${result_name}_${styp.replace('*', '_ptr')} val2 = ${fn_name} ((cJSON *)jsval);
10341034
if(val2.is_error) {
10351035
${array_free_str}
10361036
return *(${result_name}_${ret_styp}*)&val2;

0 commit comments

Comments
 (0)