DAW JSON Link
|
Arbitrary C++ data structures are supported and can be described using the trait json_data_contract
.
Below is an ordinary JSON object
The above JSON describes a class with three members, a string named member0
, an integer named member1
, and a boolean named member2
Below is the C++ data structure and the trait to map the members to that of the JSON object. Note that the names of the C++ data members do not have to be the same as the JSON object's. To see a working example using this code, refer to cookbook_class1_test.cpp
The above json_data_contract
trait maps the JSON members to the constructor of MyClass1
in the order specified. The arguments of type std::string, int, bool
will be passed.
The serializing and deserializing is recursive. So if a class contains another class, the requirement is that that class has been mapped already. Assuming we already have the mapping above, lets embed that into another class
The JSON object that has a member "a"
that matches MyClass1
, and a second member is an unsigned
To see a working example using this code, refer to cookbook_class2_test.cpp
Not all the JSON objects members need to be mapped. Below is the same JSON object as in the MyClass2
example above. To see a working example using this code, refer to cookbook_class3_test.cpp
Only the "a"
member is mapped, the "b"
member of the JSON object is ignored.
If a type is already mapped or has many possible JSON mappings you can provide an alternate mapping. This is accomplished by mapping to the daw::json::json_alt<Type, Index>
type. The optional index allows for any number of mappings that fit into a std::size_t
.
To see a working example using this code, refer to cookbook_class_alternate1_test.cpp
If constructing the class requires extra steps than passing the parsed members to it's constructor one can provide a custom Constructor callable as the default for that type or for a specific json_class
mapping.
If one wants to change the default Constructor type of a specific type they can add a type alias in that classes json_data_contract
. For instance, if a member needs extra checking that the types constructor does not provide:
Here the type Foo
will use a callable of type FooConstructor
to create all Foo
's in the library unless overridden by a mapping.
The json_class
mapping's second parameter is that of a Constructor type to call to construct that class and override the default. It can be specified like