Skip to content

Commit df3591c

Browse files
authored
json: fix struct with option enum field (fix #20597) #20597
1 parent bf8b7f0 commit df3591c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import json
2+
3+
enum Lang {
4+
en = 1
5+
}
6+
7+
struct Request {
8+
lang ?Lang // ?string, ?int are ok
9+
}
10+
11+
fn test_main() {
12+
assert dump(json.decode(Request, '{}')!) == Request{
13+
lang: ?Lang(none)
14+
}
15+
assert dump(json.decode(Request, '{"lang": "en"}')!) == Request{
16+
lang: .en
17+
}
18+
}

vlib/v/gen/c/json.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
673673
is_option_field := field.typ.has_flag(.option)
674674
if field.typ.has_flag(.option) {
675675
gen_js_get_opt(js_dec_name(field_type), field_type, styp, tmp, name, mut
676-
dec, true)
676+
dec, is_required)
677677
dec.writeln('\tif (jsonroot_${tmp} && !cJSON_IsNull(jsonroot_${tmp})) {')
678678
} else {
679679
gen_js_get(styp, tmp, name, mut dec, is_required)

0 commit comments

Comments
 (0)