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

cgen: fix json.encode of a struct containing a field of an alias type of another struct (#15490)

This commit is contained in:
yuyi 2022-08-22 16:04:58 +08:00 committed by GitHub
parent f727433929
commit 47e75c68a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -488,3 +488,23 @@ struct StByteArray {
fn test_byte_array() {
assert json.encode(StByteArray{ ba: [byte(1), 2, 3, 4, 5] }) == '{"ba":[1,2,3,4,5]}'
}
struct Aa {
sub SumType
}
struct Bb {
a int
}
type SumType = Bb
fn test_encode_alias_field() {
s := json.encode(Aa{
sub: SumType(Bb{
a: 1
})
})
println(s)
assert s == '{"sub":{"a":1}}'
}

View File

@ -436,7 +436,7 @@ fn (mut g Gen) gen_struct_enc_dec(type_info ast.TypeInfo, styp string, mut enc s
}
dec.writeln('\t}')
} else {
g.gen_json_for_type(field.typ)
g.gen_json_for_type(alias.parent_type)
tmp := g.new_tmp_var()
gen_js_get_opt(dec_name, field_type, styp, tmp, name, mut dec, is_required)
dec.writeln('\tif (jsonroot_$tmp) {')