Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Dec 15, 2024
1 parent 3d5f81b commit f78977e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
6 changes: 6 additions & 0 deletions vlib/json/json_primitives.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ fn json_parse(s string) &C.cJSON {
@[markused]
fn json_print(data &C.cJSON) string {
s := C.cJSON_PrintUnformatted(data)
if s == unsafe { nil } {
return ''
}
r := unsafe { tos_clone(&u8(s)) }
C.cJSON_free(s)
return r
Expand All @@ -260,6 +263,9 @@ fn json_print(data &C.cJSON) string {
@[markused]
fn json_print_pretty(data &C.cJSON) string {
s := C.cJSON_Print(data)
if s == unsafe { nil } {
return ''
}
r := unsafe { tos_clone(&u8(s)) }
C.cJSON_free(s)
return r
Expand Down
19 changes: 6 additions & 13 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,6 @@ ${enc_fn_dec} {
// cJSON_delete
dec.writeln('\t${result_name}_${ret_styp} ret;')
dec.writeln('\t_result_ok(&res, (${result_name}*)&ret, sizeof(res));')
if utyp.has_flag(.option) {
dec.writeln('\tif (res.state != 2) {')
dec.writeln('\t\t_option_ok(&res.data, (${option_name}*)&ret.data, sizeof(${g.base_type(utyp)}));')
dec.writeln('\t}')
}
dec.writeln('\treturn ret;\n}')
enc.writeln('\treturn o;\n}')
g.gowrappers.writeln(dec.str())
Expand Down Expand Up @@ -646,7 +641,7 @@ fn (mut g Gen) gen_prim_type_validation(name string, typ ast.Type, tmp string, r
return
}
dec.writeln('if (!(${type_check})) {')
dec.writeln('\treturn (${ret_styp}){ .is_error = true, .err = _v_error(string__plus(_SLIT("type mismatch for field \'${name}\', expecting `${g.table.type_to_str(typ)}` type, got: "), tos5(cJSON_PrintUnformatted(jsonroot_${tmp})))), .data = {0} };')
dec.writeln('\treturn (${ret_styp}){ .is_error = true, .err = _v_error(string__plus(_SLIT("type mismatch for field \'${name}\', expecting `${g.table.type_to_str(typ)}` type, got: "), json__json_print(jsonroot_${tmp}))), .data = {0} };')
dec.writeln('}')
}

Expand Down Expand Up @@ -712,11 +707,9 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
}
dec.writeln('\t\t_option_none(&(${base_typ}[]) { ${default_init} }, (${option_name}*)&${prefix}${op}${c_name(field.name)}, sizeof(${base_typ}));')
dec.writeln('\telse')
dec.writeln('\t\t_option_ok(&(${base_typ}[]) { tos5(cJSON_PrintUnformatted(js_get(root, "${name}"))) }, (${option_name}*)&${prefix}${op}${c_name(field.name)}, sizeof(${base_typ}));')
dec.writeln('\t\t_option_ok(&(${base_typ}[]) { json__json_print(js_get(root, "${name}")) }, (${option_name}*)&${prefix}${op}${c_name(field.name)}, sizeof(${base_typ}));')
} else {
dec.writeln(
'\t${prefix}${op}${c_name(field.name)} = tos5(cJSON_PrintUnformatted(' +
'js_get(root, "${name}")));')
dec.writeln('\t${prefix}${op}${c_name(field.name)} = json__json_print(js_get(root, "${name}"));')
}
} else {
// Now generate decoders for all field types in this struct
Expand Down Expand Up @@ -1076,7 +1069,7 @@ fn (mut g Gen) decode_array(utyp ast.Type, value_type ast.Type, fixed_array_size

return '
if(root && !cJSON_IsArray(root) && !cJSON_IsNull(root)) {
return (${result_name}_${ret_styp}){.is_error = true, .err = _v_error(string__plus(_SLIT("Json element is not an array: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}};
return (${result_name}_${ret_styp}){.is_error = true, .err = _v_error(string__plus(_SLIT("Json element is not an array: "), json__json_print(root))), .data = {0}};
}
${res_str}
const cJSON *jsval = NULL;
Expand Down Expand Up @@ -1145,7 +1138,7 @@ fn (mut g Gen) decode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type,
if utyp.has_flag(.option) {
return '
if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) {
return (${result_name}_${ustyp}){ .is_error = true, .err = _v_error(string__plus(_SLIT("Json element is not an object: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}};
return (${result_name}_${ustyp}){ .is_error = true, .err = _v_error(string__plus(_SLIT("Json element is not an object: "), json__json_print(root))), .data = {0}};
}
_option_ok(&(${g.base_type(utyp)}[]) { new_map(sizeof(${styp}), sizeof(${styp_v}), ${hash_fn}, ${key_eq_fn}, ${clone_fn}, ${free_fn}) }, (${option_name}*)&res, sizeof(${g.base_type(utyp)}));
cJSON *jsval = NULL;
Expand All @@ -1159,7 +1152,7 @@ fn (mut g Gen) decode_map(utyp ast.Type, key_type ast.Type, value_type ast.Type,
} else {
return '
if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) {
return (${result_name}_${ustyp}){ .is_error = true, .err = _v_error(string__plus(_SLIT("Json element is not an object: "), tos2((byteptr)cJSON_PrintUnformatted(root)))), .data = {0}};
return (${result_name}_${ustyp}){ .is_error = true, .err = _v_error(string__plus(_SLIT("Json element is not an object: "), json__json_print(root))), .data = {0}};
}
res = new_map(sizeof(${styp}), sizeof(${styp_v}), ${hash_fn}, ${key_eq_fn}, ${clone_fn}, ${free_fn});
cJSON *jsval = NULL;
Expand Down

0 comments on commit f78977e

Please sign in to comment.