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.

91 lines
2.0 KiB
C

#include "react.h"
#include "json/json.h"
#include "json/layout.h"
#include "collections/array.h"
#include <stdlib.h>
#include <string.h>
static bool reader( struct json_pull_parser* jpp, void* field_data, struct json_reflection* layout_field_data, int offset )
{
int save;
if( !json_pull_parser_begin_object( jpp, &save ) ) {
return false;
}
struct status_react* sr = *(void**)field_data = malloc(sizeof(struct status_react));
memset(sr,0,sizeof(*sr));
char* key = json_pull_parser_read_object_key(jpp);
if( !key ) { return false; }
sr->code = key;
int save2;
if( !json_pull_parser_begin_array(jpp,&save2)) { return false; }
while( !json_pull_parser_end_array(jpp,&save2)) {
int account_id;
if( !json_pull_parser_read_int( jpp, &account_id ) ) {
printf( "! Failed to read int\n" );
return false;
}
array_append( &sr->accounts, sizeof(int), &account_id );
}
if( !json_pull_parser_end_object( jpp, &save ) ) {
printf( "! Failed to end object\n" );
return false;
}
return true;
}
static bool writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_reflection* layout_field_data, int offset )
{
struct status_react* sr = *(void**)field_data;
FILE* f = jw->f;
fprintf( f, "{" );
json_write_string( f, sr->code );
fprintf( f, ":[" );
for( int i = 0; i < sr->accounts.count; ++i ) {
fprintf(f, (i!=0)?",%d":"%d", sr->accounts.items[i] );
}
fprintf( f, "]}" );
return true;
}
static void* alloc()
{
struct status_react* react;
react = malloc(sizeof(*react));
memset(react,0,sizeof(*react));
printf( "status::react::alloc = %p\n", react );
return react;
}
void status_react_free( struct status_react* re )
{
if( !re ) { return; }
free(re->accounts.items);
free(re->code);
free(re);
}
static void free_react( void* ptr )
{
status_react_free(ptr);
}
struct json_field_type status_react_type = {
.reader = reader,
.writer = writer,
.size = sizeof(struct status_react*),
.alloc = alloc,
.free = free_react,
};