mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
json: omitempty
This commit is contained in:
parent
5bc6cc9512
commit
d00808660f
@ -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))
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user