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.
71 lines
1.8 KiB
C
71 lines
1.8 KiB
C
|
|
#include "json/json.h"
|
|
#include "json/layout.h"
|
|
|
|
#include "../activity.h"
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
bool ap_object_ptr_or_ref_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
|
|
{
|
|
struct ap_object_ptr_or_ref* o = field_data;
|
|
char* ref = json_pull_parser_read_string(jpp);
|
|
if( ref ) {
|
|
o->tag = apaot_ref;
|
|
o->ref = ref;
|
|
return true;
|
|
} else {
|
|
o->tag = apaot_activity;
|
|
o->ptr = malloc(sizeof(*o->ptr));
|
|
memset(o->ptr,0,sizeof(*o->ptr));
|
|
struct json_object_field f = {
|
|
.key = layout_field_data->key,
|
|
.offset = 0,
|
|
.required = true,
|
|
.type = &ap_object_type,
|
|
};
|
|
if( !ap_object_type.reader( jpp, &o->ptr, &f ) ) {
|
|
ap_object_free(o->ptr);
|
|
o->ptr = malloc(sizeof(*o->ptr));
|
|
memset(o->ptr,0,sizeof(*o->ptr));
|
|
o->ptr = NULL;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool ap_object_ptr_or_ref_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_object_field* layout_field_data )
|
|
{
|
|
struct ap_object_ptr_or_ref* o = field_data;
|
|
if( o->tag == apaot_ref ) {
|
|
if( o->ref ) {
|
|
json_write_field_name( jw, field_name );
|
|
json_write_string( jw->f, o->ref );
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} else if( o->tag == apaot_activity ) {
|
|
struct json_object_field f = {
|
|
.key = layout_field_data->key,
|
|
.offset = 0,
|
|
.required = true,
|
|
.type = &ap_object_type,
|
|
};
|
|
return ap_object_type.writer( jw, field_name, &o->ptr, &f );
|
|
}
|
|
return false;
|
|
}
|
|
void ap_object_ptr_or_ref_free_shim( void* ptr )
|
|
{
|
|
ap_object_ptr_or_ref_free(ptr);
|
|
}
|
|
struct json_field_type ap_object_ptr_or_ref_type = {
|
|
.reader = ap_object_ptr_or_ref_reader,
|
|
.writer = ap_object_ptr_or_ref_writer,
|
|
.size = sizeof(struct ap_object_ptr_or_ref),
|
|
.free = ap_object_ptr_or_ref_free_shim,
|
|
};
|