diff --git a/vlib/json/json_decode_with_sumtype_test.v b/vlib/json/json_decode_with_sumtype_test.v new file mode 100644 index 0000000000..5fbc81573f --- /dev/null +++ b/vlib/json/json_decode_with_sumtype_test.v @@ -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'])" +} diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index 5f909d6c75..d939fdc595 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -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 {