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 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## Building and testing with other build systems 40See https://github.com/open-source-parsers/jsoncpp/wiki/Building 41 42## Running the tests manually 43 44You need to run tests manually only if you are troubleshooting an issue. 45 46In the instructions below, replace `path/to/jsontest` with the path of the 47`jsontest` executable that was compiled on your platform. 48 49 cd test 50 # This will run the Reader/Writer tests 51 python runjsontests.py path/to/jsontest 52 53 # This will run the Reader/Writer tests, using JSONChecker test suite 54 # (http://www.json.org/JSON_checker/). 55 # Notes: not all tests pass: JsonCpp is too lenient (for example, 56 # it allows an integer to start with '0'). The goal is to improve 57 # strict mode parsing to get all tests to pass. 58 python runjsontests.py --with-json-checker path/to/jsontest 59 60 # This will run the unit tests (mostly Value) 61 python rununittests.py path/to/test_lib_json 62 63 # You can run the tests using valgrind: 64 python rununittests.py --valgrind path/to/test_lib_json 65 66## Building the documentation 67 68Run the Python script `doxybuild.py` from the top directory: 69 70 python doxybuild.py --doxygen=$(which doxygen) --open --with-dot 71 72See `doxybuild.py --help` for options. 73 74## Adding a reader/writer test 75 76To add a test, you need to create two files in test/data: 77 78* a `TESTNAME.json` file, that contains the input document in JSON format. 79* a `TESTNAME.expected` file, that contains a flatened representation of the 80 input document. 81 82The `TESTNAME.expected` file format is as follows: 83 84* Each line represents a JSON element of the element tree represented by the 85 input document. 86* Each line has two parts: the path to access the element separated from the 87 element value by `=`. Array and object values are always empty (i.e. 88 represented by either `[]` or `{}`). 89* Element path `.` represents the root element, and is used to separate object 90 members. `[N]` is used to specify the value of an array element at index `N`. 91 92See the examples `test_complex_01.json` and `test_complex_01.expected` to better understand element paths. 93 94## Understanding reader/writer test output 95 96When a test is run, output files are generated beside the input test files. Below is a short description of the content of each file: 97 98* `test_complex_01.json`: input JSON document. 99* `test_complex_01.expected`: flattened JSON element tree used to check if 100 parsing was corrected. 101* `test_complex_01.actual`: flattened JSON element tree produced by `jsontest` 102 from reading `test_complex_01.json`. 103* `test_complex_01.rewrite`: JSON document written by `jsontest` using the 104 `Json::Value` parsed from `test_complex_01.json` and serialized using 105 `Json::StyledWritter`. 106* `test_complex_01.actual-rewrite`: flattened JSON element tree produced by 107 `jsontest` from reading `test_complex_01.rewrite`. 108* `test_complex_01.process-output`: `jsontest` output, typically useful for 109 understanding parsing errors. 110 111## Versioning rules 112 113Consumers of this library require a strict approach to incrementing versioning of the JsonCpp library. Currently, we follow the below set of rules: 114 115* Any new public symbols require a minor version bump. 116* Any alteration or removal of public symbols requires a major version bump, including changing the size of a class. This is necessary for 117consumers to do dependency injection properly. 118 119## Preparing code for submission 120 121Generally, JsonCpp's style guide has been pretty relaxed, with the following common themes: 122 123* Variables and function names use lower camel case (E.g. parseValue or collectComments). 124* Class use camel case (e.g. OurReader) 125* Member variables have a trailing underscore 126* Prefer `nullptr` over `NULL`. 127* Passing by non-const reference is allowed. 128* Single statement if blocks may omit brackets. 129* Generally prefer less space over more space. 130 131For an example: 132 133```c++ 134bool Reader::decodeNumber(Token& token) { 135 Value decoded; 136 if (!decodeNumber(token, decoded)) 137 return false; 138 currentValue().swapPayload(decoded); 139 currentValue().setOffsetStart(token.start_ - begin_); 140 currentValue().setOffsetLimit(token.end_ - begin_); 141 return true; 142} 143``` 144 145Before 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: 146 147``` 148ninja -v -C build-${LIB_TYPE}/ clang-format 149``` 150