JSON is a lightweight data-interchange format. It can represent numbers, strings, ordered sequences of values, and collections of name/value pairs.
JsonCpp is a C++ library that allows manipulating JSON values, including serialization and deserialization to and from strings. It can also preserve existing comment in unserialization/serialization steps, making it a convenient format to store user input files.
JsonCpp documentation is generated using Doxygen.
1.y.zis built with C++11.
0.y.zcan be used with older compilers.
The recommended approach to integrating JsonCpp in your project is to include the amalgamated source (a single
.cpp file and two
.h files) in your project, and compile and build as you would any other source file. This ensures consistency of compilation flags and ABI compatibility, issues which arise when building shared or static libraries. See the next section for instructions.
include/ should be added to your compiler include path. JsonCpp headers should be included as follow:
If JsonCpp was built as a dynamic library on Windows, then your project needs to define the macro
JsonCpp is provided with a script to generate a single header and a single source file to ease inclusion into an existing project. The amalgamated source can be generated at any time by running the following command from the top-directory (this requires Python 2.6):
It is possible to specify header name. See the
-h option for detail.
By default, the following files are generated:
dist/jsoncpp.cpp: source file that needs to be added to your project.
dist/json/json.h: corresponding header file for use in your project. It is equivalent to including
json/json.hin non-amalgamated source. This header only depends on standard headers.
dist/json/json-forwards.h: header that provides forward declaration of all JsonCpp types.
The amalgamated sources are generated by concatenating JsonCpp source in the correct order and defining the macro
JSON_IS_AMALGAMATION to prevent inclusion of other headers.
Conan is an open source package manager intended for C/C++ projects. It is cross platform and build system agnostic.
Conan requires Python for running, and can be installed using pip:
pip install conan
Detailed instructions can be found on conan docs.
For build jsoncpp with conan, you need to create a conanfile.txt or a conanfile.py. The first is simpler, but the second is more flexible.
This is a sample conanfile.txt:
[requires] jsoncpp/1.8.0@theirix/ci [generators] cmake
Note: cmake is not required, you can use other integrations. Or you can set the appropriate environment variables, using virtualenv generators.
Then run the following command from the conanfile directory:
conan install --build missing
This will try to download the appropriate package for your settings (OS, compiler, architecture) from the recipe packages. If it is not found, the package will be built.
Note: you do not need to install cmake to build jsoncpp using conan, because the recipe will download it automatically.
If you need, you can customize the jsoncpp recipe. Just clone/fork it from github.
See integrations instructions for how to use your build system with conan.
CMake is a C++ Makefiles/Solution generator. It is usually available on most Linux system as package. On Ubuntu:
sudo apt-get install cmake
Note that Python is also required to run the JSON reader/writer tests. If missing, the build will skip running those tests.
When running CMake, a few parameters are required:
Steps for generating solution/makefiles using
BUILD_SHARED_LIBSto build as a dynamic library).
Alternatively, from the command-line on Unix in the source directory:
mkdir -p build/debug cd build/debug cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ../.. make
For a good pkg-config file, add:
cmake -h will display the list of available generators (passed using the
By default CMake hides compilation commands. This can be modified by specifying
-DCMAKE_VERBOSE_MAKEFILE=true when generating makefiles.
Note: The SCons-based build system is deprecated. Please use CMake (see the section above).
JsonCpp can use Scons as a build system. Note that SCons requires Python to be installed.
Invoke SCons as follows:
scons platform=$PLATFORM [TARGET]
$PLATFORM may be one of:
suncc: Sun C++ (Solaris)
vacpp: Visual Age C++ (AIX)
msvc6: Microsoft Visual Studio 6 service pack 5-6
msvc70: Microsoft Visual Studio 2002
msvc71: Microsoft Visual Studio 2003
msvc80: Microsoft Visual Studio 2005
msvc90: Microsoft Visual Studio 2008
linux-gcc: Gnu C++ (linux, also reported to work for Mac OS X)
If you are building with Microsoft Visual Studio 2008, you need to set up the environment by running
vcvars32.bat (e.g. MSVC 2008 command prompt) before running SCons.
You need to run tests manually only if you are troubleshooting an issue.
In the instructions below, replace
path/to/jsontest with the path of the
jsontest executable that was compiled on your platform.
cd test # This will run the Reader/Writer tests python runjsontests.py path/to/jsontest # This will run the Reader/Writer tests, using JSONChecker test suite # (http://www.json.org/JSON_checker/). # Notes: not all tests pass: JsonCpp is too lenient (for example, # it allows an integer to start with '0'). The goal is to improve # strict mode parsing to get all tests to pass. python runjsontests.py --with-json-checker path/to/jsontest # This will run the unit tests (mostly Value) python rununittests.py path/to/test_lib_json # You can run the tests using valgrind: python rununittests.py --valgrind path/to/test_lib_json
Note that tests can be run using SCons using the
scons platform=$PLATFORM check
Run the Python script
doxybuild.py from the top directory:
python doxybuild.py --doxygen=$(which doxygen) --open --with-dot
doxybuild.py --help for options.
To add a test, you need to create two files in test/data:
TESTNAME.jsonfile, that contains the input document in JSON format.
TESTNAME.expectedfile, that contains a flatened representation of the input document.
TESTNAME.expected file format is as follows:
=. Array and object values are always empty (i.e. represented by either
.represents the root element, and is used to separate object members.
[N]is used to specify the value of an array element at index
See the examples
test_complex_01.expected to better understand element paths.
When a test is run, output files are generated beside the input test files. Below is a short description of the content of each file:
test_complex_01.json: input JSON document.
test_complex_01.expected: flattened JSON element tree used to check if parsing was corrected.
test_complex_01.actual: flattened JSON element tree produced by
test_complex_01.rewrite: JSON document written by
test_complex_01.jsonand serialized using
test_complex_01.actual-rewrite: flattened JSON element tree produced by
jsontestoutput, typically useful for understanding parsing errors.
LICENSE file for details. In summary, JsonCpp is licensed under the MIT license, or public domain if desired and recognized in your jurisdiction.