Handle optional fields in generated JSON parser
@@ -408,6 +408,7 @@ static int gen_field_match_handler(fb_output_t *out, fb_compound_type_t *ct, voi
int is_vector = 0;
int is_offset = 0;
int is_scalar = 0;
+ int is_optional = 0;
int is_table = 0;
int is_struct = 0;
int is_union = 0;
@@ -426,6 +427,7 @@ static int gen_field_match_handler(fb_output_t *out, fb_compound_type_t *ct, voi
fb_copy_scope(ct->scope, scope_name);
is_struct_container = ct->symbol.kind == fb_is_struct;
+ is_optional = !!(member->flags & fb_fm_optional);
switch (member->type.type) {
case vt_vector_type:
@@ -599,7 +601,7 @@ repeat_nested:
println(out, "buf = flatcc_json_parser_symbolic_%s(ctx, (mark = buf), end, symbolic_parsers, &val);", tname_prefix);
println(out, "if (buf == mark || buf == end) goto failed;");
unindent(); println(out, "}");
- if (!is_struct_container && !is_vector && !is_base64 && !is_base64url) {
+ if (!is_struct_container && !is_vector && !is_base64 && !is_base64url && !is_optional) {
#if !FLATCC_JSON_PARSE_FORCE_DEFAULTS
/* We need to create a check for the default value and create a table field if not the default. */
switch (member->value.type) {
@@ -650,7 +652,7 @@ repeat_nested:
} else {
println(out, "%s%s_write_to_pe(pval, val);", out->nsc, tname_prefix);
}
- if (!is_struct_container && !is_vector) {
+ if (!is_struct_container && !is_vector && !(is_scalar && is_optional)) {
unindent(); println(out, "}");
}
} else if (is_struct) {
