You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That's all! When calling the `json` constructor with your type, your custom `to_json` method will be automatically called.
844
-
Likewise, when calling `template get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.
844
+
Likewise, when calling `get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.
845
845
846
846
Some important things:
847
847
848
848
- 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).
849
849
- Those methods **MUST** be available (e.g., proper headers must be included) everywhere you use these conversions. Look at [issue 1108](https://github.com/nlohmann/json/issues/1108) for errors that may occur otherwise.
850
-
- When using `template get<your_type>()`, `your_type`**MUST** be [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). (There is a way to bypass this requirement described later.)
850
+
- When using `get<your_type>()`, `your_type`**MUST** be [DefaultConstructible](https://en.cppreference.com/w/cpp/named_req/DefaultConstructible). (There is a way to bypass this requirement described later.)
851
851
- In function `from_json`, use function [`at()`](https://json.nlohmann.me/api/basic_json/at/) to access the object values rather than `operator[]`. In case a key does not exist, `at` throws an exception that you can handle, whereas `operator[]` exhibits undefined behavior.
852
852
- You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these.
853
853
@@ -927,8 +927,8 @@ namespace nlohmann {
927
927
if (j.is_null()) {
928
928
opt = boost::none;
929
929
} else {
930
-
opt = j.template get<T>(); // same as above, but with
931
-
// adl_serializer<T>::from_json
930
+
opt = j.get<T>(); // same as above, but with
931
+
// adl_serializer<T>::from_json
932
932
}
933
933
}
934
934
};
@@ -955,7 +955,7 @@ namespace nlohmann {
955
955
// note: the return type is no longer 'void', and the method only takes
956
956
// one argument
957
957
static move_only_type from_json(const json& j) {
958
-
return {j.template get<int>()};
958
+
return {j.get<int>()};
959
959
}
960
960
961
961
// Here's the catch! You must provide a to_json method! Otherwise, you
Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
@@ -1073,7 +1073,7 @@ Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
1073
1073
1074
1074
Other Important points:
1075
1075
1076
-
- When using `template get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this default pair carefully.
1076
+
- When using `get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this default pair carefully.
1077
1077
- If an enum or JSON value is specified more than once in your map, the first matching occurrence from the top of the map will be returned when converting to or from JSON.
1078
1078
1079
1079
### Binary formats (BSON, CBOR, MessagePack, UBJSON, and BJData)
0 commit comments