1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

json: fix decode option string (#17812)

This commit is contained in:
Felipe Pena 2023-03-29 13:45:41 -03:00 committed by GitHub
parent dd0b68ac90
commit 75deb66fd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,46 @@
import json
pub struct MyStruct[T] {
pub mut:
result ?T
id string
}
fn test_gn_struct_string() ! {
a := MyStruct[string]{
result: 'test'
id: 'some id'
}
encoded_string := json.encode(a)
dump(encoded_string)
test := json.decode(MyStruct[string], encoded_string)!
dump(test)
assert a == test
}
fn test_gn_struct_int() ! {
a := MyStruct[int]{
result: 1
id: 'some id'
}
encoded_string := json.encode(a)
dump(encoded_string)
test := json.decode(MyStruct[int], encoded_string)!
dump(test)
assert a == test
}
fn test_gn_struct_f64() ! {
a := MyStruct[f64]{
result: 1.2
id: 'some id'
}
encoded_string := json.encode(a)
dump(encoded_string)
test := json.decode(MyStruct[f64], encoded_string)!
dump(test)
assert a == test
}

View File

@ -164,7 +164,8 @@ ${enc_fn_dec} {
g.gen_sumtype_enc_dec(utyp, sym, mut enc, mut dec, ret_styp)
} else if sym.kind == .enum_ {
g.gen_enum_enc_dec(utyp, sym, mut enc, mut dec)
} else if utyp.has_flag(.option) && sym.info !is ast.Struct {
} else if utyp.has_flag(.option)
&& (is_js_prim(g.typ(utyp.clear_flag(.option))) || sym.info !is ast.Struct) {
g.gen_option_enc_dec(utyp, mut enc, mut dec)
} else {
enc.writeln('\to = cJSON_CreateObject();')