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 and proof of functional correctness through the CBMC automated reasoning tool.
See memory requirements for this library here.
#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
.
A compiler that supports C90 or later such as gcc is required to build the library.
The library uses a header file introduced in ISO C99, stdbool.h
. For compilers that do not provide this header file, the source/include directory contains stdbool.readme, which can be renamed to stdbool.h
.
For instance, if the example above is copied to a file named example.c
, gcc can be used like so:
gcc -I source/include example.c source/core_json.c -o example
./example
gcc can also produce an output file to be linked:
gcc -I source/include -c source/core_json.c
The Doxygen references were created using Doxygen version 1.8.20. To generate the Doxygen pages, please run the following command from the root of this repository:
doxygen docs/doxygen/config.doxyfile
By default, the submodules in this repository are configured with update=none
in .gitmodules, to avoid increasing clone time and disk space usage of other repositories (like amazon-freertos that submodules this repository).
To build unit tests, the submodule dependency of Unity is required. Use the following command to clone the submodule:
git submodule update --checkout --init --recursive --test/unit-test/Unity
Go to the root directory of this repository. (Make sure that the Unity submodule is cloned as described above.)
Create build directory: mkdir build && cd build
Run cmake while inside build directory: cmake -S ../test
Run this command to build the library and unit tests: make all
The generated test executables will be present in build/bin/tests
folder.
Run ctest
to execute all tests and view the test run summary.
See CONTRIBUTING.md for information on contributing.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。