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.
119 lines
3.1 KiB
C
119 lines
3.1 KiB
C
#include "../../json.h"
|
|
#include "../../layout.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
static bool json_field_string_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
|
|
{
|
|
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;
|
|
}
|
|
|
|
if( layout_field_data ) {
|
|
if( !res && !layout_field_data->required ) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return !!res;
|
|
}
|
|
static bool json_field_string_deserialize( struct json_value value, void* field_data, struct json_reflection* layout_field_data, int offset )
|
|
{
|
|
free( *(char**)field_data );
|
|
|
|
if( value.type == jvt_string ) {
|
|
*(char**)field_data = strdup(value.s);
|
|
return true;
|
|
}
|
|
|
|
if( layout_field_data && !layout_field_data->required ) {
|
|
if( value.type == jvt_null ) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
static bool json_field_string_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
|
|
{
|
|
const char* str = *(char**)field_data;
|
|
|
|
if( !str ) {
|
|
if( layout_field_data->required ) {
|
|
json_write_field_name( jw, field_name );
|
|
fprintf( jw->f, "\"\"" );
|
|
return true;
|
|
}
|
|
} else {
|
|
json_write_field_name( jw, field_name );
|
|
json_write_string( jw->f, str );
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
struct json_field_type json_field_string = {
|
|
.reader = json_field_string_reader,
|
|
.writer = json_field_string_writer,
|
|
.deserialize = json_field_string_deserialize,
|
|
.size = sizeof(char*),
|
|
};
|
|
|
|
|
|
|
|
static bool json_field_fixed_string_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
|
|
{
|
|
char* res = json_pull_parser_read_string(jpp);
|
|
free(res);
|
|
return true;
|
|
}
|
|
static bool json_field_fixed_string_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 );
|
|
json_write_string( jw->f, layout_field_data->fixed_string );
|
|
|
|
return true;
|
|
}
|
|
|
|
struct json_field_type json_field_fixed_string = {
|
|
.reader = json_field_fixed_string_reader,
|
|
.writer = json_field_fixed_string_writer,
|
|
.size = sizeof(char*),
|
|
};
|
|
|
|
static bool json_field_callback_string_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
|
|
{
|
|
char* res = json_pull_parser_read_string(jpp);
|
|
layout_field_data->string_callback( field_data, true, &res );
|
|
free(res);
|
|
return true;
|
|
}
|
|
static bool json_field_callback_string_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
|
|
{
|
|
|
|
char* res = NULL;
|
|
if( !layout_field_data->string_callback( field_data, false, &res ) ) {
|
|
free(res);
|
|
return false;
|
|
}
|
|
|
|
json_write_field_name( jw, field_name );
|
|
json_write_string( jw->f, res );
|
|
free(res);
|
|
return true;
|
|
}
|
|
|
|
struct json_field_type json_field_callback_string = {
|
|
.reader = json_field_callback_string_reader,
|
|
.writer = json_field_callback_string_writer,
|
|
.size = sizeof(char*),
|
|
};
|
|
|