mirror of
https://github.com/nlohmann/json.git
synced 2025-07-31 10:24:23 +03:00
Use template get instead of get in examples (#4039)
Co-authored-by: tusooa <tusooa@kazv.moe>
This commit is contained in:
@ -37,7 +37,7 @@ Copy of the JSON value, converted to `ValueType`
|
||||
??? example "Example: (1) Default-constructible type"
|
||||
|
||||
The example below shows how a `from_json` function can be implemented for a user-defined type. This function is
|
||||
called by the `adl_serializer` when `get<ns::person>()` is called.
|
||||
called by the `adl_serializer` when `template get<ns::person>()` is called.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/from_json__default_constructible.cpp"
|
||||
|
@ -53,7 +53,7 @@ The default value is `0`.
|
||||
const json j = Choice::first;
|
||||
|
||||
// normally invokes from_json parse function but with JSON_DISABLE_ENUM_SERIALIZATION defined, it does not
|
||||
Choice ch = j.get<Choice>();
|
||||
Choice ch = j.template get<Choice>();
|
||||
}
|
||||
```
|
||||
|
||||
@ -86,7 +86,7 @@ The default value is `0`.
|
||||
const json j = Choice::first;
|
||||
|
||||
// uses user-defined from_json function defined by macro
|
||||
Choice ch = j.get<Choice>();
|
||||
Choice ch = j.template get<Choice>();
|
||||
}
|
||||
```
|
||||
|
||||
@ -109,7 +109,7 @@ The default value is `0`.
|
||||
|
||||
void from_json(const json& j, Choice& ch)
|
||||
{
|
||||
auto value = j.get<std::string>();
|
||||
auto value = j.template get<std::string>();
|
||||
if (value == "first")
|
||||
{
|
||||
ch = Choice::first;
|
||||
@ -122,7 +122,7 @@ The default value is `0`.
|
||||
|
||||
void to_json(json& j, const Choice& ch)
|
||||
{
|
||||
auto value = j.get<std::string>();
|
||||
auto value = j.template get<std::string>();
|
||||
if (value == "first")
|
||||
{
|
||||
ch = Choice::first;
|
||||
@ -139,7 +139,7 @@ The default value is `0`.
|
||||
const json j = Choice::first;
|
||||
|
||||
// uses user-defined from_json function
|
||||
Choice ch = j.get<Choice>();
|
||||
Choice ch = j.template get<Choice>();
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -46,7 +46,7 @@ By default, implicit conversions are enabled.
|
||||
|
||||
```cpp
|
||||
json j = "Hello, world!";
|
||||
auto s = j.get<std::string>();
|
||||
auto s = j.template get<std::string>();
|
||||
```
|
||||
|
||||
## See also
|
||||
|
@ -37,7 +37,7 @@ inline void from_json(const BasicJsonType& j, type& e);
|
||||
|
||||
!!! important "Important notes"
|
||||
|
||||
- When using [`get<ENUM_TYPE>()`](../basic_json/get.md), undefined JSON values will default to the first specified
|
||||
- When using [`template get<ENUM_TYPE>()`](../basic_json/get.md), undefined JSON values will default to the first specified
|
||||
conversion. Select this default pair carefully. See example 1 below.
|
||||
- If an enum or JSON value is specified in multiple conversions, the first matching conversion from the top of the
|
||||
list will be returned when converting to or from JSON. See example 2 below.
|
||||
|
@ -24,9 +24,9 @@ j["age"] = p.age;
|
||||
|
||||
// convert from JSON: copy each value from the JSON object
|
||||
ns::person p {
|
||||
j["name"].get<std::string>(),
|
||||
j["address"].get<std::string>(),
|
||||
j["age"].get<int>()
|
||||
j["name"].template get<std::string>(),
|
||||
j["address"].template get<std::string>(),
|
||||
j["age"].template get<int>()
|
||||
};
|
||||
```
|
||||
|
||||
@ -43,7 +43,7 @@ std::cout << j << std::endl;
|
||||
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
|
||||
|
||||
// conversion: json -> person
|
||||
auto p2 = j.get<ns::person>();
|
||||
auto p2 = j.template get<ns::person>();
|
||||
|
||||
// that's it
|
||||
assert(p == p2);
|
||||
@ -70,13 +70,13 @@ namespace ns {
|
||||
```
|
||||
|
||||
That's all! When calling the `json` constructor with your type, your custom `to_json` method will be automatically called.
|
||||
Likewise, when calling `get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.
|
||||
Likewise, when calling `template get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.
|
||||
|
||||
Some important things:
|
||||
|
||||
* 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).
|
||||
* 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.
|
||||
* 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.)
|
||||
* 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.)
|
||||
* In function `from_json`, use function [`at()`](../api/basic_json/at.md) 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.
|
||||
* You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these.
|
||||
|
||||
@ -171,7 +171,7 @@ struct adl_serializer<boost::optional<T>> {
|
||||
if (j.is_null()) {
|
||||
opt = boost::none;
|
||||
} else {
|
||||
opt = j.get<T>(); // same as above, but with
|
||||
opt = j.template get<T>(); // same as above, but with
|
||||
// adl_serializer<T>::from_json
|
||||
}
|
||||
}
|
||||
@ -204,7 +204,7 @@ namespace nlohmann {
|
||||
// note: the return type is no longer 'void', and the method only takes
|
||||
// one argument
|
||||
static move_only_type from_json(const json& j) {
|
||||
return {j.get<int>()};
|
||||
return {j.template get<int>()};
|
||||
}
|
||||
|
||||
// Here's the catch! You must provide a to_json method! Otherwise, you
|
||||
@ -268,7 +268,7 @@ struct bad_serializer
|
||||
static void to_json(const BasicJsonType& j, T& value) {
|
||||
// this calls BasicJsonType::json_serializer<T>::from_json(j, value);
|
||||
// if BasicJsonType::json_serializer == bad_serializer ... oops!
|
||||
value = j.template get<T>(); // oops!
|
||||
value = j.template template get<T>(); // oops!
|
||||
}
|
||||
};
|
||||
```
|
||||
|
@ -36,11 +36,11 @@ assert(j == "stopped");
|
||||
|
||||
// json string to enum
|
||||
json j3 = "running";
|
||||
assert(j3.get<TaskState>() == TS_RUNNING);
|
||||
assert(j3.template get<TaskState>() == TS_RUNNING);
|
||||
|
||||
// undefined json value to enum (where the first map entry above is the default)
|
||||
json jPi = 3.14;
|
||||
assert(jPi.get<TaskState>() == TS_INVALID );
|
||||
assert(jPi.template get<TaskState>() == TS_INVALID );
|
||||
```
|
||||
|
||||
## Notes
|
||||
@ -54,7 +54,7 @@ Just as in [Arbitrary Type Conversions](arbitrary_types.md) above,
|
||||
|
||||
Other Important points:
|
||||
|
||||
- When using `get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this
|
||||
- When using `template get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this
|
||||
default pair carefully.
|
||||
- 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.
|
||||
|
@ -235,9 +235,9 @@ integers, and between integers and floating-point values to integers. This beha
|
||||
!!! warning "Unconditional number conversions"
|
||||
|
||||
```cpp hl_lines="3"
|
||||
double d = 42.3; // non-integer double value 42.3
|
||||
json jd = d; // stores double value 42.3
|
||||
std::int64_t i = jd.get<std::int64_t>(); // now i==42; no warning or error is produced
|
||||
double d = 42.3; // non-integer double value 42.3
|
||||
json jd = d; // stores double value 42.3
|
||||
std::int64_t i = jd.template get<std::int64_t>(); // now i==42; no warning or error is produced
|
||||
```
|
||||
|
||||
Note the last line with throw a [`json.exception.type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302)
|
||||
@ -259,7 +259,7 @@ The rationale is twofold:
|
||||
if (jd.is_number_integer())
|
||||
{
|
||||
// if so, do the conversion and use i
|
||||
std::int64_t i = jd.get<std::int64_t>();
|
||||
std::int64_t i = jd.template get<std::int64_t>();
|
||||
// ...
|
||||
}
|
||||
else
|
||||
|
@ -187,7 +187,7 @@ conversions with calls to [`get`](../api/basic_json/get.md), [`get_to`](../api/b
|
||||
|
||||
```cpp
|
||||
nlohmann::json j = "Hello, world!";
|
||||
auto s = j.get<std::string>();
|
||||
auto s = j.template get<std::string>();
|
||||
```
|
||||
|
||||
=== "Future-proof (alternative)"
|
||||
|
Reference in New Issue
Block a user