basic/json_parser/parse_example9.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 reading nested values from a JSON structure.
*
*
* The parsed JSON looks like:
*
{
"name": "Product",
"properties": {
"id": {
"type": "number",
"description": "Product identifier",
"required": true
},
"name": {
"description": "Name of the product",
"type": "string",
"required": true
},
"price": {
"type": "number",
"minimum": 0,
"required": true
},
"tags": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
*
*
*/
zos_result_t parse_example9(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
{
json_tok_t *prop = JSON_GET_VALUE(context, "properties");
json_tok_t *prop_tags = JSON_GET_VALUE_WITH(context, "tags", prop);
json_tok_t *prop_tags_type = JSON_GET_VALUE_WITH(context, "type", prop_tags);
json_tok_t *prop_tags_items = JSON_GET_VALUE_WITH(context, "items", prop_tags);
json_tok_t *prop_tags_items_type = JSON_GET_VALUE_WITH(context, "type", prop_tags_items);
if(prop != NULL)
{
ZOS_LOG("properties has %d tokens", JSON_UINT32(prop));
if(prop_tags != NULL)
{
ZOS_LOG("properties.tags has %d tokens", JSON_UINT32(prop_tags));
if(prop_tags_type != NULL)
{
ZOS_LOG("properties.tags.type : %s", JSON_STR(prop_tags_type));
}
else
{
ZOS_LOG("Failed to get properties.tags.type token");
}
if(prop_tags_items != NULL)
{
ZOS_LOG("properties.tags.items has %d tokens", JSON_UINT32(prop_tags_items));
if(prop_tags_items_type != NULL)
{
ZOS_LOG("properties.tags.items.type : %s", JSON_STR(prop_tags_items_type));
}
else
{
ZOS_LOG("Failed to get properties.tags.items.type token");
}
}
else
{
ZOS_LOG("Failed to get properties.tags.items token");
}
}
else
{
ZOS_LOG("Failed to get properties.tags token");
}
}
else
{
ZOS_LOG("Failed to get properties token");
}
}
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;
}