Skip to content

Commit

Permalink
json: fix struct with option enum field (fix #20597) #20597
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Jan 20, 2024
1 parent bf8b7f0 commit df3591c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions vlib/json/json_decode_option_enum_test.v
@@ -0,0 +1,18 @@
import json

enum Lang {
en = 1
}

struct Request {
lang ?Lang // ?string, ?int are ok
}

fn test_main() {
assert dump(json.decode(Request, '{}')!) == Request{
lang: ?Lang(none)
}
assert dump(json.decode(Request, '{"lang": "en"}')!) == Request{
lang: .en
}
}
2 changes: 1 addition & 1 deletion vlib/v/gen/c/json.v
Expand Up @@ -673,7 +673,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
is_option_field := field.typ.has_flag(.option)
if field.typ.has_flag(.option) {
gen_js_get_opt(js_dec_name(field_type), field_type, styp, tmp, name, mut
dec, true)
dec, is_required)
dec.writeln('\tif (jsonroot_${tmp} && !cJSON_IsNull(jsonroot_${tmp})) {')
} else {
gen_js_get(styp, tmp, name, mut dec, is_required)
Expand Down

0 comments on commit df3591c

Please sign in to comment.