|
|
|
@ -73,11 +73,11 @@ int count_required_fields( struct json_object_field* layout )
|
|
|
|
|
return count;
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
inline void debug_json_type_data( struct json_reflection* json_type_data, void* field_Data )
|
|
|
|
|
void debug_json_type_data( struct json_reflection* json_type_data, void* field_data, int offset )
|
|
|
|
|
{
|
|
|
|
|
DEBUG_printf( "layout_data = { .key = %s, .offset = %d, .required = %c, ... }, field_data=%p\n",
|
|
|
|
|
json_type_data->key,
|
|
|
|
|
json_type_data->offset,
|
|
|
|
|
offset,
|
|
|
|
|
json_type_data->required ? 'T' : 'F',
|
|
|
|
|
field_data
|
|
|
|
|
);
|
|
|
|
@ -87,22 +87,53 @@ static bool reader_deserialize( struct json_pull_parser* jpp, struct json_value*
|
|
|
|
|
{
|
|
|
|
|
if( jpp ) {
|
|
|
|
|
if( !json_type_data->type->reader ) {
|
|
|
|
|
printf( "! Missing `reader' field for type %s (%p)\n", json_type_data->type->type_string, json_type_data->type );
|
|
|
|
|
printf( "! Missing `reader' field for type %s (%p) used for key %s\n",
|
|
|
|
|
json_type_data->type->type_string, json_type_data->type,
|
|
|
|
|
json_type_data->key
|
|
|
|
|
);
|
|
|
|
|
__builtin_trap();
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
if( json_type_data->type->reader( jpp, field_data, json_type_data, offset ) ) {
|
|
|
|
|
DEBUG_printf( "read value for %s (extra)\n", key );
|
|
|
|
|
DEBUG_printf( "read value for %s (extra)\n", json_type_data->key );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else if( value ) {
|
|
|
|
|
if( !json_type_data->type->deserialize ) {
|
|
|
|
|
printf( "! Missing `deserialize' field for type %s (%p)\n", json_type_data->type->type_string, json_type_data->type );
|
|
|
|
|
__builtin_trap();
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
// Hack to make this work
|
|
|
|
|
// TODO: rework json_pull_parser to allow reading directly from value without
|
|
|
|
|
// needing to serialize to string
|
|
|
|
|
|
|
|
|
|
// Create dummy file with data
|
|
|
|
|
char* data = NULL;
|
|
|
|
|
size_t size = 0;
|
|
|
|
|
FILE* f = open_memstream(&data,&size);
|
|
|
|
|
struct json_writer jw = {
|
|
|
|
|
.f = f,
|
|
|
|
|
.indentation = "\t",
|
|
|
|
|
.indent = 0,
|
|
|
|
|
};
|
|
|
|
|
json_write_value( &jw, value );
|
|
|
|
|
fprintf( f, "\n" ); // TODO: this should not be required to parse correctly
|
|
|
|
|
fflush(f);
|
|
|
|
|
|
|
|
|
|
//printf( "Handling %s\n", json_type_data->key );
|
|
|
|
|
//printf( "data: %s\n", data );
|
|
|
|
|
|
|
|
|
|
// Create a new pull parser with string data from value
|
|
|
|
|
struct json_pull_parser* jpp2 = json_pull_parser_new( f );
|
|
|
|
|
|
|
|
|
|
// Read the value
|
|
|
|
|
bool result = reader_deserialize( jpp2, NULL, field_data, json_type_data, offset );
|
|
|
|
|
//printf( "result=%c\n\n", result ? 'T' : 'F' );
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
free(data);
|
|
|
|
|
json_pull_parser_release(jpp2);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
if( json_type_data->type->deserialize( *value, field_data, json_type_data, offset ) ) {
|
|
|
|
|
DEBUG_printf( "read value for %s (extra)\n", key );
|
|
|
|
|
DEBUG_printf( "read value for %s (extra)\n", json_type_data->key );
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -132,7 +163,7 @@ static bool unified_handle_layout_field( struct json_pull_parser* jpp, struct js
|
|
|
|
|
DEBUG_printf( "extra layout\n" );
|
|
|
|
|
|
|
|
|
|
json_type_data->key = key;
|
|
|
|
|
DEBUG_json_type_data( json_type_data, field_data );
|
|
|
|
|
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 );
|
|
|
|
@ -142,7 +173,7 @@ static bool unified_handle_layout_field( struct json_pull_parser* jpp, struct js
|
|
|
|
|
}
|
|
|
|
|
} else if( 0 == strcmp( key, json_type_data->key ) ) {
|
|
|
|
|
|
|
|
|
|
DEBUG_json_type_data( json_type_data, field_data )
|
|
|
|
|
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 );
|
|
|
|
@ -204,6 +235,7 @@ static bool read_layout_with_downcast( struct json_pull_parser* jpp, struct json
|
|
|
|
|
// ... buffer values until we get a successfull downcast ...
|
|
|
|
|
while(( key = json_pull_parser_read_object_key(jpp) )) {
|
|
|
|
|
struct json_value value;
|
|
|
|
|
memset(&value,0,sizeof(value));
|
|
|
|
|
|
|
|
|
|
json_pull_parser_read_value(jpp,&value);
|
|
|
|
|
json_value_object_set( &buffer, key, value );
|
|
|
|
@ -332,7 +364,7 @@ bool json_read_object_layout_from_file_ex( const char* filename, struct json_obj
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return json_read_object_layout_from_FILE_ex( f, layout, ptr, flags, NULL );
|
|
|
|
|
return json_read_object_layout_from_FILE_ex( f, layout, &ptr, flags, NULL );
|
|
|
|
|
}
|
|
|
|
|
bool json_read_object_layout_from_FILE( FILE* f, struct json_object_field* layout, void* ptr )
|
|
|
|
|
{
|
|
|
|
@ -368,7 +400,7 @@ void json_write_pretty_layout( struct json_writer* jw, struct json_layout* layou
|
|
|
|
|
struct json_reflection* json_type_data = LAYOUT_GET_REFLECT( layout, i );
|
|
|
|
|
|
|
|
|
|
void* field_data = &((char*)(data))[ offset ];
|
|
|
|
|
DEBUG_json_type_data( json_type_data, field_data );
|
|
|
|
|
DEBUG_json_type_data( json_type_data, field_data, offset );
|
|
|
|
|
|
|
|
|
|
if( !json_type_data->type->writer ) {
|
|
|
|
|
printf( "! Missing `writer' field for type %s (%p)\n", json_type_data->type->type_string, json_type_data->type );
|
|
|
|
|