@@ -449,6 +449,7 @@ Every type can be serialized in JSON, not just STL-containers and scalar types.
...
@@ -449,6 +449,7 @@ Every type can be serialized in JSON, not just STL-containers and scalar types.
```cpp
```cpp
namespacens{
namespacens{
// a simple struct to model a person
structperson{
structperson{
std::stringname;
std::stringname;
std::stringaddress;
std::stringaddress;
...
@@ -456,7 +457,7 @@ namespace ns {
...
@@ -456,7 +457,7 @@ namespace ns {
};
};
}
}
// convert to JSON
// convert to JSON: copy each value into the JSON object
jsonj;
jsonj;
ns::personp=createSomeone();
ns::personp=createSomeone();
j["name"]=p.name;
j["name"]=p.name;
...
@@ -465,7 +466,7 @@ j["age"] = p.age;
...
@@ -465,7 +466,7 @@ j["age"] = p.age;
// ...
// ...
// convert from JSON
// convert from JSON: copy each value from the JSON object
ns::personp{
ns::personp{
j["name"].get<std::string>(),
j["name"].get<std::string>(),
j["address"].get<std::string>(),
j["address"].get<std::string>(),
...
@@ -476,9 +477,11 @@ ns::person p {
...
@@ -476,9 +477,11 @@ ns::person p {
It works, but that's quite a lot of boilerplate... Hopefully, there's a better way:
It works, but that's quite a lot of boilerplate... Hopefully, there's a better way:
```cpp
```cpp
// person -> json
ns::personp=createPerson();
ns::personp=createPerson();
jsonj=p;
jsonj=p;
// json -> person
autop2=j.get<ns::person>();
autop2=j.get<ns::person>();
assert(p==p2);
assert(p==p2);
```
```
...
@@ -547,9 +550,8 @@ namespace nlohmann {
...
@@ -547,9 +550,8 @@ namespace nlohmann {
if(opt==boost::none){
if(opt==boost::none){
j=nullptr;
j=nullptr;
}else{
}else{
j=*opt;// this will call adl_serializer<T>::to_json
j=*opt;// this will call adl_serializer<T>::to_json which will
// which will find the free function to_json
// find the free function to_json in T's namespace!
// in T's namespace!
}
}
}
}
...
@@ -581,15 +583,15 @@ struct move_only_type {
...
@@ -581,15 +583,15 @@ struct move_only_type {
namespacenlohmann{
namespacenlohmann{
template<>
template<>
structadl_serializer<move_only_type>{
structadl_serializer<move_only_type>{
// note: the return type is no longer 'void',
// note: the return type is no longer 'void', and the method only takes
// and the method only takes one argument
// one argument
staticmove_only_typefrom_json(constjson&j){
staticmove_only_typefrom_json(constjson&j){
return{j.get<int>()};
return{j.get<int>()};
}
}
// Here's the catch! You must provide a to_json method!
// Here's the catch! You must provide a to_json method! Otherwise you
// Otherwise you will not be able to convert move_only_type to json,
// will not be able to convert move_only_type to json, since you fully
// since you fully specialized adl_serializer on that type
// specialized adl_serializer on that type
staticvoidto_json(json&j,move_only_typet){
staticvoidto_json(json&j,move_only_typet){
j=t.i;
j=t.i;
}
}
...
@@ -599,7 +601,7 @@ namespace nlohmann {
...
@@ -599,7 +601,7 @@ namespace nlohmann {
#### Can I write my own serializer? (Advanced use)
#### Can I write my own serializer? (Advanced use)
Yes. You might want to take a look at `[unit-udt.cpp](https://github.com/nlohmann/json/blob/develop/test/src/unit-udt.cpp)` in the test suite, to see a few examples.
Yes. You might want to take a look at [`unit-udt.cpp`](https://github.com/nlohmann/json/blob/develop/test/src/unit-udt.cpp) in the test suite, to see a few examples.
If you write your own serializer, you'll need to do a few things:
If you write your own serializer, you'll need to do a few things: