Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 11, 2023
1 parent e7cad4f commit fff005f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions vlib/json/json_encode_recursive_ptr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import json

struct PostTag {
id string
parent ?&PostTag
visibility string
created_at string [json: 'createdAt']
metadata string [raw]
}

fn test_main() {
new_post_tag := PostTag{}
assert json.encode(new_post_tag) == '{"id":"","visibility":"","createdAt":"","metadata":""}'

new_post_tag2 := PostTag{
parent: new_post_tag
}
assert json.encode(new_post_tag2) == '{"id":"","parent":{"id":"","visibility":"","createdAt":"","metadata":""},"visibility":"","createdAt":"","metadata":""}'
}
5 changes: 3 additions & 2 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn (mut g Gen) gen_jsons() {

mut init_styp := '${styp} res'
if utyp.has_flag(.option) {
if sym.kind == .struct_ {
if sym.kind == .struct_ && !utyp.is_ptr() {
init_styp += ' = '
g.set_current_pos_as_last_stmt_pos()
pos := g.out.len
Expand Down Expand Up @@ -634,7 +634,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
dec.writeln('\telse')
dec.writeln('\t\t_option_ok(&(${base_typ}[]) { tos5(cJSON_PrintUnformatted(js_get(root, "${name}"))) }, &${prefix}${op}${c_name(field.name)}, sizeof(${base_typ}));')
} else {
dec.writeln('\tres${op}${c_name(field.name)} = tos5(cJSON_PrintUnformatted(' +
dec.writeln(
'\t${prefix}${op}${c_name(field.name)} = tos5(cJSON_PrintUnformatted(' +
'js_get(root, "${name}")));')
}
} else {
Expand Down

0 comments on commit fff005f

Please sign in to comment.