View all files | ||||
jsoncons is a C++, header-only library for constructing JSON and JSON-like data formats such as CBOR. For each supported data format, it enables you to work with the data in a number of ways:
As a variant-like, allocator-aware, data structure, basic_json
As a strongly typed C++ data structure that implements jsoncons reflection traits
With cursor-level access to a stream of parse events, somewhat analogous to StAX pull parsing and push serializing in the XML world.
Compared to other JSON libraries, jsoncons has been designed to handle very large JSON texts. At its heart are SAX-style parsers and serializers. It supports reading an entire JSON text in memory in a variant-like structure. But it also supports efficient access to the underlying data using StAX-style pull parsing and push serializing. And it supports incremental parsing into a user's preferred types, using information about user types provided by specializations of reflection traits.
The jsoncons data model supports the familiar JSON types - nulls, booleans, numbers, strings, arrays, objects - plus byte strings. In addition, jsoncons supports semantic tagging of datetimes, epoch times, big integers, big decimals, big floats and binary encodings. This allows it to preserve these type semantics when parsing JSON-like data formats such as CBOR that have them.
jsoncons is distributed under the Boost Software License.
jsoncons is free but welcomes support to sustain its development. If you find this library helpful, please consider making a one time donation or becoming a ❤️ sponsor.
As the jsoncons library has evolved, names have sometimes changed, see deprecated. To ease transition, jsoncons deprecates the old names but continues to support many of them. The deprecated names can be suppressed by defining the macro JSONCONS_NO_DEPRECATED, and doing so is recommended for new code.
"Apache Kvrocks consistently utilizes jsoncons to offer support for JSON data structures to users. We find the development experience with jsoncons outstanding!"
"I have been using your library in my native language – R – and have created an R package making it easy for (a) JMESpath and JSONpath queries on JSON strings or R objects and (b) for other R developers to link to your library."
"I’m using your library for an external interface to pass data, as well as using the conversions from csv to json, which are really helpful for converting data for use in javascript"
"Verified that, for my needs in JSON and CBOR, it is working perfectly"
"the JSONPath feature of this library, it's great"
"We use JMESPath implementation quite extensively"
"We love your JSON Schema validator. We are using it in ER/Studio our data modelling tool to parse JSON Schema files so we can create entity relations models from them."
"the serialization lib of choice with its beautiful mappings and ease of use"
"really good" "awesome project" "very solid and very dependable" "my team loves it" "Your repo rocks!!!!!"
Get started with HealthImaging image sets and image frames using an AWS SDK
RubyGems.org rjsoncons CoppeliaSim reflect-cpp
The benchmark code is available here.
JSONPath Comparison shows how jsoncons JsonPath compares with other implementations
You can use the vcpkg platform library manager to install the jsoncons package.
Or, download the latest release and unpack the zip file. Copy the directory include/jsoncons to your include directory. If you wish to use extensions, copy include/jsoncons_ext as well.
Or, download the latest code on main.
The library requires a C++ Compiler with C++11 support. In addition the library defines jsoncons::endian, jsoncons::basic_string_view, jsoncons::optional, and jsoncons::span, which will be typedef-ed to their standard library equivalents if detected. Otherwise they will be typedef-ed to internal, C++11 compatible, implementations.
The library uses exceptions and in some cases std::error_code's to report errors. Apart from jsoncons::assertion_error, all jsoncons exception classes implement the jsoncons::json_error interface. If exceptions are disabled or if the compile time macro JSONCONS_NO_EXCEPTIONS is defined, throws become calls to std::terminate.
For the examples below you need to include some header files and initialize a string of JSON data:
jsoncons allows you to work with the data in a number of ways:
As a variant-like data structure, basic_json
As a strongly typed C++ data structure that implements jsoncons reflection traits
With cursor-level access to a stream of parse events
Output:
jsoncons supports transforming JSON texts into C++ data structures. The functions decode_json and encode_json convert strings or streams of JSON data to C++ data structures and back. Decode and encode work for all C++ classes that implement jsoncons reflection traits. defined. jsoncons already supports many types in the standard library, and your own types will be supported too if you specialize reflection traits in the jsoncons namespace.
Output:
This example makes use of the convenience macros JSONCONS_ENUM_TRAITS, JSONCONS_N_CTOR_GETTER_TRAITS, and JSONCONS_ALL_CTOR_GETTER_TRAITS to specialize the reflection traits for the enum type ns::hiking_experience, the class ns::hiking_reputon (with some non-mandatory members), and the class ns::hiking_reputation (with all mandatory members.) The macro JSONCONS_ENUM_TRAITS generates the code from the enum identifiers, and the macros JSONCONS_N_CTOR_GETTER_TRAITS and JSONCONS_ALL_CTOR_GETTER_TRAITS generate the code from the get functions and a constructor. These macro declarations must be placed outside any namespace blocks.
See examples for other ways of specializing reflection traits.
A typical pull parsing application will repeatedly process the current() event and call next() to advance to the next event, until done() returns true.
Output:
You can apply a filter to a cursor using the pipe syntax (e.g., cursor | filter1 | filter2 | ...)
Output:
For the examples below you need to include some header files and initialize a buffer of CBOR data:
jsoncons allows you to work with the CBOR data similarly to JSON data:
As a variant-like data structure, basic_json
As a strongly typed C++ data structure that implements reflection traits
With cursor-level access to a stream of parse events
Output:
Output:
Note that when decoding the bigfloat and decimal fraction into a std::string, we lose the semantic information that the variant like data structure preserved with a tag, so serializing back to CBOR produces a text string.
A typical pull parsing application will repeatedly process the current() event and call next() to advance to the next event, until done() returns true.
Output:
You can apply a filter to a cursor using the pipe syntax,
Output:
jsoncons requires a compiler with minimally C++11 support. It is tested in continuous integration on Github Actions and circleci. UndefinedBehaviorSanitizer (UBSan) diagnostics are enabled for selected gcc and clang builds. Since v0.151.0, it is integrated with Google OSS-fuzz, with coverage for all parsers and encoders.
| Visual Studio | vs2019 | default | x86, x64 | Windows 11 | GitHub Actions |
| vs2022 | default | x86, x64 | Windows 11 | GitHub Actions | |
| Visual Studio - clang | vs2019 | default | x86, x64 | Windows 11 | GitHub Actions |
| vs2022 | default | x86, x64 | Windows 11 | GitHub Actions | |
| g++ | 6, 7, 8, 9, 10, 11, 12 | default | x64 | Ubuntu | circleci |
| g++ | 12 | c++20 | x64 | Ubuntu | GitHub Actions |
| clang | 3.9, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 | default | x64 | Ubuntu | circleci |
| clang | 14 | c++20 | x64 | Ubuntu | GitHub Actions |
| clang xcode | 11, 12, 13 | default | x64 | OSX 11 | GitHub Actions |
| clang xcode | 13, 14 | default | x64 | OSX 12 | GitHub Actions |
CMake is a cross-platform build tool that generates makefiles and solutions for the compiler environment of your choice. On Windows you can download a Windows Installer package. On Linux it is usually available as a package, e.g., on Ubuntu,
Once cmake is installed, you can build and run the unit tests from the jsoncons directory,
On Windows:
On UNIX:
jsoncons uses the PVS-Studio static analyzer, provided free for open source projects.
A big thanks to the comp.lang.c++ community for help with implementation details.
The jsoncons platform dependent binary configuration draws on to the excellent MIT licensed tinycbor.
Thanks to Milo Yip, author of RapidJSON, for raising the quality of JSON libraries across the board, by publishing the benchmarks, and contacting this project (among others) to share the results.
The library includes a slightly modified version of the Grisu3 algorithm for printing floating-point numbers from Florian Loitsch ahich is licensed under the MIT License. Copyright © 2009 Florian Loitsch
Since 1.9.0, the library includes a slightly modified version of the A5HASH Fast Hash Functions from Aleksey Vaneev which is licensed under the MIT License. Copyright (c) 2025 Aleksey Vaneev
The macro JSONCONS_ALL_MEMBER_TRAITS follows the approach taken by Martin York's ThorsSerializer
The jsoncons implementations of BSON decimal128 to and from string, and ObjectId to and from string, are based on the Apache 2 licensed libbson.
Special thanks to our contributors