1
0
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:
Felipe Pena 2023-04-22 04:58:01 -03:00 committed by GitHub
parent c43ea09d87
commit 89b7bebc3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View 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) == '{}'
}

View File

@ -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
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${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}')
}
}