diff --git a/vlib/json/json_encode_ptr_test.v b/vlib/json/json_encode_ptr_test.v new file mode 100644 index 0000000000..ee21a68ac6 --- /dev/null +++ b/vlib/json/json_encode_ptr_test.v @@ -0,0 +1,31 @@ +import json + +struct Number { + min int + max int +} + +pub struct Resp { +pub: + options []string @[omitempty] + number &Number = unsafe { nil } @[omitempty] +} + +fn (r Resp) str() string { + return json.encode(r) +} + +fn test_main() { + r1 := Resp{ + options: ['first', 'second'] + } + r2 := Resp{ + number: &Number{0, 0} + } + r3 := Resp{ + number: &Number{1, 2} + } + assert r1.str() == '{"options":["first","second"]}' + assert r2.str() == '{"number":{"min":0,"max":0}}' + assert r3.str() == '{"number":{"min":1,"max":2}}' +} diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 6e19fb2414..8006a935ec 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -842,7 +842,8 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st } else if field.typ == ast.string_type { enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)}.len != 0)') } else { - if field_sym.kind in [.alias, .sum_type, .map, .array, .struct] { + if !field.typ.is_ptr() + && field_sym.kind in [.alias, .sum_type, .map, .array, .struct] { ptr_typ := g.equality_fn(field.typ) if field_sym.kind == .alias { enc.writeln('${indent}if (!${ptr_typ}_alias_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))')