Reflection for RapidJSON  0.0.3
Reflection for serializing/deserializing with RapidJSON
README.md
Go to the documentation of this file.
1 # Reflective RapidJSON
2 
3 The main goal of this project is to provide a code generator for serializing/deserializing C++ objects to/from JSON
4 using Clang and RapidJSON.
5 
6 However, extending the generator to generate code for other applications of reflection or to provide generic
7 reflection would be possible as well.
8 
9 ## Open for other reflection approaches
10 The reflection implementation used behind the scenes of this library is exchangeable:
11 
12 * This repository already provides a small, additional header to use RapidJSON with Boost.Hana. This allows to
13  serialize or dezerialize simple data structures declared using the `BOOST_HANA_DEFINE_STRUCT` macro rather than
14  requiring the code generator.
15 * When native reflection becomes standardized, it would be possible to make use of it as well. In this case,
16  the code generator could still act as a fallback.
17 
18 ## Current state
19 The basic functionality is implemented, tested and documented:
20 
21 * Serialization and deserialization of datatypes listed under "Supported datatypes"
22  * Nesting and inheritance is possible
23  * Adapting 3rdparty structs/classes is supported
24 * Basic error handling when deserializing
25 * CMake macro to conveniently include the code generator into the build process
26 * Allow to use Boost.Hana
27 
28 ### Planned features and TODOs
29 There are still things missing which would likely be very useful in practise. The following list contains the
30 open TODOs which are supposed to be most relevant in practise:
31 
32 * [ ] Allow to specify which member variables should be considered
33  * This could work similar to Qt's Signals & Slots macros.
34  * But there should also be a way to do this for 3rdparty types.
35  * Note that currently all public, non-static member variables are (de)serialized.
36 * [ ] Support getter/setter methods
37  * [ ] Allow to serialize the result of methods
38  * [ ] Allow to pass a deserialized value to a method
39 * [ ] Validate enum values when deserializing
40 * [ ] Untie serialization and deserialization
41 
42 For a full list of further ideas, see [TODOs.md](./TODOs.md).
43 
44 ## Supported datatypes
45 The following table shows the mapping of supported C++ types to supported JSON types:
46 
47 | C++ type | JSON type |
48 | ------------------------------------------------- |:------------:|
49 | custom structures/classes | object |
50 | `bool` | true/false |
51 | signed and unsigned integral types | number |
52 | `float` and `double` | number |
53 | `enum` and `enum class` | number |
54 | `std::string` | string |
55 | `const char *` | string |
56 | iteratable lists (`std::vector`, `std::list`, ...)| array |
57 | `std::tuple` | array |
58 | `std::unique_ptr`, `std::shared_ptr` | depends/null |
59 | `std::map`, `std::unordered_map` | object |
60 | `JsonSerializable` | object |
61 
62 ### Remarks
63 * Raw pointer are not supported. This prevents
64  forgetting to free memory which would have to be allocated when deserializing.
65 * For the same reason `const char *` strings are only supported for serialization.
66 * Enums are (de)serialized as their underlying integer value. When deserializing, it is currently *not* checked
67  whether the present integer value is a valid enumeration item.
68 * The JSON type for smart pointer depends on the type the pointer refers to. It can also be `null`.
69 * For deserialization
70  * iteratables must provide an `emplace_back` method. So deserialization of eg. `std::forward_list`
71  is currently not supported.
72  * custom types must provide a default constructor.
73  * constant member variables are skipped.
74 * For custom (de)serialization, see the section below.
75 
76 ## Usage
77 This example shows how the library can be used to make a `struct` serializable:
78 <pre>
79 #include <reflective_rapidjson/json/serializable.h>
80 
81 // define structures, eg.
82 struct TestObject : public ReflectiveRapidJSON::JsonSerializable<TestObject> {
83  int number;
84  double number2;
85  vector<int> numbers;
86  string text;
87  bool boolean;
88 };
89 struct NestingObject : public ReflectiveRapidJSON::JsonSerializable<NestingObject> {
90  string name;
91  TestObject testObj;
92 };
93 struct NestingArray : public ReflectiveRapidJSON::JsonSerializable<NestingArray> {
94  string name;
95  vector<TestObject> testObjects;
96 };
97 
98 // serialize to JSON
99 NestingArray obj{ ... };
100 cout << "JSON: " << obj.toJson().GetString();
101 
102 // deserialize from JSON
103 const auto obj = NestingArray::fromJson(...);
104 
105 // in exactly one of the project's translation units
106 #include "reflection/code-defining-structs.h"
107 </pre>
108 
109 Note that the header included at the bottom must be generated by invoking the code generator appropriately, eg.:
110 <pre>
111 reflective_rapidjson_generator \
112  --input-file "$srcdir/code-defining-structs.cpp" \
113  --output-file "$builddir/reflection/code-defining-structs.h"
114 </pre>
115 
116 #### Invoking code generator with CMake macro
117 It is possible to use the provided CMake macro to automate the code generator invocation:
118 <pre>
119 # find the package and make macro available
120 find_package(reflective-rapidjson REQUIRED)
121 list(APPEND CMAKE_MODULE_PATH ${REFLECTIVE_RAPIDJSON_MODULE_DIRS})
122 include(ReflectionGenerator)
123 
124 # "link" against reflective_rapidjson
125 # it is a header-only lib so this will only add the required include paths
126 # to your target
127 target_link_libraries(mytarget PRIVATE reflective_rapidjson)
128 
129 # invoke macro
130 add_reflection_generator_invocation(
131  INPUT_FILES code-defining-structs.cpp
132  GENERATORS json
133  OUTPUT_LISTS LIST_OF_GENERATED_HEADERS
134  CLANG_OPTIONS_FROM_TARGETS mytarget
135 )
136 </pre>
137 
138 This will produce the file `code-defining-structs.h` in the directory `reflection` in the current build directory. So
139 make sure the current build directory is added to the include directories of your target. The default output directory can
140 also be overridden by passing `OUTPUT_DIRECTORY custom/directory` to the arguments.
141 
142 It is possible to specify multiple input files at once. A separate output file is generated for each input. The output files
143 will always have the extension `.h`, independently of the extension of the input file.
144 
145 The full paths of the generated files are also appended to the variable `LIST_OF_GENERATED_HEADERS` which then can be added
146 to the sources of your target. Of course this can be skipped if not required/wanted.
147 
148 The macro will also automatically pass Clang's resource directory which is detected by invoking `clang -print-resource-dir`.
149 To adjust that, just set the cache variable `REFLECTION_GENERATOR_CLANG_RESOURCE_DIR` before including the module.
150 
151 For an explanation of the `CLANG_OPTIONS_FROM_TARGETS` argument, read the next section.
152 
153 #### Passing Clang options
154 It is possible to pass additional options to the Clang tool invocation used by the code generator.
155 This can be done using the `--clang-opt` argument or the `CLANG_OPTIONS` argument when using the CMake macro.
156 
157 For example, additional definitions could be added using `--clang-opt -DSOME_DEFINE -DANOTHER_DEFINE`.
158 But it is actually possible to pass anything from `clang --help`, including the `-X...` options.
159 
160 ##### Specifying Clang's resource directory
161 In case you get a massive number of errors, ensure Clang's resource directory can be located.
162 [Clang documentation](https://clang.llvm.org/docs/LibTooling.html#libtooling-builtin-includes):
163 
164 > The default location to look for builtin headers is in a path `$(dirname /path/to/tool)/../lib/clang/3.3/include` relative to the tool binary.
165 
166 To adjust the default location, just add eg. `--clang-opt -resource-dir /usr/lib/clang/5.0.1` to the arguments.
167 
168 ##### Pass options from regular targets
169 It makes most sense to specify the same options for the code generator as during the actual compilation. This way the code
170 generator uses the same flags, defines and include directories as the compiler and hence behaves like the compiler.
171 When using the CMake macro, it is possible to automatically pass all compile flags, compile definitions and include directories
172 from certain targets to the code generator. Those targets can be specified using the
173 Macro's `CLANG_OPTIONS_FROM_TARGETS` argument.
174 
175 #### Notes regarding cross-compilation
176 * For cross compilation, it is required to build the code generator for the platform you're building on.
177 * Since the code generator is likely not required under the target platform, you should add `-DNO_GENERATOR:BOOL=ON` to the CMake
178  arguments when building Reflective RapidJSON for the target platform.
179 * When using the `add_reflection_generator_invocation` macro, you need to set the following CMake cache variables:
180  * `REFLECTION_GENERATOR_EXECUTABLE:FILEPATH=/path/to/reflective_rapidjson_generator`
181  * specifies the path of the code generator executable built for the platform you're building on
182  * only required if the executable is not in the path anyways
183  * `REFLECTION_GENERATOR_TRIPLE:STRING=machine-vendor-operatingsystem`
184  * specifies the GNU platform triple for the target platform
185  * examples for cross compiling with mingw-w64 under GNU/Linux:
186  `x86_64-w64-mingw32`, `i686-w64-mingw32`
187  * `REFLECTION_GENERATOR_INCLUDE_DIRECTORIES:STRING=/custom/prefix/include`
188  * implicit include directories for target platform
189  * example for cross compiling with mingw-w64 under GNU/Linux:
190  `/usr/lib/gcc/x86_64-w64-mingw32/7.2.1/include;/usr/x86_64-w64-mingw32/include/c++/7.2.1/x86_64-w64-mingw32;/usr/x86_64-w64-mingw32/include`
191 * The Arch Linux packages mentioned at the end of the README file also include `mingw-w64` variants which give a concrete example how
192  cross-compilation can be done.
193 
194 ### Using Boost.Hana instead of the code generator
195 The same example as above. However, this time Boost.Hana is used - so it doesn't require invoking the generator.
196 
197 <pre>
198 #include "<reflective_rapidjson/json/serializable-boosthana.h>
199 
200 // define structures using BOOST_HANA_DEFINE_STRUCT, eg.
201 struct TestObject : public JsonSerializable<TestObject> {
202  BOOST_HANA_DEFINE_STRUCT(TestObject,
203  (int, number),
204  (double, number2),
205  (vector<int>, numbers),
206  (string, text),
207  (bool, boolean)
208  );
209 };
210 struct NestingObject : public JsonSerializable<NestingObject> {
211  BOOST_HANA_DEFINE_STRUCT(NestingObject,
212  (string, name),
213  (TestObject, testObj)
214  );
215 };
216 struct NestingArray : public JsonSerializable<NestingArray> {
217  BOOST_HANA_DEFINE_STRUCT(NestingArray,
218  (string, name),
219  (vector<TestObject>, testObjects)
220  );
221 };
222 
223 // serialize to JSON
224 NestingArray obj{ ... };
225 cout << "JSON: " << obj.toJson().GetString();
226 
227 // deserialize from JSON
228 const auto obj = NestingArray::fromJson(...);
229 </pre>
230 
231 So beside the `BOOST_HANA_DEFINE_STRUCT` macro, the usage remains the same.
232 
233 #### Disadvantages
234 * Use of ugly macro required
235 * No context information for errors like type-mismatch available
236 * Inherited members not considered
237 * Proper support for enums is unlikely
238 
239 ### Enable reflection for 3rd party classes/structs
240 It is obvious that the previously shown examples do not work for classes
241 defined in 3rd party header files as it requires adding an additional
242 base class.
243 
244 To work around this issue, one can use the `REFLECTIVE_RAPIDJSON_MAKE_JSON_SERIALIZABLE`
245 macro. It will enable the `toJson` and `fromJson` methods for the specified class
246 in the `ReflectiveRapidJSON::JsonReflector` namespace:
247 
248 <pre>
249 // somewhere in included header
250 struct ThridPartyStruct
251 { ... };
252 
253 // somewhere in own header or source file
254 REFLECTIVE_RAPIDJSON_MAKE_JSON_SERIALIZABLE(ThridPartyStruct)
255 
256 // (de)serialization
257 ReflectiveRapidJSON::JsonReflector::toJson(...).GetString();
258 ReflectiveRapidJSON::JsonReflector::fromJson<ThridPartyStruct>("...");
259 </pre>
260 
261 The code generator will emit the code in the same way as if `JsonSerializable` was
262 used.
263 
264 By the way, the functions in the `ReflectiveRapidJSON::JsonReflector` namespace can also
265 be used when inheriting from `JsonSerializable` (instead of the member functions).
266 
267 ### (De)serializing private members
268 By default, private members are not considered for (de)serialization. However, it is possible
269 to enable this by adding `friend` methods for the helper functions of Reflective RapidJSON.
270 
271 To make things easier, there's a macro provided:
272 <pre>
273 struct SomeStruct : public JsonSerializable<SomeStruct> {
274  REFLECTIVE_RAPIDJSON_ENABLE_PRIVATE_MEMBERS(SomeStruct);
275 
276 public:
277  std::string publicMember = "will be (de)serialized anyways";
278 
279 private:
280  std::string privateMember = "will be (de)serialized with the help of REFLECTIVE_RAPIDJSON_ENABLE_PRIVATE_MEMBERS macro";
281 };
282 </pre>
283 
284 #### Caveats
285 * It will obviously not work for 3rd party structs.
286 * This way to allow (de)serialization of private members must be applied when using Boost.Hana
287  and there are any private members present. The reason is that accessing the private members can
288  currently not prevented when using Boost.Hana.
289 
290 ### Custom (de)serialization
291 Sometimes it is appropriate to implement custom (de)serialization. For instance, a
292 custom object representing a time value should likey be serialized as a string rather
293 than an object containing the internal structure.
294 
295 An example for such custom (de)serialization can be found in the file
296 `json/reflector-chronoutilities.h`. It provides (de)serialization of `DateTime` and
297 `TimeSpan` objects from the C++ utilities library mentioned under dependencies.
298 
299 ### Remarks
300 * Static member variables and member functions are currently ignored by the generator.
301 * It is currently not possible to ignore a specific member variable.
302 
303 ### Further examples
304 * Checkout the test cases for further examples. Relevant files are in
305  the directories `lib/tests` and `generator/tests`.
306 * There's also my
307  [tag editor](https://github.com/Martchus/tageditor), which uses Reflective RapidJSON to provide
308  a JSON export.
309  See [json.h](https://github.com/Martchus/tageditor/blob/master/cli/json.h) and
310  [mainfeatures.cpp#exportToJson](https://github.com/Martchus/tageditor/blob/master/cli/mainfeatures.cpp#L856).
311 
312 ## Architecture
313 The following diagram gives an overview about the architecture of the code generator and wrapper library
314 around RapidJSON:
315 
316 ![Architectue overview](/doc/arch.svg)
317 
318 * blue: classes from LibTooling/Clang
319 * grey: conceivable extension or use
320 
321 ## Install instructions
322 
323 ### Dependencies
324 The following dependencies are required at build time. Note that Reflective RapidJSON itself
325 and *none* of these dependencies are required at runtime by an application which makes use of
326 Reflective RapidJSON.
327 
328 * C++ compiler and C++ standard library supporting at least C++14
329 * the [CMake](https://cmake.org) build system
330 * LibTooling from [Clang](https://clang.llvm.org) for the code generator (optional when using
331  Boost.Hana)
332 * [RapidJSON](https://github.com/Tencent/rapidjson) for JSON (de)serialization
333 * [C++ utilities](https://github.com/Martchus/cpp-utilities) for various helper functions
334 
335 #### Optional
336 * [Boost.Hana](http://www.boost.org/doc/libs/1_65_1/libs/hana/doc/html/index.html) for using
337  `BOOST_HANA_DEFINE_STRUCT` instead of code generator
338 * [CppUnit](https://www.freedesktop.org/wiki/Software/cppunit) for building and running the tests
339 * [Doxygen](http://www.doxygen.org) for generating API documentation
340 * [Graphviz](http://www.graphviz.org) for diagrams in the API documentation
341 
342 #### Remarks
343 * It is not required to use CMake as build system for your own project. However, when using a
344  different build system, there is no helper for adding the code generator to the build process
345  provided (so far).
346 * I usually develop using the latest version of those dependencies. So it is recommend to get the
347  the latest versions as well. I tested the following versions so far:
348  * GCC 7.2.1/7.3.0 or Clang 5.0 as compiler
349  * libstdc++ from GCC 7.2.1
350  * CMake 3.10.1
351  * Clang 5.0.0/5.0.1 for LibTooling
352  * RapidJSON 1.1.0
353  * C++ utilities 4.12
354  * Boost.Hana 1.65.1 and 1.66.0
355  * CppUnit 1.14.0
356  * Doxygen 1.8.13
357  * Graphviz 2.40.1
358 
359 ### How to build
360 #### 1. Install dependencies
361 Install all required dependencies. Under a typical GNU/Linux system most of these dependencies
362 can be installed via the package manager. Otherwise follow the links in the "Dependencies" section
363 above.
364 
365 C++ utilities is likely not available as package. However, it is possible to build C++ utilities
366 together with `reflective-rapidjson` to simplify the build process. The following build script makes
367 use of this. (To use system C++ utilities, just skip any lines with "`c++utilities`" in the following
368 examples.)
369 
370 #### 2. Make dependencies available
371 
372 When installing (some) of the dependencies at custom locations, it is likely neccassary to tell
373 CMake where to find them. If you installed everything using packages provided by the system,
374 you can skip this step of course.
375 
376 To specify custom locations, just set some environment variables before invoking CMake. This
377 can likely be done in your IDE settings and of course at command line. Here is a Bash example:
378 <pre>
379 export PATH=$CUSTOM_INSTALL_PREFIX/bin:$PATH
380 export CMAKE_PREFIX_PATH=$CUSTOM_INSTALL_PREFIX:$CMAKE_PREFIX_PATH
381 export CMAKE_LIBRARY_PATH=$CUSTOM_INSTALL_PREFIX/lib:$CMAKE_LIBRARY_PATH
382 export CMAKE_INCLUDE_PATH=$CUSTOM_INSTALL_PREFIX/include:$CMAKE_INCLUDE_PATH
383 </pre>
384 
385 There are also a lot of [useful variables](https://cmake.org/Wiki/CMake_Useful_Variables)
386 that can be specified as CMake arguments. It is also possible to create a
387 [toolchain file](https://cmake.org/cmake/help/v3.10/manual/cmake-toolchains.7.html).
388 
389 
390 #### 3. Get sources, eg. using Git:
391 <pre>
392 cd $SOURCES
393 git clone https://github.com/Martchus/cpp-utilities.git c++utilities
394 git clone https://github.com/Martchus/reflective-rapidjson.git
395 </pre>
396 
397 If you don't want to build the development version, just checkout the desired version tag.
398 
399 #### 4. Run the build script
400 Here is an example for building with GNU Make:
401 <pre>
402 cd $BUILD_DIR
403 # generate Makefile
404 cmake \
405  -DCMAKE_BUILD_TYPE:STRING=Release \
406  -DCMAKE_INSTALL_PREFIX:PATH="/final/install/prefix" \
407  -DBUNDLED_CPP_UTILITIES_PATH:PATH="$SOURCES/c++utilities" \
408  "$SOURCES/reflective-rapidjson"
409 # build library and generators
410 make
411 # build and run tests (optional, requires CppUnit)
412 make check
413 # build tests but do not run them (optional, requires CppUnit)
414 make tests
415 # generate API documentation (optional, reqquires Doxygen)
416 make apidoc
417 # install header files, libraries and generator
418 make install DESTDIR="/temporary/install/location"
419 </pre>
420 Add eg. `-j$(nproc)` to `make` arguments for using all cores.
421 
422 ### Packages
423 I currently only provide an
424 [Arch Linux package](https://github.com/Martchus/PKGBUILDs/blob/master/reflective-rapidjson/git/PKGBUILD)
425 for the current Git version. This package shows the required dependencies and commands to build
426 in a plain way. So it might be useful for making Reflective RapidJSON available under other platforms,
427 too.