basic/json_parser/parse_example2.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"
/*************************************************************************************************
* This demonstrates having the entire JSON string pre-read into a buffer.
* The json buffer is then parsed and the tokens are looked up by their string value.
*
*
* The parsed JSON looks like:
*
* {
* "colorsArray":[{
* "colorName":"red",
* "hexValue":"#f00"
* },
* {
* "colorName":"green",
* "hexValue":"#0f0"
* }
* ]}
*
*
*/
zos_result_t parse_example2(const char *filename)
{
zos_result_t result;
json_parse_context_t *context = NULL;
char *json_data = NULL;
ZOS_LOG("\r\n\r\nParsing: %s", filename);
if(ZOS_FAILED(result, read_file(filename, &json_data)))
{
ZOS_LOG("Failed to read: %s", filename);
return result;
}
const json_parse_config_t config =
{
.buffer = json_data,
.buffer_len = strlen(json_data)
};
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, NULL)))
{
ZOS_LOG("Failed to parse json file");
}
else
{
// get the array token
const json_tok_t *color_array = json_context_get_value(context, "colorsArray", NULL);
if(color_array == NULL || color_array->type != JSON_TYPE_ARRAY)
{
ZOS_LOG("Malformed JSON!");
goto cleanup;
}
ZOS_LOG("Color names/values: (%d)", color_array->data.uint32);
const json_tok_t *obj_tok = color_array->next; // the first object in the array
for(int color_count = color_array->data.uint32; color_count > 0 && obj_tok != NULL; --color_count)
{
const json_tok_t *name_tok = json_context_get_value(context, "colorName", obj_tok);
const json_tok_t *value_tok = json_context_get_value(context, "hexValue", obj_tok);
if(name_tok == NULL || value_tok == NULL)
{
ZOS_LOG("Malformed JSON!");
result = ZOS_ERROR;
break;
}
ZOS_LOG("%s : %s", name_tok->data.str, value_tok->data.str);
// just skip to a token in the current object
// the json_context_get_value() automatically searches for the next
// object starting at this token
obj_tok = name_tok->next;
}
}
cleanup:
if(json_data != NULL)
{
zn_free(json_data);
}
ZOS_LOG("Finished");
return result;
}
/*************************************************************************************************
* Pre-read the JSON file into a buffer
*/
static zos_result_t read_file(const char *filename, char **buffer)
{
zos_result_t result;
uint32_t handle;
uint8_t *ptr;
if(ZOS_FAILED(result, zn_file_open(filename, &handle)))
{
}
else if(ZOS_FAILED(result, zn_file_get_descriptor(handle, &fd)))
{
}
else if(ZOS_FAILED(result, zn_malloc(&ptr, fd.file_size+1)))
{
}
else if(ZOS_FAILED(result, zn_file_read(handle, ptr, fd.file_size, NULL)))
{
}
else
{
ptr[fd.file_size] = 0; // null-terminate string
*buffer = (char*)ptr;
}
return result;
}