Skip to content

Commit 1afc9e8

Browse files
committed
fix
1 parent e5a727c commit 1afc9e8

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

vlib/json/json_option_raw_test.v

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import json
2+
3+
pub struct Dto {
4+
pub:
5+
key string [raw]
6+
key2 string [raw]
7+
data ?string [raw]
8+
optional ?string [raw]
9+
}
10+
11+
fn test_main() {
12+
raw_json := '{
13+
"key": [1, 2, "test"],
14+
"key2": { "test": 1 },
15+
"data": { "test": 1 },
16+
"optional": "test"
17+
}'
18+
19+
dto := json.decode(Dto, raw_json)!
20+
21+
println(dto)
22+
}
23+
24+
fn test_none() {
25+
raw_json := '{
26+
"key": [1, 2, "test"],
27+
"optional": "test"
28+
}'
29+
30+
dto := json.decode(Dto, raw_json)!
31+
32+
println(dto)
33+
}
34+
35+
fn test_null() {
36+
raw_json := '{
37+
"key": [1, 2, "test"],
38+
"key2": null,
39+
"data": null,
40+
"optional": "test"
41+
}'
42+
43+
dto := json.decode(Dto, raw_json)!
44+
45+
println(dto)
46+
}
47+
48+
fn test_not_set() {
49+
raw_json := '{
50+
}'
51+
52+
dto := json.decode(Dto, raw_json)!
53+
54+
println(dto)
55+
}

vlib/v/gen/c/json.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
629629
if field.typ.has_flag(.option) {
630630
g.gen_json_for_type(field.typ)
631631
base_typ := g.base_type(field.typ)
632-
dec.writeln('\tif (!cJSON_IsString(js_get(root, "${name}")))')
632+
dec.writeln('\tif (js_get(root, "${name}") == NULL)')
633633
dec.writeln('\t\t_option_none(&(${base_typ}[]) { {0} }, &${prefix}${op}${c_name(field.name)}, sizeof(${base_typ}));')
634634
dec.writeln('\telse')
635635
dec.writeln('\t\t_option_ok(&(${base_typ}[]) { tos5(cJSON_PrintUnformatted(js_get(root, "${name}"))) }, &${prefix}${op}${c_name(field.name)}, sizeof(${base_typ}));')

0 commit comments

Comments
 (0)