From 0037a2ccfd63f72ab90f34fa8075849a738a7a72 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 8 Oct 2024 08:18:17 -0300 Subject: [PATCH] fix --- vlib/json/json_fixed_array_test.v | 25 +++++++++++++++++++++++++ vlib/v/gen/c/json.v | 16 +++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 vlib/json/json_fixed_array_test.v diff --git a/vlib/json/json_fixed_array_test.v b/vlib/json/json_fixed_array_test.v new file mode 100644 index 00000000000000..d97ce8f0f9e187 --- /dev/null +++ b/vlib/json/json_fixed_array_test.v @@ -0,0 +1,25 @@ +import json + +struct Base { + options Options + profiles Profiles +} + +struct Options { + cfg [6]u8 +} + +struct Profiles { + cfg [4][7]u8 +} + +fn test_main() { + a := json.encode(Base{}) + println(a) + assert a.contains('"cfg":[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]') + + b := json.decode(Base, a)! + assert b.options.cfg.len == 6 + assert b.profiles.cfg.len == 4 + assert b.profiles.cfg[0].len == 7 +} diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 20e6b1bebc1d9c..7583a2f468ff31 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -983,6 +983,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size mut fixed_array_idx := '' mut fixed_array_idx_increment := '' mut array_element_assign := '' + is_array_fixed_val := g.table.final_sym(value_type).kind == .array_fixed if utyp.has_flag(.option) { if fixed_array_size > -1 { fixed_array_idx += 'int fixed_array_idx = 0;' @@ -994,7 +995,11 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size array_free_str += 'array_free(&res.data);' } } else { - if fixed_array_size > -1 { + if is_array_fixed_val { + fixed_array_idx += 'int fixed_array_idx = 0;' + array_element_assign += 'memcpy(res[fixed_array_idx], val, sizeof(${styp}));' + fixed_array_idx_increment += 'fixed_array_idx++;' + } else if fixed_array_size > -1 { fixed_array_idx += 'int fixed_array_idx = 0;' array_element_assign += 'res[fixed_array_idx] = val;' fixed_array_idx_increment += 'fixed_array_idx++;' @@ -1008,6 +1013,15 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size mut s := '' if is_js_prim(styp) { s = '${styp} val = ${fn_name}((cJSON *)jsval); ' + } else if is_array_fixed_val { + s = ' + ${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval); + if(val2.is_error) { + ${array_free_str} + return *(${result_name}_${ret_styp}*)&val2; + } + ${styp} val; + memcpy(&val, (${styp}*)val2.data, sizeof(${styp}));' } else { s = ' ${result_name}_${styp} val2 = ${fn_name} ((cJSON *)jsval);