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

cgen,json: bugfix for json.decode; now [skip] fields are initialised

This commit is contained in:
Delyan Angelov 2022-01-22 19:25:53 +02:00
parent e66e35ced1
commit 9ebd56caa7
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 33 additions and 2 deletions

View File

@ -42,5 +42,30 @@ fn test_decode_u64() ? {
data := '{"size": 10737418240}'
m := json.decode(Mount, data) ?
assert m.size == 10737418240
println(m)
// println(m)
}
//
pub struct Comment {
pub mut:
id string
comment string
}
pub struct Task {
mut:
description string
id int
total_comments int
file_name string [skip]
comments []Comment [skip]
}
fn test_skip_fields_should_be_initialised_by_json_decode() ? {
data := '{"total_comments": 55, "id": 123}'
mut task := json.decode(Task, data) ?
assert task.id == 123
assert task.total_comments == 55
assert task.comments == []
}

View File

@ -46,9 +46,15 @@ fn (mut g Gen) gen_jsons() {
// Code gen decoder
dec_fn_name := js_dec_name(styp)
dec_fn_dec := 'Option_$styp ${dec_fn_name}(cJSON* root)'
init_styp := g.expr_string(ast.Expr(ast.StructInit{
typ: utyp
typ_str: styp
}))
dec.writeln('
$dec_fn_dec {
$styp res;
$styp res = $init_styp;
if (!root) {
const char *error_ptr = cJSON_GetErrorPtr();
if (error_ptr != NULL) {