Implement more deserializers, implement fallback json_value->string->json_pull_parser deserializer

master
teknomunk 1 year ago
parent f259764f9d
commit c29aaf9985

@ -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 );

@ -28,6 +28,8 @@ static bool array_reader( struct json_pull_parser* jpp, void* field_data, struct
return true;
}
}
printf( "Parse failed, not an array\n" );
return false;
}
@ -40,7 +42,10 @@ static bool array_reader( struct json_pull_parser* jpp, void* field_data, struct
memset(item,0,sizeof(item));
}
if( !json_pull_parser_end_array( jpp, &save ) ) { return false; }
if( !json_pull_parser_end_array( jpp, &save ) ) {
printf( "Parse failed, array not ended\n" );
return false;
}
return true;
}

@ -16,28 +16,43 @@
#define DEBUG_fflush(...)
#endif
static bool extra_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
static bool extra_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_type_data, int offset )
{
printf( "extra_reader key=%s\n", layout_field_data->key );
struct json_value* extra = field_data;
struct json_object_pair pair = {
.key = strdup(layout_field_data->key),
.value = {
.type = jvt_null
},
};
if( !json_pull_parser_read_value( jpp, &pair.value ) ) {
printf( "extra_reader key=%s\n", json_type_data->key );
struct json_value value;
memset(&value,0,sizeof(value));
if( !json_pull_parser_read_value( jpp, &value ) ) {
return false;
}
// Force extra to jvt_object
if( extra->type != jvt_object ) {
json_value_free_composite(extra);
memset(extra,0,sizeof(*extra));
}
extra->type = jvt_object;
// Set key
json_value_object_set( extra, json_type_data->key, value );
return true;
}
bool extra_deserialize( struct json_value value, void* field_data, struct json_reflection* json_type_data, int offset )
{
struct json_value* extra = field_data;
printf( "extra_reader key=%s\n", json_type_data->key );
// Force extra to jvt_object
if( extra->type != jvt_object ) {
json_value_free_composite(extra);
memset(extra,0,sizeof(*extra));
}
extra->type = jvt_object;
array_append( &extra->o, sizeof(pair), &pair );
json_value_object_set( extra, json_type_data->key, value );
return true;
}

@ -6,6 +6,14 @@ static bool json_field_integer_reader( struct json_pull_parser* jpp, void* field
{
return json_pull_parser_read_int( jpp, (int*) field_data );
}
static bool json_field_integer_deserialize( struct json_value value, void* field_data, struct json_reflection* json_type_data, int offset )
{
if( value.type == jvt_number ) {
*(int*)field_data = value.n;
return true;
}
return false;
}
static bool json_field_integer_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
{
// Allow omitting field if not required and value is zero
@ -20,6 +28,7 @@ struct json_field_type json_field_integer = {
.type_string = "integer",
.reader = json_field_integer_reader,
.writer = json_field_integer_writer,
.deserialize = json_field_integer_deserialize,
.size = sizeof(int),
};

Loading…
Cancel
Save