diff --git a/vlib/json/json_decode_test.v b/vlib/json/json_decode_test.v index 5b21838835..8d52fc2b5c 100644 --- a/vlib/json/json_decode_test.v +++ b/vlib/json/json_decode_test.v @@ -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 == [] } diff --git a/vlib/v/gen/c/json.v b/vlib/v/gen/c/json.v index f635db3fb6..626caff2c7 100644 --- a/vlib/v/gen/c/json.v +++ b/vlib/v/gen/c/json.v @@ -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) {