1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json: fix [omitempty] with string (#17813)

This commit is contained in:
Felipe Pena 2023-03-30 17:09:47 -03:00 committed by GitHub
parent 57aa4def62
commit cd6cc65ece
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 19 deletions

View File

@ -0,0 +1,25 @@
import json
pub struct MyStruct {
pub mut:
code int
message string
data string [omitempty]
data2 ?string [omitempty]
}
fn test_simple() {
obj := MyStruct{
code: 1
message: 'yes'
data2: 'a'
}
assert dump(json.encode(obj)) == '{"code":1,"message":"yes","data2":"a"}'
}
fn test_none() {
obj := MyStruct{
code: 1
}
assert dump(json.encode(obj)) == '{"code":1,"message":""}'
}

View File

@ -648,11 +648,18 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
'val' 'val'
} }
is_option := field.typ.has_flag(.option) is_option := field.typ.has_flag(.option)
indent := if is_option { '\t\t' } else { '\t' }
if is_option { if is_option {
enc.writeln('\tif (val${op}${c_name(field.name)}.state != 2) {') enc.writeln('\tif (val${op}${c_name(field.name)}.state != 2) {')
} }
if is_omit_empty { if is_omit_empty {
enc.writeln('\t if (val${op}${c_name(field.name)} != ${g.type_default(field.typ)})') if field.typ.has_flag(.option) {
enc.writeln('${indent}if (val${op}${c_name(field.name)}.state != 2)')
} else if field.typ == ast.string_type {
enc.writeln('${indent}if (val${op}${c_name(field.name)}.len != 0)')
} else {
enc.writeln('${indent}if (val${op}${c_name(field.name)} != ${g.type_default(field.typ)})')
}
} }
if !is_js_prim(field_type) { if !is_js_prim(field_type) {
if field_sym.kind == .alias { if field_sym.kind == .alias {
@ -663,44 +670,44 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
if field_sym.kind == .enum_ { if field_sym.kind == .enum_ {
if g.is_enum_as_int(field_sym) { if g.is_enum_as_int(field_sym) {
if field.typ.has_flag(.option) { if field.typ.has_flag(.option) {
enc.writeln('\tcJSON_AddItemToObject(o, "${name}", json__encode_u64(*${prefix_enc}${op}${c_name(field.name)}.data));\n') enc.writeln('${indent}\tcJSON_AddItemToObject(o, "${name}", json__encode_u64(*${prefix_enc}${op}${c_name(field.name)}.data));\n')
} else { } else {
enc.writeln('\tcJSON_AddItemToObject(o, "${name}", json__encode_u64(${prefix_enc}${op}${c_name(field.name)}));\n') enc.writeln('${indent}\tcJSON_AddItemToObject(o, "${name}", json__encode_u64(${prefix_enc}${op}${c_name(field.name)}));\n')
} }
} else { } else {
if field.typ.has_flag(.option) { if field.typ.has_flag(.option) {
enc.writeln('\t{') enc.writeln('${indent}\t{')
enc.writeln('\t\tcJSON *enum_val;') enc.writeln('${indent}\t\tcJSON *enum_val;')
g.gen_enum_to_str(field.typ, field_sym, '*(${g.base_type(field.typ)}*)${prefix_enc}${op}${c_name(field.name)}.data', g.gen_enum_to_str(field.typ, field_sym, '*(${g.base_type(field.typ)}*)${prefix_enc}${op}${c_name(field.name)}.data',
'enum_val', '\t\t', mut enc) 'enum_val', '${indent}\t\t', mut enc)
enc.writeln('\t\tcJSON_AddItemToObject(o, "${name}", enum_val);') enc.writeln('${indent}\t\tcJSON_AddItemToObject(o, "${name}", enum_val);')
enc.writeln('\t}') enc.writeln('${indent}\t}')
} else { } else {
enc.writeln('\t{') enc.writeln('${indent}\t{')
enc.writeln('\t\tcJSON *enum_val;') enc.writeln('${indent}\t\tcJSON *enum_val;')
g.gen_enum_to_str(field.typ, field_sym, '${prefix_enc}${op}${c_name(field.name)}', g.gen_enum_to_str(field.typ, field_sym, '${prefix_enc}${op}${c_name(field.name)}',
'enum_val', '\t\t', mut enc) 'enum_val', '${indent}\t\t', mut enc)
enc.writeln('\t\tcJSON_AddItemToObject(o, "${name}", enum_val);') enc.writeln('${indent}\t\tcJSON_AddItemToObject(o, "${name}", enum_val);')
enc.writeln('\t}') enc.writeln('${indent}\t}')
} }
} }
} else { } else {
if field_sym.name == 'time.Time' { if field_sym.name == 'time.Time' {
// time struct requires special treatment // time struct requires special treatment
// it has to be encoded as a unix timestamp number // it has to be encoded as a unix timestamp number
enc.writeln('\tcJSON_AddItemToObject(o, "${name}", json__encode_u64(${prefix_enc}${op}${c_name(field.name)}._v_unix));') enc.writeln('${indent}\tcJSON_AddItemToObject(o, "${name}", json__encode_u64(${prefix_enc}${op}${c_name(field.name)}._v_unix));')
} else { } else {
if !field.typ.is_real_pointer() { if !field.typ.is_real_pointer() {
enc.writeln('\tcJSON_AddItemToObject(o, "${name}", ${enc_name}(${prefix_enc}${op}${c_name(field.name)})); /*A*/') enc.writeln('${indent}\tcJSON_AddItemToObject(o, "${name}", ${enc_name}(${prefix_enc}${op}${c_name(field.name)})); /*A*/')
} else { } else {
arg_prefix := if field.typ.is_ptr() { '' } else { '*' } arg_prefix := if field.typ.is_ptr() { '' } else { '*' }
sptr_value := '${prefix_enc}${op}${c_name(field.name)}' sptr_value := '${prefix_enc}${op}${c_name(field.name)}'
if !field.typ.has_flag(.option) { if !field.typ.has_flag(.option) {
enc.writeln('\tif (${sptr_value} != 0) {') enc.writeln('${indent}\tif (${sptr_value} != 0) {')
enc.writeln('\t\tcJSON_AddItemToObject(o, "${name}", ${enc_name}(${arg_prefix}${sptr_value}));') enc.writeln('${indent}\t\tcJSON_AddItemToObject(o, "${name}", ${enc_name}(${arg_prefix}${sptr_value}));')
enc.writeln('\t}\n') enc.writeln('${indent}\t}\n')
} else { } else {
enc.writeln('\t\tcJSON_AddItemToObject(o, "${name}", ${enc_name}(${arg_prefix}${sptr_value}));') enc.writeln('${indent}\t\tcJSON_AddItemToObject(o, "${name}", ${enc_name}(${arg_prefix}${sptr_value}));')
} }
} }
} }