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

json: fix error for json decode with sumtype (#14025)

This commit is contained in:
yuyi 2022-04-13 23:06:28 +08:00 committed by GitHub
parent e5809363de
commit dc08105022
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,13 @@
import json
type Test = []string | string
struct Some {
t Test
}
fn test_json_decode_with_sumtype() ? {
v := json.decode(Some, '{"t": ["string", "string2"]}') ?
println(v)
assert '$v.t' == "Test(['string', 'string2'])"
}

View File

@ -311,6 +311,17 @@ fn (mut g Gen) gen_sumtype_enc_dec(sym ast.TypeSymbol, mut enc strings.Builder,
dec.writeln('\t\t}')
}
if var_t == 'Array_string' {
tmp := g.new_tmp_var()
dec.writeln('\t\tif (cJSON_IsArray(root)) {')
dec.writeln('\t\t\tOption_Array_string $tmp = ${js_dec_name(var_t)}(root);')
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}')
dec.writeln('\t\t\tres = Array_string_to_sumtype_${sym.cname}(($var_t*)${tmp}.data);')
dec.writeln('\t\t}')
}
if var_t in ['i64', 'int', 'i8', 'u64', 'u32', 'u16', 'byte', 'u8', 'rune', 'f64',
'f32'] {
if number_is_met {