yyjson 0.11.0
A high performance C JSON library.
|
There are several ways to integrate this library into your project: source code, package manager, and CMake.
This library aims to provide a cross-platform JSON library, so it is written in ANSI C (actually C99, but compatible with strict C89). You can copy yyjson.h
and yyjson.c
to your project and start using it without any configuration.
The library has been tested with gcc
, clang
, msvc
, tcc
compilers and x86
, arm
, ppc
, riscv
, s390x
architectures in Github CI. Please report a bug if you encounter any compilation issues.
The library has all features enabled by default, but you can trim out some of them by adding compile-time options. For example, you can disable the JSON writer to reduce the binary size when you don't need serialization, or disable comments support to improve parsing performance. See Compile-time Options
for details.
You can use some popular package managers like vcpkg
, conan
, and xmake
to download and install yyjson. The yyjson package in these package managers is kept up to date by community contributors. If the version is out of date, please create an issue or pull request on their repository.
You can build and install yyjson using vcpkg dependency manager:
If the version is out of date, please create an issue or pull request on the vcpkg repository.
Clone the repository and create build directory:
Build static library:
Build shared library:
Supported CMake options (default OFF):
-DYYJSON_BUILD_TESTS=ON
Build all tests.-DYYJSON_BUILD_FUZZER=ON
Build fuzzer with LibFuzzing.-DYYJSON_BUILD_MISC=ON
Build misc.-DYYJSON_BUILD_DOC=ON
Build documentation with doxygen.-DYYJSON_ENABLE_COVERAGE=ON
Enable code coverage for tests.-DYYJSON_ENABLE_VALGRIND=ON
Enable valgrind memory checker for tests.-DYYJSON_ENABLE_SANITIZE=ON
Enable sanitizer for tests.-DYYJSON_ENABLE_FASTMATH=ON
Enable fast-math for tests.-DYYJSON_FORCE_32_BIT=ON
Force 32-bit for tests (gcc/clang/icc).-DYYJSON_DISABLE_READER=ON
Disable JSON reader if you don't need it.-DYYJSON_DISABLE_WRITER=ON
Disable JSON writer if you don't need it.-DYYJSON_DISABLE_INCR_READER=ON
Disable incremental reader if you don't need it.-DYYJSON_DISABLE_UTILS=ON
Disable JSON Pointer, JSON Patch and JSON Merge Patch.-DYYJSON_DISABLE_FAST_FP_CONV=ON
Disable builtin fast floating-pointer conversion.-DYYJSON_DISABLE_NON_STANDARD=ON
Disable non-standard JSON support at compile-time.-DYYJSON_DISABLE_UTF8_VALIDATION=ON
Disable UTF-8 validation at compile-time.-DYYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS=ON
Disable unaligned memory access support at compile-time.You can download and unzip yyjson to your project directory and link it in your CMakeLists.txt
file:
If your CMake version is higher than 3.11, you can use the following code to let CMake automatically download it:
If you want to build or debug yyjson with another compiler or IDE, try these commands:
This project uses doxygen to generate the documentation. Make sure doxygen
is installed on your system before proceeding, it's best to use the version specified in doc/Doxyfile.in
.
To build the documentation:
The generated HTML files will be located in build/doxygen/html
.
You can also browse the pre-generated documentation online: https://ibireme.github.io/yyjson/doc/doxygen/html/
Build and run all tests:
Build and run tests with valgrind memory checker, (make sure you have valgrind
installed before proceeding):
Build and run tests with sanitizer (compiler should be gcc
or clang
):
Build and run code coverage with gcc
:
Build and run code coverage with clang
:
Build and run fuzz test with LibFuzzer (compiler should be LLVM Clang
, while Apple Clang
or gcc
are not supported):
This library provides some compile-time options that can be defined as 1 to disable specific features during compilation. For example, to disable the JSON writer:
Define as 1 to disable JSON reader at compile-time.
This disables functions with read
in their name.
Reduces binary size by about 60%.
It is recommended when JSON parsing is not required.
Define as 1 to disable JSON writer at compile-time.
This disables functions with write
in their name.
Reduces binary size by about 30%.
It is recommended when JSON serialization is not required.
Define as 1 to disable JSON incremental reader at compile-time.
This disables functions with incr
in their name.
It is recommended when JSON incremental reader is not required.
Define as 1 to disable JSON Pointer, JSON Patch and JSON Merge Patch supports.
This disables functions with ptr
or patch
in their name.
It is recommended when these functions are not required.
Define as 1 to disable the fast floating-point number conversion in yyjson.
Libc's strtod/snprintf
will be used instead.
This reduces binary size by about 30%, but significantly slows down the floating-point read/write speed.
It is recommended when processing JSON with few floating-point numbers.
Define as 1 to disable non-standard JSON features support at compile-time:
This reduces binary size by about 10%, and slightly improves performance.
It is recommended when not dealing with non-standard JSON.
Define as 1 to disable UTF-8 validation at compile-time.
Use this if all input strings are guaranteed to be valid UTF-8 (e.g. language-level String types are already validated).
Disabling UTF-8 validation improves performance for non-ASCII strings by about 3% to 7%.
Note: If this flag is enabled while passing illegal UTF-8 strings, the following errors may occur:
yyjson_mut_val
, the string's end may be accessed out of bounds, potentially causing a segmentation fault.Define as 1 to export symbols when building the library as a Windows DLL.
Define as 1 to import symbols when using the library as a Windows DLL.