mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
json: fix json decode with sumtype of multi array type (#14035)
This commit is contained in:
parent
c4dff0d797
commit
2d6d6c9ac9
@ -1,13 +1,21 @@
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
type Test = []string | string
|
type Test = []bool | []int | []string | string
|
||||||
|
|
||||||
struct Some {
|
struct Some {
|
||||||
t Test
|
t Test
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_json_decode_with_sumtype() ? {
|
fn test_json_decode_with_sumtype() ? {
|
||||||
v := json.decode(Some, '{"t": ["string", "string2"]}') ?
|
v1 := json.decode(Some, '{"t": ["string", "string2"]}') ?
|
||||||
println(v)
|
println(v1)
|
||||||
assert '$v.t' == "Test(['string', 'string2'])"
|
assert v1.t == Test(['string', 'string2'])
|
||||||
|
|
||||||
|
v2 := json.decode(Some, '{"t": [11, 22]}') ?
|
||||||
|
println(v2)
|
||||||
|
assert v2.t == Test([11, 22])
|
||||||
|
|
||||||
|
v3 := json.decode(Some, '{"t": [true, false]}') ?
|
||||||
|
println(v3)
|
||||||
|
assert v3.t == Test([true, false])
|
||||||
}
|
}
|
||||||
|
@ -311,14 +311,21 @@ fn (mut g Gen) gen_sumtype_enc_dec(sym ast.TypeSymbol, mut enc strings.Builder,
|
|||||||
dec.writeln('\t\t}')
|
dec.writeln('\t\t}')
|
||||||
}
|
}
|
||||||
|
|
||||||
if var_t == 'Array_string' {
|
if var_t.starts_with('Array_') {
|
||||||
tmp := g.new_tmp_var()
|
tmp := g.new_tmp_var()
|
||||||
dec.writeln('\t\tif (cJSON_IsArray(root)) {')
|
judge_elem_typ := if var_t.ends_with('string') {
|
||||||
dec.writeln('\t\t\tOption_Array_string $tmp = ${js_dec_name(var_t)}(root);')
|
'cJSON_IsString(root->child)'
|
||||||
|
} else if var_t.ends_with('bool') {
|
||||||
|
'cJSON_IsBool(root->child)'
|
||||||
|
} else {
|
||||||
|
'cJSON_IsNumber(root->child)'
|
||||||
|
}
|
||||||
|
dec.writeln('\t\tif (cJSON_IsArray(root) && $judge_elem_typ) {')
|
||||||
|
dec.writeln('\t\t\tOption_$var_t $tmp = ${js_dec_name(var_t)}(root);')
|
||||||
dec.writeln('\t\t\tif (${tmp}.state != 0) {')
|
dec.writeln('\t\t\tif (${tmp}.state != 0) {')
|
||||||
dec.writeln('\t\t\t\treturn (Option_$sym.cname){ .state = ${tmp}.state, .err = ${tmp}.err, .data = {0} };')
|
dec.writeln('\t\t\t\treturn (Option_$sym.cname){ .state = ${tmp}.state, .err = ${tmp}.err, .data = {0} };')
|
||||||
dec.writeln('\t\t\t}')
|
dec.writeln('\t\t\t}')
|
||||||
dec.writeln('\t\t\tres = Array_string_to_sumtype_${sym.cname}(($var_t*)${tmp}.data);')
|
dec.writeln('\t\t\tres = ${var_t}_to_sumtype_${sym.cname}(($var_t*)${tmp}.data);')
|
||||||
dec.writeln('\t\t}')
|
dec.writeln('\t\t}')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user