json: omitempty

pull/11518/head
Alexander Medvednikov 2021-09-16 07:02:43 +03:00
parent 5bc6cc9512
commit d00808660f
2 changed files with 22 additions and 0 deletions

View File

@ -366,3 +366,17 @@ fn test_pretty() {
"name": "Bob"
}'
}
struct Foo3 {
name string
age int [omitempty]
}
fn test_omit_empty() {
foo := Foo3{'Bob', 0}
assert json.encode_pretty(foo) == '{
"name": "Bob"
}'
// println('omitempty:')
// println(json.encode_pretty(foo))
}

View File

@ -117,6 +117,8 @@ fn (mut g Gen) gen_struct_enc_dec(type_info ast.TypeInfo, styp string, mut enc s
mut is_raw := false
mut is_skip := false
mut is_required := false
mut is_omit_empty := false
for attr in field.attrs {
match attr.name {
'json' {
@ -131,6 +133,9 @@ fn (mut g Gen) gen_struct_enc_dec(type_info ast.TypeInfo, styp string, mut enc s
'required' {
is_required = true
}
'omitempty' {
is_omit_empty = true
}
else {}
}
}
@ -184,6 +189,9 @@ fn (mut g Gen) gen_struct_enc_dec(type_info ast.TypeInfo, styp string, mut enc s
}
// Encoding
mut enc_name := js_enc_name(field_type)
if is_omit_empty {
enc.writeln('\t if (val.${c_name(field.name)} != ${g.type_default(field.typ)}) \n')
}
if !is_js_prim(field_type) {
if field_sym.kind == .alias {
ainfo := field_sym.info as ast.Alias