README.md 8.1 KB
Newer Older
I
Iñaki Baz Castillo 已提交
1
# JsonCpp
2

3 4 5
[JSON][json-org] is a lightweight data-interchange format. It can represent
numbers, strings, ordered sequences of values, and collections of name/value
pairs.
6

7
[json-org]: http://json.org/
8

I
Iñaki Baz Castillo 已提交
9
JsonCpp is a C++ library that allows manipulating JSON values, including
10 11 12
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.
13

I
Iñaki Baz Castillo 已提交
14 15 16 17 18 19 20 21

## Documentation

[JsonCpp documentation][JsonCpp-documentation] is generated using [Doxygen][].

[JsonCpp-documentation]: http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html
[Doxygen]: http://www.doxygen.org

C
Christopher Dunn 已提交
22

C
Christopher Dunn 已提交
23
## A note on backward-compatibility
I
Iñaki Baz Castillo 已提交
24

C
Christopher Dunn 已提交
25
* `1.y.z` is built with C++11.
26
* `0.y.z` can be used with older compilers.
C
Christopher Dunn 已提交
27
* Major versions maintain binary-compatibility.
28

I
Iñaki Baz Castillo 已提交
29 30 31

## Using JsonCpp in your project

32 33 34 35 36 37
The recommended approach to integrating JsonCpp in your project is to include
the [amalgamated source](#generating-amalgamated-source-and-header) (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.
38
  
I
Iñaki Baz Castillo 已提交
39
The `include/` should be added to your compiler include path. JsonCpp headers
40 41 42 43
should be included as follow:

    #include <json/json.h>

I
Iñaki Baz Castillo 已提交
44 45 46
If JsonCpp was built as a dynamic library on Windows, then your project needs to define the macro `JSON_DLL`.

### Generating amalgamated source and header
47

48 49 50 51 52 53 54 55 56 57
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):

    python amalgamate.py

It is possible to specify header name. See the `-h` option for detail.

By default, the following files are generated:
I
Iñaki Baz Castillo 已提交
58

59 60 61 62 63 64 65 66
* `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.h` in 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
I
Iñaki Baz Castillo 已提交
67
correct order and defining the macro `JSON_IS_AMALGAMATION` to prevent inclusion of other headers.
68 69


I
Iñaki Baz Castillo 已提交
70 71 72 73 74
## Contributing to JsonCpp

### Building and testing with CMake

[CMake][] is a C++ Makefiles/Solution generator. It is usually available on most Linux system as package. On Ubuntu:
75 76 77 78 79 80

    sudo apt-get install cmake

[CMake]: http://www.cmake.org

Note that Python is also required to run the JSON reader/writer tests. If
81
missing, the build will skip running those tests.
82

83
When running CMake, a few parameters are required:
84

I
Iñaki Baz Castillo 已提交
85
* A build directory where the makefiles/solution are generated. It is also used
86
  to store objects, libraries and executables files.
I
Iñaki Baz Castillo 已提交
87
* The generator to use: makefiles or Visual Studio solution? What version or
88 89 90 91 92 93 94
  Visual Studio, 32 or 64 bits solution? 

Steps for generating solution/makefiles using `cmake-gui`:

* Make "source code" point to the source directory.
* Make "where to build the binary" point to the directory to use for the build.
* Click on the "Grouped" check box.
95
* Review JsonCpp build options (tick `BUILD_SHARED_LIBS` to build as a
96 97 98 99
  dynamic library).
* Click the configure button at the bottom, then the generate button.
* The generated solution/makefiles can be found in the binary directory.

100
Alternatively, from the command-line on Unix in the source directory:
101

102 103
    mkdir -p build/debug
    cd build/debug
B
Brandon Myers 已提交
104
    cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ../..
105 106
    make

C
Christopher Dunn 已提交
107 108 109 110
For a good pkg-config file, add:

    -DCMAKE_INSTALL_INCLUDEDIR=include/jsoncpp

111
Running `cmake -h` will display the list of available generators (passed using
112 113 114 115 116
the `-G` option).

By default CMake hides compilation commands. This can be modified by specifying
`-DCMAKE_VERBOSE_MAKEFILE=true` when generating makefiles.

I
Iñaki Baz Castillo 已提交
117 118 119 120
### Building and testing with SCons

**Note:** The SCons-based build system is deprecated. Please use CMake (see the
section above).
121

122
JsonCpp can use [Scons][] as a build system. Note that SCons requires Python to
123 124 125 126 127 128
be installed.

[SCons]: http://www.scons.org/

Invoke SCons as follows:

129
    scons platform=$PLATFORM [TARGET]
130

131
where `$PLATFORM` may be one of:
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

* `suncc`: Sun C++ (Solaris)
* `vacpp`: Visual Age C++ (AIX)
* `mingw`
* `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.

I
Iñaki Baz Castillo 已提交
147 148
### Running the tests manually

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
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
170

I
Iñaki Baz Castillo 已提交
171 172
### Running the tests using SCons

C
Christopher Dunn 已提交
173 174 175
Note that tests can be run using SCons using the `check` target:

    scons platform=$PLATFORM check
176

I
Iñaki Baz Castillo 已提交
177 178
### Building the documentation

179
Run the Python script `doxybuild.py` from the top directory:
180

C
Christopher Dunn 已提交
181
    python doxybuild.py --doxygen=$(which doxygen) --open --with-dot
182

183
See `doxybuild.py --help` for options.
184

I
Iñaki Baz Castillo 已提交
185 186
### Adding a reader/writer test

187
To add a test, you need to create two files in test/data:
188 189 190 191 192 193 194

* a `TESTNAME.json` file, that contains the input document in JSON format.
* a `TESTNAME.expected` file, that contains a flatened representation of the
  input document.

The `TESTNAME.expected` file format is as follows:

I
Iñaki Baz Castillo 已提交
195
* Each line represents a JSON element of the element tree represented by the
196
  input document.
I
Iñaki Baz Castillo 已提交
197
* Each line has two parts: the path to access the element separated from the
198 199
  element value by `=`. Array and object values are always empty (i.e.
  represented by either `[]` or `{}`).
I
Iñaki Baz Castillo 已提交
200
* Element path `.` represents the root element, and is used to separate object
201 202
  members. `[N]` is used to specify the value of an array element at index `N`.

I
Iñaki Baz Castillo 已提交
203
See the examples `test_complex_01.json` and `test_complex_01.expected` to better understand element paths.
204

I
Iñaki Baz Castillo 已提交
205 206 207
### Understanding reader/writer test output

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:
208

209 210 211 212 213 214 215 216 217 218 219 220
* `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 `jsontest`
  from reading `test_complex_01.json`.
* `test_complex_01.rewrite`: JSON document written by `jsontest` using the
  `Json::Value` parsed from `test_complex_01.json` and serialized using
  `Json::StyledWritter`.
* `test_complex_01.actual-rewrite`: flattened JSON element tree produced by
  `jsontest` from reading `test_complex_01.rewrite`.
* `test_complex_01.process-output`: `jsontest` output, typically useful for
  understanding parsing errors.
221

I
Iñaki Baz Castillo 已提交
222 223
## License

224
See the `LICENSE` file for details. In summary, JsonCpp is licensed under the
225
MIT license, or public domain if desired and recognized in your jurisdiction.