diff --git a/vlib/json/json_decode_option_enum_test.v b/vlib/json/json_decode_option_enum_test.v new file mode 100644 index 00000000000000..f7dda4cd9d5ac9 --- /dev/null +++ b/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 + } +} diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 27f9f4d98f4547..13d647c0704958 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -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)