utility/json_parser/parse_example2.c
/*
* EVALUATION AND USE OF THIS SOFTWARE IS SUBJECT TO THE TERMS AND
* CONDITIONS OF THE CONTROLLING LICENSE AGREEMENT FOUND AT LICENSE.md
* IN THIS SDK. IF YOU DO NOT AGREE TO THE LICENSE TERMS AND CONDITIONS,
* PLEASE RETURN ALL SOURCE FILES TO SILICON LABORATORIES.
* (c) Copyright 2018, Silicon Laboratories Inc. All rights reserved.
*/
#include "gos.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"
* }
* ]}
*
*
*/
{
gos_result_t result;
gos_json_parse_context_t *context = NULL;
char *json_data = NULL;
{
GOS_LOG("Failed to read: %s", filename);
return result;
}
{
.buffer = json_data,
.buffer_len = strlen(json_data)
};
{
GOS_LOG("Failed to initialize json parsing context");
}
{
GOS_LOG("Failed to parse json file");
}
else
{
// get the array token
{
GOS_LOG("Malformed JSON!");
goto cleanup;
}
{
if (name_tok == NULL || value_tok == NULL)
{
GOS_LOG("Malformed JSON!");
result = GOS_ERROR;
break;
}
// Just skip to a token in the current object
// The gos_json_context_get_value() automatically searches for the next object starting at this token
obj_tok = name_tok->next;
}
}
cleanup:
gos_json_parse_context_deinit(context);
gos_free(json_data);
GOS_LOG("Finished");
return result;
}
/*************************************************************************************************
* Pre-read the JSON file into a buffer
*/
{
gos_result_t result;
uint8_t *ptr;
{
}
{
}
{
}
{
}
else
{
ptr[fd.size] = 0; // null-terminate string
*buffer = (char*)ptr;
}
return result;
}