Skip to content

Commit

Permalink
json: fix decode codegen for []&type (fix vlang#23007) (vlang#23010)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Nov 30, 2024
1 parent f2c281e commit a66c4c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions vlib/json/tests/json_decode_arr_ref_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json

struct Test {
a string
}

fn test_main() {
x := json.decode([]&Test, '[{"a":"a"}]') or { exit(1) }
assert x[0].a == 'a'
}
4 changes: 2 additions & 2 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size
s = '${styp} val = ${fn_name}((cJSON *)jsval); '
} else if is_array_fixed_val {
s = '
${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval);
${result_name}_${styp.replace('*', '_ptr')} val2 = ${fn_name} ((cJSON *)jsval);
if(val2.is_error) {
${array_free_str}
return *(${result_name}_${ret_styp}*)&val2;
Expand All @@ -1030,7 +1030,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size
memcpy(&val, (${styp}*)val2.data, sizeof(${styp}));'
} else {
s = '
${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval);
${result_name}_${styp.replace('*', '_ptr')} val2 = ${fn_name} ((cJSON *)jsval);
if(val2.is_error) {
${array_free_str}
return *(${result_name}_${ret_styp}*)&val2;
Expand Down

0 comments on commit a66c4c9

Please sign in to comment.