Rework json layout

master
teknomunk 1 year ago
parent e8b0c2ccde
commit 730c340d66

@ -8,57 +8,33 @@
// Stdlib
#include <stdlib.h>
#include <stddef.h>
struct hash_index_entry
{
char* key;
int value;
int val;
};
extern struct json_field_type hash_index_entry_type;
#define OBJ_TYPE struct hash_index_entry
static struct json_object_field hash_index_entry_layout[] = {
{ "key", offsetof( struct hash_index_entry, key ), true, &json_field_string },
{ "val", offsetof( struct hash_index_entry, value ), true, &json_field_integer },
{ NULL, 0, true, NULL, &hash_index_entry_type },
JSON_FIELD_STRING( key, true ),
JSON_FIELD_INTEGER( val, true ),
//{ "key", offsetof( struct hash_index_entry, key ), true, &json_field_string },
//{ "val", offsetof( struct hash_index_entry, value ), true, &json_field_integer },
JSON_FIELD_END
};
#undef OBJ_TYPE
static void* alloc_hash_index_entry()
static void hash_index_entry_free( struct hash_index_entry* e )
{
void* ptr = malloc(sizeof(struct hash_index_entry));
memset( ptr, 0, sizeof(struct hash_index_entry));
return ptr;
}
static void free_hash_index_entry( void* ptr )
{
struct hash_index_entry* rie = ptr;
free(rie->key);
free(rie);
}
if( !e ) { return; }
static bool hie_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
{
void* data = *(void**)field_data = malloc(sizeof(struct hash_index_entry));
bool res = json_read_object_layout( jpp, hash_index_entry_layout, data );
if( !res ) {
free( data );
}
return res;
}
static bool hie_writer( struct json_writer* jw, const char* field_name, void* field_data, struct json_object_field* layout_field_data )
{
json_write_field_name(jw,field_name);
json_write_pretty_object_layout( jw, hash_index_entry_layout, *(void**)field_data );
return true;
free(e->key);
free(e);
}
struct json_field_type hash_index_entry_type = {
.reader = hie_reader,
.writer = hie_writer,
.size = sizeof(void*),
.layout = hash_index_entry_layout,
.alloc = alloc_hash_index_entry,
.free = free_hash_index_entry,
};
JSON_FIELD_TYPE_OBJECT_LAYOUT_WITH_DEFAULTS( hash_index_entry );
struct hash_index
{
@ -68,10 +44,14 @@ struct hash_index
} entries;
};
static struct json_object_field hash_index_layout[] = {
{ "entries", offsetof( struct hash_index, entries ), true, &json_field_array_of, &hash_index_entry_type },
{ NULL },
};
#define OBJ_TYPE struct hash_index
static struct json_object_field hash_index_layout[] = {
JSON_FIELD_ARRAY_OF_TYPE( entries, true, hash_index_entry_type ),
//{ "entries", offsetof( struct hash_index, entries ), true, &json_field_array_of, &hash_index_entry_type },
JSON_FIELD_END,
};
#undef OBJ_TYPE
bool hash_index_set( const char* index, const char* key, int value )
{
char hash[65] = "";
@ -87,17 +67,17 @@ bool hash_index_set( const char* index, const char* key, int value )
struct hash_index_entry new_entry = {
.key = (char*)key,
.value = value,
.val = value,
};
struct hash_index_entry* ptr = &new_entry;
// Overwrite existing entries
for( int i = 0; i < hi.entries.count; ++i ) {
struct hash_index_entry* entry = hi.entries.items[i];
//printf( "\tentry[%d] = { .key = %s, .value = %d }\n", i, entry->key, entry->value );
//printf( "\tentry[%d] = { .key = %s, .val = %d }\n", i, entry->key, entry->val );
if( 0 == strcmp( entry->key, key ) ) {
entry->value = value;
entry->val = value;
goto exists;
}
}
@ -131,7 +111,7 @@ bool hash_index_get( const char* index, const char* key, int* value )
/*
for( int i = 0; i < hi.entries.count; ++i ) {
struct hash_index_entry* entry = hi.entries.items[i];
printf( "\tentry[%d] = %p{ .uri = %s, .account_id = %d }\n", i, entry, entry->key, entry->value );
printf( "\tentry[%d] = %p{ .uri = %s, .account_id = %d }\n", i, entry, entry->key, entry->val );
}
//*/
@ -140,7 +120,7 @@ bool hash_index_get( const char* index, const char* key, int* value )
for( int i = 0; i < hi.entries.count; ++i ) {
struct hash_index_entry* entry = hi.entries.items[i];
if( 0 == strcmp(entry->key,key) ) {
*value = entry->value;
*value = entry->val;
found = true;
}
free(entry->key);

Loading…
Cancel
Save