This repository contains the coreJSON library, a parser that strictly enforces the ECMA-404 JSON standard and is suitable for low memory footprint embedded devices. The coreJSON library is distributed under the MIT Open Source License.
This library has gone through code quality checks including verification that no function has a GNU Complexity score over 8, and checks against deviations from mandatory rules in the MISRA coding standard. Deviations from the MISRA C:2012 guidelines are documented under MISRA Deviations. This library has also undergone both static code analysis from Coverity static analysis, and validation of memory safety through the CBMC automated reasoning tool.
See memory requirements for this library here.
coreJSON v3.2.0 source code is part of the FreeRTOS 202210.00 LTS release.
coreJSON v3.0.0 source code is part of the FreeRTOS 202012.00 LTS release.
#include <stdio.h>
#include "core_json.h"
int main()
{
// Variables used in this example.
JSONStatus_t result;
char buffer[] = "{\"foo\":\"abc\",\"bar\":{\"foo\":\"xyz\"}}";
size_t bufferLength = sizeof( buffer ) - 1;
char queryKey[] = "bar.foo";
size_t queryKeyLength = sizeof( queryKey ) - 1;
char * value;
size_t valueLength;
// Calling JSON_Validate() is not necessary if the document is guaranteed to be valid.
result = JSON_Validate( buffer, bufferLength );
if( result == JSONSuccess )
{
result = JSON_Search( buffer, bufferLength, queryKey, queryKeyLength,
&value, &valueLength );
}
if( result == JSONSuccess )
{
// The pointer "value" will point to a location in the "buffer".
char save = value[ valueLength ];
// After saving the character, set it to a null byte for printing.
value[ valueLength ] = '\0';
// "Found: bar.foo -> xyz" will be printed.
printf( "Found: %s -> %s\n", queryKey, value );
// Restore the original character.
value[ valueLength ] = save;
}
return 0;
}
A search may descend through nested objects when the queryKey
contains matching key strings joined by a separator, .
. In the example above, bar
has the value {"foo":"xyz"}
. Therefore, a search for query key bar.foo
would output xyz
.
For pre-generated documentation, please see the documentation linked in the locations below:
Location |
---|
FreeRTOS.org |
Note that the latest included version of the coreJSON library may differ across repositories.