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

json: fix error for json encoding sumtype value (#13248)

This commit is contained in:
yuyi 2022-01-22 23:55:19 +08:00 committed by GitHub
parent 839ae6b2e4
commit e66e35ced1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -430,3 +430,27 @@ fn test_omit_empty() {
// println('omitempty:') // println('omitempty:')
// println(json.encode_pretty(foo)) // println(json.encode_pretty(foo))
} }
struct Asdasd {
data GamePacketData
}
type GamePacketData = GPEquipItem | GPScale
struct GPScale {
value f32
}
struct GPEquipItem {
name string
}
fn create_game_packet(data &GamePacketData) string {
return json.encode(data)
}
fn test_encode_sumtype_defined_ahead() {
ret := create_game_packet(&GamePacketData(GPScale{}))
println(ret)
assert ret == '{"value":0,"_type":"GPScale"}'
}

View File

@ -128,7 +128,7 @@ $enc_fn_dec {
fn (mut g Gen) gen_sumtype_enc_dec(sym ast.TypeSymbol, mut enc strings.Builder, mut dec strings.Builder) { fn (mut g Gen) gen_sumtype_enc_dec(sym ast.TypeSymbol, mut enc strings.Builder, mut dec strings.Builder) {
info := sym.info as ast.SumType info := sym.info as ast.SumType
type_var := g.new_tmp_var() type_var := g.new_tmp_var()
typ := sym.idx typ := g.table.type_idxs[sym.name]
// DECODING (inline) // DECODING (inline)
$if !json_no_inline_sumtypes ? { $if !json_no_inline_sumtypes ? {