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 result;
json_parse_context_t *context = NULL;
char *json_data = NULL;
{
ZOS_LOG("Failed to read: %s", filename);
return result;
}
{
.buffer_len = strlen(json_data)
};
{
ZOS_LOG("Failed to initialize json parsing context");
}
{
ZOS_LOG("Failed to parse json file");
}
else
{
// get the array token
{
ZOS_LOG("Malformed JSON!");
goto cleanup;
}
{
if(name_tok == NULL || value_tok == NULL)
{
ZOS_LOG("Malformed JSON!");
result = ZOS_ERROR;
break;
}
// 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:
json_parse_context_deinit(context);
if(json_data != NULL)
{
zn_free(json_data);
}
ZOS_LOG("Finished");
return result;
}
/*************************************************************************************************
* Pre-read the JSON file into a buffer
*/
{
zos_result_t result;
uint32_t handle;
uint8_t *ptr;
{
}
{
}
{
}
{
}
else
{
ptr[fd.file_size] = 0; // null-terminate string
*buffer = (char*)ptr;
}
return result;
}