mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
json: fix option sumtype with int types (#18013)
This commit is contained in:
parent
c43ea09d87
commit
89b7bebc3c
13
vlib/json/json_encode_sumtype_test.v
Normal file
13
vlib/json/json_encode_sumtype_test.v
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
struct Test {
|
||||||
|
optional_sumtype ?MySumtype
|
||||||
|
}
|
||||||
|
|
||||||
|
type MySumtype = int | string
|
||||||
|
|
||||||
|
fn test_simple() {
|
||||||
|
test := Test{}
|
||||||
|
encoded := json.encode(test)
|
||||||
|
assert dump(encoded) == '{}'
|
||||||
|
}
|
@ -501,7 +501,11 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
|
|||||||
last_number_type = var_t
|
last_number_type = var_t
|
||||||
dec.writeln('\t\tif (cJSON_IsNumber(root)) {')
|
dec.writeln('\t\tif (cJSON_IsNumber(root)) {')
|
||||||
dec.writeln('\t\t\t${var_t} value = ${js_dec_name(var_t)}(root);')
|
dec.writeln('\t\t\t${var_t} value = ${js_dec_name(var_t)}(root);')
|
||||||
dec.writeln('\t\t\t${prefix}res = ${var_t}_to_sumtype_${sym.cname}(&value);')
|
if utyp.has_flag(.option) {
|
||||||
|
dec.writeln('\t\t\t_option_ok(&(${sym.cname}[]){ ${var_t}_to_sumtype_${sym.cname}(&value) }, &${prefix}res, sizeof(${sym.cname}));')
|
||||||
|
} else {
|
||||||
|
dec.writeln('\t\t\t${prefix}res = ${var_t}_to_sumtype_${sym.cname}(&value);')
|
||||||
|
}
|
||||||
dec.writeln('\t\t}')
|
dec.writeln('\t\t}')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user