mirror of
https://github.com/nlohmann/json.git
synced 2026-02-17 09:03:58 +00:00
Use get instead of template get in REAMD.md and docs in non-template context (#4846)
Signed-off-by: Andy Choi <ccpong516@gmail.com>
This commit is contained in:
38
README.md
38
README.md
@@ -345,7 +345,7 @@ Note the difference between serialization and assignment:
|
||||
json j_string = "this is a string";
|
||||
|
||||
// retrieve the string value
|
||||
auto cpp_string = j_string.template get<std::string>();
|
||||
auto cpp_string = j_string.get<std::string>();
|
||||
// retrieve the string value (alternative when a variable already exists)
|
||||
std::string cpp_string2;
|
||||
j_string.get_to(cpp_string2);
|
||||
@@ -354,7 +354,7 @@ j_string.get_to(cpp_string2);
|
||||
std::string serialized_string = j_string.dump();
|
||||
|
||||
// output of original string
|
||||
std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.template get<std::string>() << '\n';
|
||||
std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.get<std::string>() << '\n';
|
||||
// output of serialized value
|
||||
std::cout << j_string << " == " << serialized_string << std::endl;
|
||||
```
|
||||
@@ -527,7 +527,7 @@ for (auto& element : j) {
|
||||
}
|
||||
|
||||
// getter/setter
|
||||
const auto tmp = j[0].template get<std::string>();
|
||||
const auto tmp = j[0].get<std::string>();
|
||||
j[1] = 42;
|
||||
bool foo = j.at(2);
|
||||
|
||||
@@ -734,7 +734,7 @@ You can switch off implicit conversions by defining `JSON_USE_IMPLICIT_CONVERSIO
|
||||
// strings
|
||||
std::string s1 = "Hello, world!";
|
||||
json js = s1;
|
||||
auto s2 = js.template get<std::string>();
|
||||
auto s2 = js.get<std::string>();
|
||||
// NOT RECOMMENDED
|
||||
std::string s3 = js;
|
||||
std::string s4;
|
||||
@@ -743,7 +743,7 @@ s4 = js;
|
||||
// Booleans
|
||||
bool b1 = true;
|
||||
json jb = b1;
|
||||
auto b2 = jb.template get<bool>();
|
||||
auto b2 = jb.get<bool>();
|
||||
// NOT RECOMMENDED
|
||||
bool b3 = jb;
|
||||
bool b4;
|
||||
@@ -752,7 +752,7 @@ b4 = jb;
|
||||
// numbers
|
||||
int i = 42;
|
||||
json jn = i;
|
||||
auto f = jn.template get<double>();
|
||||
auto f = jn.get<double>();
|
||||
// NOT RECOMMENDED
|
||||
double f2 = jb;
|
||||
double f3;
|
||||
@@ -795,9 +795,9 @@ j["age"] = p.age;
|
||||
|
||||
// convert from JSON: copy each value from the JSON object
|
||||
ns::person p {
|
||||
j["name"].template get<std::string>(),
|
||||
j["address"].template get<std::string>(),
|
||||
j["age"].template get<int>()
|
||||
j["name"].get<std::string>(),
|
||||
j["address"].get<std::string>(),
|
||||
j["age"].get<int>()
|
||||
};
|
||||
```
|
||||
|
||||
@@ -814,7 +814,7 @@ std::cout << j << std::endl;
|
||||
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
|
||||
|
||||
// conversion: json -> person
|
||||
auto p2 = j.template get<ns::person>();
|
||||
auto p2 = j.get<ns::person>();
|
||||
|
||||
// that's it
|
||||
assert(p == p2);
|
||||
@@ -841,13 +841,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 `template get<your_type>()` or `get_to(your_type&)`, the `from_json` method will be called.
|
||||
Likewise, when calling `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 `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.)
|
||||
- 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.)
|
||||
- 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.
|
||||
- You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these.
|
||||
|
||||
@@ -927,8 +927,8 @@ namespace nlohmann {
|
||||
if (j.is_null()) {
|
||||
opt = boost::none;
|
||||
} else {
|
||||
opt = j.template get<T>(); // same as above, but with
|
||||
// adl_serializer<T>::from_json
|
||||
opt = j.get<T>(); // same as above, but with
|
||||
// adl_serializer<T>::from_json
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -955,7 +955,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.template get<int>()};
|
||||
return {j.get<int>()};
|
||||
}
|
||||
|
||||
// Here's the catch! You must provide a to_json method! Otherwise, you
|
||||
@@ -1019,7 +1019,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.get<T>(); // oops!
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -1059,11 +1059,11 @@ assert(j == "stopped");
|
||||
|
||||
// json string to enum
|
||||
json j3 = "running";
|
||||
assert(j3.template get<TaskState>() == TS_RUNNING);
|
||||
assert(j3.get<TaskState>() == TS_RUNNING);
|
||||
|
||||
// undefined json value to enum (where the first map entry above is the default)
|
||||
json jPi = 3.14;
|
||||
assert(jPi.template get<TaskState>() == TS_INVALID);
|
||||
assert(jPi.get<TaskState>() == TS_INVALID);
|
||||
```
|
||||
|
||||
Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
|
||||
@@ -1073,7 +1073,7 @@ Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
|
||||
|
||||
Other Important points:
|
||||
|
||||
- When using `template get<ENUM_TYPE>()`, undefined JSON values will default to the first pair specified in your map. Select this default pair carefully.
|
||||
- When using `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.
|
||||
|
||||
### Binary formats (BSON, CBOR, MessagePack, UBJSON, and BJData)
|
||||
|
||||
Reference in New Issue
Block a user