Extend README.md

This commit is contained in:
Martchus 2017-10-25 20:03:58 +02:00
parent 646edfc16d
commit d4662164fc
1 changed files with 84 additions and 7 deletions

View File

@ -13,26 +13,103 @@ generator.
## Usage
This example shows how the library can be used to make a `struct` serializable:
```
TODO
#include "<reflective-rapidjson/jsonserializable.h>
// define structures, eg.
struct TestObject : public JSONSerializable<TestObject> {
int number;
double number2;
vector<int> numbers;
string text;
bool boolean;
};
struct NestingObject : public JSONSerializable<NestingObject> {
string name;
TestObject testObj;
};
struct NestingArray : public JSONSerializable<NestingArray> {
string name;
vector<TestObject> testObjects;
};
// serialize to JSON
NestingArray obj{ ... };
cout << "JSON: " << obj.toJson().GetString();
// deserialize from JSON
const auto obj = NestingArray::fromJson(...);
// in exactly one of the project's translation units
#include "reflection/code-defining-structs.h"
```
Note that the header included at the bottom must be generated by invoking the code generator appropriately:
Note that the header included at the bottom must be generated by invoking the code generator appropriately, eg.:
```
TODO
reflective_rapidjson_generator -i "$srcdir/code-defining-structs.cpp" -o "$builddir/reflection/code-defining-structs.h"
```
It is possible to use the provided CMake macro to automate this task:
```
TODO
find_package(reflective-rapidjson REQUIRED)
list(APPEND CMAKE_MODULE_PATH ${REFLECTIVE_RAPIDJSON_MODULE_DIRS})
include(ReflectionGenerator)
add_reflection_generator_invocation(
INPUT_FILES code-defining-structs.cpp
GENERATORS json
OUTPUT_LISTS LIST_OF_GENERATED_HEADERS
)
```
### Using Boost.Hana instead of code generator
The same example as above. However, this time Boost.Hana is used. So it doesn't required to invoking the generator.
This will produce the file `code-defining-structs.h` in the directory `reflection` in the current build directory. So
make sure the current build directory is added to the include directories of your target. The default output directory can
also be overridden by passing `OUTPUT_DIRECTORY custom/directory` to the arguments.
It is possible to specify multiple input files at once. A separate output file is generated for each input. The output files
will always have the extension "`.h`", independently of the extension of the input file.
The full paths of the generated files are also appended to the variable `LIST_OF_GENERATED_HEADERS` which then can be added
to the sources of your target. Of course this can be skipped if not required/wanted.
### Using Boost.Hana instead of the code generator
The same example as above. However, this time Boost.Hana is used - so it doesn't require invoking the generator.
```
TODO
#include "<reflective-rapidjson/jsonserializable-boosthana.h>
// define structures using BOOST_HANA_DEFINE_STRUCT, eg.
struct TestObject : public JSONSerializable<TestObject> {
BOOST_HANA_DEFINE_STRUCT(TestObject,
(int, number),
(double, number2),
(vector<int>, numbers),
(string, text),
(bool, boolean)
);
};
struct NestingObject : public JSONSerializable<NestingObject> {
BOOST_HANA_DEFINE_STRUCT(NestingObject,
(string, name),
(TestObject, testObj)
);
};
struct NestingArray : public JSONSerializable<NestingArray> {
BOOST_HANA_DEFINE_STRUCT(NestingArray,
(string, name),
(vector<TestObject>, testObjects)
);
};
// serialize to JSON
NestingArray obj{ ... };
cout << "JSON: " << obj.toJson().GetString();
// deserialize from JSON
const auto obj = NestingArray::fromJson(...);
```
So the usage remains the same.
## Install instructions
### Dependencies