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.

107 lines
2.3 KiB
C

#include "context.h"
#include "json/layout.h"
#include "json/json.h"
#include "collections/array.h"
#include "collections/item_type/string.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static bool context_reader( struct json_pull_parser* jpp, void* field_data, struct json_object_field* layout_field_data )
{
printf( "context_reader\n" );
struct ap_activity_context* ctx = field_data;
char* str = json_pull_parser_read_string(jpp);
if( str ) {
if( 0 != strcmp(str,"https://www.w3.org/ns/activitystreams") ) {
free(str);
return false;
}
free(str);
return true;
}
int save;
if( !json_pull_parser_begin_array(jpp,&save) ) {
return false;
}
struct collection c = {
.ptr = &ctx->extra,
.vtable = &array_vtable,
.itable = &string_itable,
};
while( !json_pull_parser_end_array( jpp, &save ) ) {
str = json_pull_parser_read_string(jpp);
if( str ) {
array_append_c( c, &str );
} else {
int save2;
if( !json_pull_parser_begin_object( jpp, &save2 ) ) {
return false;
}
char* key = json_pull_parser_read_object_key(jpp);
if( 0 != strcmp("@language",key) ) {
return false;
}
free(key);
char* lang = json_pull_parser_read_string(jpp);
if( !lang ) {
return false;
}
static const char* langs[] = {
"und",
};
ctx->language = clang_unknown;
for( int i = 0; i < 1; ++i ) {
if( 0 == strcmp(langs[i],lang) ) {
ctx->language = i+1;
}
}
if( ctx->language == clang_unknown ) {
printf( "unknown language: %s\n", lang );
return false;
}
free(lang);
if( !json_pull_parser_end_object(jpp,&save2) ) {
printf( "Object failed\n" );
return false;
}
}
}
return true;
}
static bool context_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 );
struct ap_activity_context* ctx = field_data;
int items = 1 + ctx->extra.count + (ctx->language ? 1 : 0 );
if( items == 1 ) {
json_write_string( jw->f, "https://www.w3.org/ns/activitystreams" );
} else {
printf( "TODO: context_writer, handle array\n" );
exit(1);
}
return true;
}
struct json_field_type ap_activity_context_type = {
.reader = context_reader,
.writer = context_writer,
.size = sizeof(struct ap_activity_context)
};