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.

134 lines
3.9 KiB
C

#include "../../json.h"
#include "../../layout.h"
#include <stdbool.h>
static bool json_field_bool_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_field_data, int offset )
{
return json_pull_parser_read_bool( jpp, (bool*)field_data );
}
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 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;
bool res = json_pull_parser_read_bool( jpp, (bool*)field_data )
|| json_pull_parser_read_null( jpp );
printf( "res = %s\n", res ? "T" : "F" );
if( json_field_data->required && !res ) {
return false;
}
if( !res ) {
// If this field is not required and no value was read, discard the value
json_pull_parser_read_value( jpp, NULL );
}
return true;
}
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_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_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;
json_write_field_name( jw, field_name );
fprintf( jw->f, value ? "true" : "false" );
return true;
}
struct json_field_type json_field_fixed_bool = {
.reader = json_field_fixed_bool_reader,
.writer = json_field_fixed_bool_writer,
.size = sizeof(bool),
};
static bool json_field_bool_callback_reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* json_field_data, int offset )
{
bool res;
if( !json_pull_parser_read_bool(jpp,&res) ) {
return false;
}
return json_field_data->bool_callback( field_data, true, &res );
}
static bool json_field_bool_callback_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* json_field_data, int offset )
{
bool res;
if( !json_field_data->bool_callback( field_data, false, &res ) ) {
return false;
}
json_write_field_name( jw, field_name );
fprintf( jw->f, res ? "true" : " false" );
return true;
}
struct json_field_type json_field_bool_callback = {
.writer = json_field_bool_callback_writer,
.reader = json_field_bool_callback_reader,
.size = sizeof(bool),
.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),
};