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 8ae627f commit bd22d58
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
27 changes: 27 additions & 0 deletions vlib/json/json_encode_embed_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import json

struct Json2 {
inner []f64
}

struct Json {
Json2
test f64
}

fn test_main() {
str := '{"inner":[1,2,3,4,5],"test":1.2}'
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.2

data_json := json.encode(data)
dump(data_json)
assert data_json == str
}
11 changes: 6 additions & 5 deletions vlib/v/gen/c/json.v
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
mut is_skip := false
mut is_required := false
mut is_omit_empty := false
mut skip_embed := false

for attr in field.attrs {
match attr.name {
Expand Down Expand Up @@ -768,10 +769,10 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
if embed == int(field.typ) {
g.gen_struct_enc_dec(field.typ, g.table.sym(field.typ).info,
styp, mut enc, mut dec, name)
skip_embed = true
break
}
}
continue
}
tmp := g.new_tmp_var()
gen_js_get_opt(dec_name, field_type, styp, tmp, name, mut dec, is_required)
Expand All @@ -797,15 +798,15 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
dec.writeln('\t}')
}
}
if embed_prefix.len > 0 {
return
if skip_embed {
continue
}
// Encoding
mut enc_name := js_enc_name(field_type)
prefix_enc := if utyp.has_flag(.option) {
'(*(${g.base_type(utyp)}*)val.data)'
'(*(${g.base_type(utyp)}*)val${embed_member}.data)'
} else {
'val'
'val${embed_member}'
}
is_option := field.typ.has_flag(.option)
indent := if is_option { '\t\t' } else { '\t' }
Expand Down

0 comments on commit bd22d58

Please sign in to comment.