1# Contributing to JsonCpp
2
3## Building
4
5Both CMake and Meson tools are capable of generating a variety of build environments for you preferred development environment.
6Using cmake or meson you can generate an XCode, Visual Studio, Unix Makefile, Ninja, or other environment that fits your needs.
7
8An example of a common Meson/Ninja environment is described next.
9
10## Building and testing with Meson/Ninja
11Thanks to David Seifert (@SoapGentoo), we (the maintainers) now use
12[meson](http://mesonbuild.com/) and [ninja](https://ninja-build.org/) to build
13for debugging, as well as for continuous integration (see
14[`./.travis_scripts/meson_builder.sh`](./.travis_scripts/meson_builder.sh) ). Other systems may work, but minor
15things like version strings might break.
16
17First, install both meson (which requires Python3) and ninja.
18If you wish to install to a directory other than /usr/local, set an environment variable called DESTDIR with the desired path:
19    DESTDIR=/path/to/install/dir
20
21Then,
22```sh
23    cd jsoncpp/
24    BUILD_TYPE=debug
25    #BUILD_TYPE=release
26    LIB_TYPE=shared
27    #LIB_TYPE=static
28    meson --buildtype ${BUILD_TYPE} --default-library ${LIB_TYPE} . build-${LIB_TYPE}
29    ninja -v -C build-${LIB_TYPE}
30
31    ninja -C build-static/ test
32
33    # Or
34    #cd build-${LIB_TYPE}
35    #meson test --no-rebuild --print-errorlogs
36
37    sudo ninja install
38```
39
40## Building and testing with other build systems
41See https://github.com/open-source-parsers/jsoncpp/wiki/Building
42
43## Running the tests manually
44
45You need to run tests manually only if you are troubleshooting an issue.
46
47In the instructions below, replace `path/to/jsontest` with the path of the
48`jsontest` executable that was compiled on your platform.
49
50    cd test
51    # This will run the Reader/Writer tests
52    python runjsontests.py path/to/jsontest
53
54    # This will run the Reader/Writer tests, using JSONChecker test suite
55    # (http://www.json.org/JSON_checker/).
56    # Notes: not all tests pass: JsonCpp is too lenient (for example,
57    # it allows an integer to start with '0'). The goal is to improve
58    # strict mode parsing to get all tests to pass.
59    python runjsontests.py --with-json-checker path/to/jsontest
60
61    # This will run the unit tests (mostly Value)
62    python rununittests.py path/to/test_lib_json
63
64    # You can run the tests using valgrind:
65    python rununittests.py --valgrind path/to/test_lib_json
66
67## Building the documentation
68
69Run the Python script `doxybuild.py` from the top directory:
70
71    python doxybuild.py --doxygen=$(which doxygen) --open --with-dot
72
73See `doxybuild.py --help` for options.
74
75## Adding a reader/writer test
76
77To add a test, you need to create two files in test/data:
78
79* a `TESTNAME.json` file, that contains the input document in JSON format.
80* a `TESTNAME.expected` file, that contains a flatened representation of the
81  input document.
82
83The `TESTNAME.expected` file format is as follows:
84
85* Each line represents a JSON element of the element tree represented by the
86  input document.
87* Each line has two parts: the path to access the element separated from the
88  element value by `=`. Array and object values are always empty (i.e.
89  represented by either `[]` or `{}`).
90* Element path `.` represents the root element, and is used to separate object
91  members. `[N]` is used to specify the value of an array element at index `N`.
92
93See the examples `test_complex_01.json` and `test_complex_01.expected` to better understand element paths.
94
95## Understanding reader/writer test output
96
97When a test is run, output files are generated beside the input test files. Below is a short description of the content of each file:
98
99* `test_complex_01.json`: input JSON document.
100* `test_complex_01.expected`: flattened JSON element tree used to check if
101  parsing was corrected.
102* `test_complex_01.actual`: flattened JSON element tree produced by `jsontest`
103  from reading `test_complex_01.json`.
104* `test_complex_01.rewrite`: JSON document written by `jsontest` using the
105  `Json::Value` parsed from `test_complex_01.json` and serialized using
106  `Json::StyledWritter`.
107* `test_complex_01.actual-rewrite`: flattened JSON element tree produced by
108  `jsontest` from reading `test_complex_01.rewrite`.
109* `test_complex_01.process-output`: `jsontest` output, typically useful for
110  understanding parsing errors.
111
112## Versioning rules
113
114Consumers of this library require a strict approach to incrementing versioning of the JsonCpp library. Currently, we follow the below set of rules:
115
116* Any new public symbols require a minor version bump.
117* Any alteration or removal of public symbols requires a major version bump, including changing the size of a class. This is necessary for
118consumers to do dependency injection properly.
119
120## Preparing code for submission
121
122Generally, JsonCpp's style guide has been pretty relaxed, with the following common themes:
123
124* Variables and function names use lower camel case (E.g. parseValue or collectComments).
125* Class use camel case (e.g. OurReader)
126* Member variables have a trailing underscore
127* Prefer `nullptr` over `NULL`.
128* Passing by non-const reference is allowed.
129* Single statement if blocks may omit brackets.
130* Generally prefer less space over more space.
131
132For an example:
133
134```c++
135bool Reader::decodeNumber(Token& token) {
136  Value decoded;
137  if (!decodeNumber(token, decoded))
138    return false;
139  currentValue().swapPayload(decoded);
140  currentValue().setOffsetStart(token.start_ - begin_);
141  currentValue().setOffsetLimit(token.end_ - begin_);
142  return true;
143}
144```
145
146Before submitting your code, ensure that you meet the versioning requirements above, follow the style guide of the file you are modifying (or the above rules for new files), and run clang format. Meson exposes clang format with the following command:
147```
148ninja -v -C build-${LIB_TYPE}/ clang-format
149```
150
151For convenience, you can also run the `reformat.sh` script located in the root directory.
152
153