basic/json_parser/parse_all_examples.c

See examples/basic/json-parser

/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
#include "zos.h"
/*************************************************************************************************
* Parse all the files in the 'json/' folder and display their contents
*/
zos_result_t parse_all_examples(void)
{
zos_result_t result;
zos_file_t *file_list;
const zos_file_parameters_t params =
{
.name = "json/*"
};
if(ZOS_FAILED(result, zn_file_list_with_parameters(&file_list, &params)))
{
ZOS_LOG("Failed to get file listing: %d", result);
}
else
{
for(const zos_file_t *file = file_list; file != NULL; file = file->next)
{
if(ZOS_FAILED(result, parse_file(file->name)))
{
break;
}
}
}
ZOS_LOG("Finished");
return result;
}
/*************************************************************************************************/
static zos_result_t parse_file(const char *filename)
{
zos_result_t result;
json_parse_context_t *context = NULL;
char buffer[128];
uint32_t handle = -1;
const json_parse_config_t config =
{
.buffer = buffer,
.buffer_len = sizeof(buffer),
.reader = file_reader
};
ZOS_LOG("\r\nParsing: %s", filename);
if(ZOS_FAILED(result, zn_file_open(filename, &handle)))
{
ZOS_LOG("Failed to open: %s", filename);
}
else if(ZOS_FAILED(result, json_parse_context_init(&context, &config)))
{
ZOS_LOG("Failed to initialize json parsing context");
}
else if(ZOS_FAILED(result, json_parse_chunked(context, (void*)handle)))
{
ZOS_LOG("Failed to parse json file");
}
else
{
int tok_stack[10];
int level = 0;
memset(tok_stack, 0, sizeof(tok_stack));
ZOS_LOG("JSON file contents:");
for(const json_tok_t *tok = json_context_get_token(context, NULL, NULL); tok != NULL; tok = tok->next)
{
level = print_token(tok, level, tok_stack);
}
}
zn_file_close(handle);
return result;
}
/*************************************************************************************************/
static int print_token(const json_tok_t *tok, int level, int *tok_stack)
{
char print_buffer[128];
char *print_ptr = print_buffer;
if(level > 0)
{
if(tok_stack[level-1] >= 0)
{
if(tok_stack[level-1] == 0)
{
--level;
}
for(int l = level; l > 0; --l)
{
*print_ptr++ = ' ';
*print_ptr++ = ' ';
*print_ptr++ = ' ';
}
--tok_stack[level-1];
}
}
switch(tok->type)
{
sprintf(print_ptr, "Object: %u", (unsigned int)tok->data.uint32);
tok_stack[level++] = tok->data.uint32;
break;
sprintf(print_ptr, "Array: %u", (unsigned int)tok->data.uint32);
tok_stack[level++] = tok->data.uint32;
break;
sprintf(print_ptr, "String: %s", tok->data.str);
break;
sprintf(print_ptr, "Boolean: %s", tok->data.boolean ? "true" : "false");
break;
sprintf(print_ptr, "Integer: %u", (unsigned int)tok->data.uint32);
break;
{
char str[16];
sprintf(print_ptr, "Integer64: %s", uint64_to_str(&tok->data.uint64, str));
} break;
sprintf(print_ptr, "Float: %s", tok->data.str);
break;
sprintf(print_ptr, "NULL element");
break;
default:
sprintf(print_ptr, "Unknown type: %d", (int)tok->type);
break;
}
ZOS_LOG("%s", print_buffer);
return level;
}
/*************************************************************************************************
* The parser will call this until it returns an error or
* the 'bytes_read' parameter is return with a 0 value.
*/
static zos_result_t file_reader(void *user, void *data, uint32_t max_length, uint32_t *bytes_read)
{
uint32_t file_handle = (uint32_t)user;
uint32_t has_more_data;
zn_file_poll(file_handle, &has_more_data);
if(has_more_data == 0)
{
// no more data so set this to 0 to tell the parser to stop calling this reader
*bytes_read = 0;
}
else if(ZOS_FAILED(result, zn_file_read(file_handle, data, max_length, bytes_read)))
{
}
return result;
}