utility/json_parser/parse_example2.c
/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#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;
}