You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.9 KiB
C

#include "../../json.h"
#include "../../layout.h"
//#define DEBUG
#ifdef DEBUG
#define DEBUG_printf printf
#define DEBUG_fflush fflush
#else
#define DEBUG_printf(...)
#define DEBUG_fflush(...)
#endif
bool json_field_object_type_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
{
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 );
return false;
}
*(void**)field_data = ptr;
return true;
}
bool json_field_object_type_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
{
void* obj_ptr = *(void**)field_data;
if( !layout_field_data->required && !obj_ptr ) { return false; }
json_write_field_name(jw,field_name);
struct json_field_type* ft = layout_field_data->type;
json_write_pretty_object_layout( jw, ft->layout, *(void**)field_data );
return true;
}
bool json_field_object_composite_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
{
struct json_field_type* type = layout_field_data->type;
return json_read_object_layout( jpp, type->layout, field_data );
}
bool json_field_object_composite_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
{
struct json_field_type* type = layout_field_data->type;
json_write_field_name(jw,field_name);
json_write_pretty_object_layout( jw, type->layout, field_data );
return true;
}
bool json_field_object_composite_reader2( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
{
struct json_object_field* layout = layout_field_data->composite_layout;
return json_read_object_layout( jpp, layout, field_data );
}
bool json_field_object_composite_writer2( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
{
struct json_object_field* layout = layout_field_data->composite_layout;
json_write_field_name(jw,field_name);
json_write_pretty_object_layout( jw, layout, field_data );
return true;
}
struct json_field_type json_field_object_composite = {
.reader = json_field_object_composite_reader2,
.writer = json_field_object_composite_writer2,
.size = sizeof(int),
};
bool json_field_fixed_null_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
{
return json_pull_parser_read_null( jpp );
}
bool json_field_fixed_null_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
{
json_write_field_name(jw,field_name);
fprintf( jw->f, "null" );
return true;
}
struct json_field_type json_field_fixed_null = {
.reader = json_field_fixed_null_reader,
.writer = json_field_fixed_null_writer,
.size = sizeof(int),
};
bool inline_layout_writer( struct json_writer* jw, const char* field_name, void* data, struct json_reflection* layout_field_data, int offset )
{
bool result = false;
DEBUG_printf( "begin inline_layout_writer()\n" );
struct json_object_field* layout = layout_field_data->sublayout;
for( int i = 0; layout[i].key; ++i ) {
void* field_data = &((char*)(data))[ layout[i].offset ];
DEBUG_printf( "layout[%d] = { .key = %s, .offset = %d, .required = %c, ... }, field_data=%p\n",
i,
layout[i].key,
layout[i].offset,
layout[i].required ? 'T' : 'F',
field_data
);
if( layout[i].type->writer( jw, layout[i].key, field_data, (struct json_reflection*)&layout[i].key, layout[i].offset ) ) {
jw->need_comma = true;
result = true;
}
}
DEBUG_printf( "end inline_layout_writer()\n" );
return result;
}
struct json_field_type json_field_inline_sublayout = {
.writer = inline_layout_writer,
};