diff --git a/vlib/json/json_omitempty_types_test.v b/vlib/json/json_omitempty_types_test.v new file mode 100644 index 0000000000..38050badc7 --- /dev/null +++ b/vlib/json/json_omitempty_types_test.v @@ -0,0 +1,104 @@ +import json + +struct OmitEmptyStruct { + bug Struct [omitempty] +} + +type MyAlias = string + +struct OmitEmptyAlias { + bug MyAlias [omitempty] +} + +struct Struct { + name string +} + +struct OmitEmptyMap { + bug map[string]string [omitempty] +} + +struct OmitEmptyArray { + bug []string [omitempty] +} + +type MySum = int | string + +struct OmitEmptySumType { + bug MySum [omitempty] +} + +fn main() { + test_struct() + test_alias() + test_map() + test_sumtype() + test_array() +} + +fn test_struct() { + test2 := OmitEmptyStruct{ + bug: Struct{} + } + encoded2 := json.encode(test2) + dump(encoded2) + test := OmitEmptyStruct{ + bug: Struct{ + name: 'mybug' + } + } + encoded := json.encode(test) + dump(encoded) +} + +fn test_alias() { + test3 := OmitEmptyAlias{ + bug: '' + } + encoded3 := json.encode(test3) + dump(encoded3) + test4 := OmitEmptyAlias{ + bug: 'foo' + } + encoded4 := json.encode(test4) + dump(encoded4) +} + +fn test_map() { + test3 := OmitEmptyMap{ + bug: {} + } + encoded3 := json.encode(test3) + dump(encoded3) + test4 := OmitEmptyMap{ + bug: { + 'foo': 'bar' + } + } + encoded4 := json.encode(test4) + dump(encoded4) +} + +fn test_sumtype() { + test3 := OmitEmptySumType{} + encoded3 := json.encode(test3) + dump(encoded3) + test4 := OmitEmptySumType{ + bug: 1 + } + encoded4 := json.encode(test4) + dump(encoded4) +} + +fn test_array() { + test3 := OmitEmptyArray{ + bug: [] + } + encoded3 := json.encode(test3) + dump(encoded3) + test4 := OmitEmptyArray{ + bug: ['1'] + } + encoded4 := json.encode(test4) + dump(encoded4) +} diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index db3ba06f37..b81537b48a 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -694,7 +694,22 @@ 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 { - enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)} != ${g.type_default(field.typ)})') + if 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)}))') + } else if field_sym.kind == .sum_type { + enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)}._typ != 0)') + } else if field_sym.kind == .map { + enc.writeln('${indent}if (!${ptr_typ}_map_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))') + } else if field_sym.kind == .array { + enc.writeln('${indent}if (!${ptr_typ}_arr_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))') + } else if field_sym.kind == .struct_ { + enc.writeln('${indent}if (!${ptr_typ}_struct_eq(${prefix_enc}${op}${c_name(field.name)}, ${g.type_default(field.typ)}))') + } + } else { + enc.writeln('${indent}if (${prefix_enc}${op}${c_name(field.name)} != ${g.type_default(field.typ)})') + } } } if !is_js_prim(field_type) {