diff --git a/layout.c b/layout.c index 96af3f3..54a15a1 100644 --- a/layout.c +++ b/layout.c @@ -208,7 +208,7 @@ static bool unified_handle_layout_field( struct json_pull_parser* jpp, struct js DEBUG_json_type_data( json_type_data, field_data, offset ); if( reader_deserialize( jpp, value, field_data, json_type_data, offset ) ) { - DEBUG_printf( "read value for %s\n", key ); + DEBUG_printf( "read value for %s, *has_field=%p\n", key, *has_field ); **has_field = true; return true; } @@ -366,8 +366,13 @@ static bool read_layout_with_downcast( struct json_pull_parser* jpp, struct json for( int i = 0; i < required_field_count; ++i ) { if( !has_field[i] ) { int offset = i; - printf( "missing field[%d] '%s'\n", i, get_required_field_name(layout,&offset) ); + printf( "missing field[%d] '%s' &has_field[]=%p\n", i, get_required_field_name(layout,&offset), &has_field[i] ); return false; + } else { + #ifdef DEBUG + int offset = i; + #endif + DEBUG_printf( "present field[%d] '%s' &has_field[]=%p\n", i, get_required_field_name(layout,&offset), &has_field[i] ); } } diff --git a/layout.h b/layout.h index d3b52b4..a86f849 100644 --- a/layout.h +++ b/layout.h @@ -60,6 +60,11 @@ extern struct json_field_type json_field_bool; extern struct json_field_type json_field_bool_or_null; extern struct json_field_type json_field_fixed_bool; extern struct json_field_type json_field_bool_callback; +struct rich_bool { + unsigned visible : 1; + unsigned value : 1; +}; +extern struct json_field_type json_field_rich_bool; // layout/string.c extern struct json_field_type json_field_string; @@ -310,6 +315,7 @@ struct json_field_type name ## _type = { \ .layout = name ## _layout, \ .alloc = name ## _alloc_shim, \ .free = name ## _free_shim, \ + .type_string = #name, \ } diff --git a/layout/types/bool.c b/layout/types/bool.c index d0c6d25..61f7ac8 100644 --- a/layout/types/bool.c +++ b/layout/types/bool.c @@ -7,11 +7,26 @@ static bool json_field_bool_reader( struct json_pull_parser* jpp, void* field_da { return json_pull_parser_read_bool( jpp, (bool*)field_data ); } -static bool json_field_fixed_bool_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_field_data, int offset ) +static bool json_field_bool_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* json_field_data, int offset ) { - bool val = json_pull_parser_read_bool( jpp, (bool*)field_data ); - return val == json_field_data->fixed_bool; + bool value = *(bool*)(field_data); + if( !json_field_data->always_write ) { + if( !json_field_data->required && !value ) { + return false; + } + } + + json_write_field_name( jw, field_name ); + fprintf( jw->f, value ? "true" : "false" ); + return true; } +struct json_field_type json_field_bool = { + .reader = json_field_bool_reader, + .writer = json_field_bool_writer, + .size = sizeof(bool), +}; + + static bool json_field_bool_or_null_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_field_data, int offset ) { *(bool*)field_data = false; @@ -31,43 +46,34 @@ static bool json_field_bool_or_null_reader( struct json_pull_parser* jpp, void* return true; } -static bool json_field_fixed_bool_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* json_field_data, int offset ) -{ - bool value = json_field_data->fixed_bool; +struct json_field_type json_field_bool_or_null = { + .reader = json_field_bool_or_null_reader, + .writer = json_field_bool_writer, + .size = sizeof(bool), + .type_string = "bool_or_null", +}; - json_write_field_name( jw, field_name ); - fprintf( jw->f, value ? "true" : "false" ); - return true; + +static bool json_field_fixed_bool_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_field_data, int offset ) +{ + bool val = json_pull_parser_read_bool( jpp, (bool*)field_data ); + return val == json_field_data->fixed_bool; } -static bool json_field_bool_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* json_field_data, int offset ) +static bool json_field_fixed_bool_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* json_field_data, int offset ) { - bool value = *(bool*)(field_data); - if( !json_field_data->always_write ) { - if( !json_field_data->required && !value ) { - return false; - } - } + bool value = json_field_data->fixed_bool; json_write_field_name( jw, field_name ); fprintf( jw->f, value ? "true" : "false" ); return true; } -struct json_field_type json_field_bool = { - .reader = json_field_bool_reader, - .writer = json_field_bool_writer, - .size = sizeof(bool), -}; struct json_field_type json_field_fixed_bool = { .reader = json_field_fixed_bool_reader, .writer = json_field_fixed_bool_writer, .size = sizeof(bool), }; -struct json_field_type json_field_bool_or_null = { - .reader = json_field_bool_or_null_reader, - .writer = json_field_bool_writer, - .size = sizeof(bool), - .type_string = "bool_or_null", -}; + + static bool json_field_bool_callback_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_field_data, int offset ) { @@ -96,3 +102,32 @@ struct json_field_type json_field_bool_callback = { .type_string = "bool", }; + +static bool json_field_rich_bool_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_field_data, int offset ) +{ + struct rich_bool* rb = field_data; + bool value; + bool result = json_pull_parser_read_bool( jpp, &value ); + + rb->value = value; + rb->visible = result; + + return result; +} +static bool json_field_rich_bool_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* json_field_data, int offset ) +{ + struct rich_bool* rb = field_data; + + if( !rb->visible ) { + return false; + } + + json_write_field_name( jw, field_name ); + fprintf( jw->f, rb->value ? "true" : "false" ); + return true; +} +struct json_field_type json_field_rich_bool = { + .reader = json_field_rich_bool_reader, + .writer = json_field_rich_bool_writer, + .size = sizeof(bool), +}; diff --git a/layout/types/object.c b/layout/types/object.c index bc4ce9c..ccc1537 100644 --- a/layout/types/object.c +++ b/layout/types/object.c @@ -16,6 +16,7 @@ bool json_field_object_type_reader( struct json_pull_parser* jpp, void* field_da struct json_field_type* ft = layout_field_data->type; void* ptr = ft->alloc(); + DEBUG_printf( "Parsing field of type %s\n", ft->type_string ); if( !json_read_object_layout( jpp, ft->layout, ptr ) ) { ft->free( ptr ); diff --git a/layout/types/string.c b/layout/types/string.c index b40e332..bb8ed5f 100644 --- a/layout/types/string.c +++ b/layout/types/string.c @@ -8,6 +8,9 @@ static bool json_field_string_reader( struct json_pull_parser* jpp, void* field_ { free( *(char**)field_data ); char* res = *(char**)field_data = json_pull_parser_read_string(jpp); + + //printf( "string_reader, res=%s\n", res ); + if( !res && !json_pull_parser_read_null(jpp) ) { return false; }