Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Sep 21, 2024
1 parent bd22d58 commit 493feae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
31 changes: 31 additions & 0 deletions vlib/json/json_embed_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json

struct Json3 {
embed f64
}

struct Json2 {
Json3
inner []f64
}

struct Json {
Json2
test f64
}

fn test_main() {
str := '{"inner": [1, 2, 3, 4, 5],"test": 1.0, "embed": 2.0}'
data := json.decode(Json, str) or {
eprintln('Failed to decode json, error: ${err}')
return
}
println(data)
assert data.inner.len == 5
assert data.inner[0] == 1.0
assert data.inner[4] == 5.0
assert data.test == 1.0
assert data.embed == 2.0

assert dump(json.encode(data)) == '{"embed":2,"inner":[1,2,3,4,5],"test":1}'
}
7 changes: 6 additions & 1 deletion vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -767,8 +767,13 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
if name.len > 0 && name[0].is_capital() && field_sym.info is ast.Struct {
for embed in info.embeds {
if embed == int(field.typ) {
prefix_embed := if embed_prefix != '' {
'${embed_prefix}.${name}'
} else {
name
}
g.gen_struct_enc_dec(field.typ, g.table.sym(field.typ).info,
styp, mut enc, mut dec, name)
styp, mut enc, mut dec, prefix_embed)
skip_embed = true
break
}
Expand Down

0 comments on commit 493feae

Please sign in to comment.