diff --git a/README.md b/README.md index 33d7518de..8cf4865b3 100644 --- a/README.md +++ b/README.md @@ -475,7 +475,7 @@ 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... Fortunately, there's a better way: ```cpp // create a person @@ -496,17 +496,17 @@ assert(p == p2); #### Basic usage -To make this work with one of your types, you only need to provide two methods: +To make this work with one of your types, you only need to provide two functions: ```cpp using nlohmann::json; namespace ns { - void to_json(json& j, person const& p) { + void to_json(json& j, const person& p) { j = json{{"name", p.name}, {"address", p.address}, {"age", p.age}}; } - void from_json(json const& j, person& p) { + void from_json(const json& j, person& p) { p.name = j["name"].get(); p.address = j["address"].get(); p.age = j["age"].get(); @@ -519,13 +519,12 @@ Likewise, when calling `get()`, the `from_json` method will be called Some important things: -* Those methods **MUST** be in your type's namespace, or the library will not be able to locate them (in this example, they are in namespace `ns`, where `person` is defined). -* When using `get()`, `your_type` **MUST** be [DefaultConstructible](http://en.cppreference.com/w/cpp/concept/DefaultConstructible) and [CopyConstructible](http://en.cppreference.com/w/cpp/concept/CopyConstructible). (There is a way to bypass those requirements described later.) +* Those methods **MUST** be in your type's namespace (which can be the global namespace), or the library will not be able to locate them (in this example, they are in namespace `ns`, where `person` is defined). +* When using `get()`, `your_type` **MUST** be [DefaultConstructible](http://en.cppreference.com/w/cpp/concept/DefaultConstructible). (There is a way to bypass those requirements described later.) #### How do I convert third-party types? -This requires a bit more advanced technique. -But first, let's see how this conversion mechanism works: +This requires a bit more advanced technique. But first, let's see how this conversion mechanism works: The library uses **JSON Serializers** to convert types to json. The default serializer for `nlohmann::json` is `nlohmann::adl_serializer` (ADL means [Argument-Dependent Lookup](http://en.cppreference.com/w/cpp/language/adl)). diff --git a/src/json.hpp b/src/json.hpp index f7af945aa..13e7b07d0 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -239,9 +239,6 @@ template<> struct priority_tag<0> {}; // constructors // ////////////////// -// This is an experiment. I need this to move constructors out of basic_json. -// I'm sure there is a better way, but this might need a big basic_json -// refactoring template struct external_constructor; template<> diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 039fc9fb8..45e1ba58f 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -239,9 +239,6 @@ template<> struct priority_tag<0> {}; // constructors // ////////////////// -// This is an experiment. I need this to move constructors out of basic_json. -// I'm sure there is a better way, but this might need a big basic_json -// refactoring template struct external_constructor; template<>