Skip to content

Commit d2587eb

Browse files
authored
cgen: handle pointers in json map decode/encode (fix #25632) (#25646)
1 parent 6520805 commit d2587eb

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import json
2+
3+
fn q_and_a(mut db_json map[string][]string) {
4+
x := json.encode(db_json)
5+
assert x == '{}'
6+
}
7+
8+
fn test_main() {
9+
mut db_json := json.decode(map[string][]string, '{}')!
10+
assert db_json == {}
11+
q_and_a(mut db_json)
12+
}

vlib/v/gen/c/json.v

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,14 +1165,15 @@ fn (mut g Gen) decode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type,
11651165
key_type_symbol := g.table.sym(key_type)
11661166
hash_fn, key_eq_fn, clone_fn, free_fn := g.map_fn_ptrs(key_type_symbol)
11671167
fn_name_v := js_dec_name(styp_v)
1168+
ref, ptr := if utyp.is_ptr() { '', '*' } else { '&', '' }
11681169
mut s := ''
11691170
if is_js_prim(styp_v) {
11701171
s = '${styp_v} val = ${fn_name_v} (js_get(root, jsval->string));'
11711172
} else {
11721173
s = '
11731174
${result_name}_${ret_styp} val2 = ${fn_name_v} (js_get(root, jsval->string));
11741175
if(val2.is_error) {
1175-
builtin__map_free(&res);
1176+
builtin__map_free(${ref}res);
11761177
return *(${result_name}_${ustyp}*)&val2;
11771178
}
11781179
${styp_v} val = *(${styp_v}*)val2.data;
@@ -1198,13 +1199,13 @@ fn (mut g Gen) decode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type,
11981199
if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) {
11991200
return (${result_name}_${ustyp}){ .is_error = true, .err = builtin___v_error(builtin__string__plus(_S("Json element is not an object: "), json__json_print(root))), .data = {0}};
12001201
}
1201-
res = builtin__new_map(sizeof(${styp}), sizeof(${styp_v}), ${hash_fn}, ${key_eq_fn}, ${clone_fn}, ${free_fn});
1202+
${ptr}res = builtin__new_map(sizeof(${styp}), sizeof(${styp_v}), ${hash_fn}, ${key_eq_fn}, ${clone_fn}, ${free_fn});
12021203
cJSON *jsval = NULL;
12031204
cJSON_ArrayForEach(jsval, root)
12041205
{
12051206
${s}
12061207
string key = builtin__tos2((byteptr)jsval->string);
1207-
builtin__map_set(&res, &key, &val);
1208+
builtin__map_set(${ref}res, &key, &val);
12081209
}
12091210
'
12101211
}
@@ -1234,12 +1235,13 @@ fn (mut g Gen) encode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type)
12341235
builtin__array_free(&${keys_tmp});
12351236
'
12361237
} else {
1238+
ref := if utyp.is_ptr() { '' } else { '&' }
12371239
return '
12381240
o = cJSON_CreateObject();
1239-
Array_${styp} ${keys_tmp} = builtin__map_keys(&val);
1241+
Array_${styp} ${keys_tmp} = builtin__map_keys(${ref}val);
12401242
for (${ast.int_type_name} i = 0; i < ${keys_tmp}.len; ++i) {
12411243
${key}
1242-
cJSON_AddItemToObject(o, (char*) key.str, ${fn_name_v} ( *(${styp_v}*) builtin__map_get(&val, &key, &(${styp_v}[]) { ${zero} } ) ) );
1244+
cJSON_AddItemToObject(o, (char*) key.str, ${fn_name_v} ( *(${styp_v}*) builtin__map_get(${ref}val, &key, &(${styp_v}[]) { ${zero} } ) ) );
12431245
}
12441246
builtin__array_free(&${keys_tmp});
12451247
'

0 commit comments

Comments
 (0)