Skip to content

Commit cda9c1e

Browse files
authored
Use get instead of template get in REAMD.md and docs in non-template context (#4846)
Signed-off-by: Andy Choi <[email protected]>
1 parent a6a92c1 commit cda9c1e

23 files changed

+81
-81
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ Note the difference between serialization and assignment:
345345
json j_string = "this is a string";
346346

347347
// retrieve the string value
348-
auto cpp_string = j_string.template get<std::string>();
348+
auto cpp_string = j_string.get<std::string>();
349349
// retrieve the string value (alternative when a variable already exists)
350350
std::string cpp_string2;
351351
j_string.get_to(cpp_string2);
@@ -354,7 +354,7 @@ j_string.get_to(cpp_string2);
354354
std::string serialized_string = j_string.dump();
355355

356356
// output of original string
357-
std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.template get<std::string>() << '\n';
357+
std::cout << cpp_string << " == " << cpp_string2 << " == " << j_string.get<std::string>() << '\n';
358358
// output of serialized value
359359
std::cout << j_string << " == " << serialized_string << std::endl;
360360
```
@@ -527,7 +527,7 @@ for (auto& element : j) {
527527
}
528528
529529
// getter/setter
530-
const auto tmp = j[0].template get<std::string>();
530+
const auto tmp = j[0].get<std::string>();
531531
j[1] = 42;
532532
bool foo = j.at(2);
533533
@@ -734,7 +734,7 @@ You can switch off implicit conversions by defining `JSON_USE_IMPLICIT_CONVERSIO
734734
// strings
735735
std::string s1 = "Hello, world!";
736736
json js = s1;
737-
auto s2 = js.template get<std::string>();
737+
auto s2 = js.get<std::string>();
738738
// NOT RECOMMENDED
739739
std::string s3 = js;
740740
std::string s4;
@@ -743,7 +743,7 @@ s4 = js;
743743
// Booleans
744744
bool b1 = true;
745745
json jb = b1;
746-
auto b2 = jb.template get<bool>();
746+
auto b2 = jb.get<bool>();
747747
// NOT RECOMMENDED
748748
bool b3 = jb;
749749
bool b4;
@@ -752,7 +752,7 @@ b4 = jb;
752752
// numbers
753753
int i = 42;
754754
json jn = i;
755-
auto f = jn.template get<double>();
755+
auto f = jn.get<double>();
756756
// NOT RECOMMENDED
757757
double f2 = jb;
758758
double f3;
@@ -795,9 +795,9 @@ j["age"] = p.age;
795795

796796
// convert from JSON: copy each value from the JSON object
797797
ns::person p {
798-
j["name"].template get<std::string>(),
799-
j["address"].template get<std::string>(),
800-
j["age"].template get<int>()
798+
j["name"].get<std::string>(),
799+
j["address"].get<std::string>(),
800+
j["age"].get<int>()
801801
};
802802
```
803803
@@ -814,7 +814,7 @@ std::cout << j << std::endl;
814814
// {"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}
815815
816816
// conversion: json -> person
817-
auto p2 = j.template get<ns::person>();
817+
auto p2 = j.get<ns::person>();
818818
819819
// that's it
820820
assert(p == p2);
@@ -841,13 +841,13 @@ namespace ns {
841841
```
842842

843843
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.
845845

846846
Some important things:
847847

848848
- 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).
849849
- 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.)
851851
- 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.
852852
- You do not need to add serializers or deserializers for STL types like `std::vector`: the library already implements these.
853853

@@ -927,8 +927,8 @@ namespace nlohmann {
927927
if (j.is_null()) {
928928
opt = boost::none;
929929
} 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
932932
}
933933
}
934934
};
@@ -955,7 +955,7 @@ namespace nlohmann {
955955
// note: the return type is no longer 'void', and the method only takes
956956
// one argument
957957
static move_only_type from_json(const json& j) {
958-
return {j.template get<int>()};
958+
return {j.get<int>()};
959959
}
960960

961961
// Here's the catch! You must provide a to_json method! Otherwise, you
@@ -1019,7 +1019,7 @@ struct bad_serializer
10191019
static void to_json(const BasicJsonType& j, T& value) {
10201020
// this calls BasicJsonType::json_serializer<T>::from_json(j, value)
10211021
// if BasicJsonType::json_serializer == bad_serializer ... oops!
1022-
value = j.template get<T>(); // oops!
1022+
value = j.get<T>(); // oops!
10231023
}
10241024
};
10251025
```
@@ -1059,11 +1059,11 @@ assert(j == "stopped");
10591059
10601060
// json string to enum
10611061
json j3 = "running";
1062-
assert(j3.template get<TaskState>() == TS_RUNNING);
1062+
assert(j3.get<TaskState>() == TS_RUNNING);
10631063
10641064
// undefined json value to enum (where the first map entry above is the default)
10651065
json jPi = 3.14;
1066-
assert(jPi.template get<TaskState>() == TS_INVALID);
1066+
assert(jPi.get<TaskState>() == TS_INVALID);
10671067
```
10681068

10691069
Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
@@ -1073,7 +1073,7 @@ Just as in [Arbitrary Type Conversions](#arbitrary-types-conversions) above,
10731073

10741074
Other Important points:
10751075

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.
10771077
- 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.
10781078

10791079
### Binary formats (BSON, CBOR, MessagePack, UBJSON, and BJData)

docs/mkdocs/docs/api/adl_serializer/from_json.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Copy of the JSON value, converted to `ValueType`
3737
??? example "Example: (1) Default-constructible type"
3838
3939
The example below shows how a `from_json` function can be implemented for a user-defined type. This function is
40-
called by the `adl_serializer` when `template get<ns::person>()` is called.
40+
called by the `adl_serializer` when `get<ns::person>()` is called.
4141
4242
```cpp
4343
--8<-- "examples/from_json__default_constructible.cpp"

docs/mkdocs/docs/api/macros/json_disable_enum_serialization.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ The default value is `0`.
5353
const json j = Choice::first;
5454

5555
// normally invokes from_json parse function but with JSON_DISABLE_ENUM_SERIALIZATION defined, it does not
56-
Choice ch = j.template get<Choice>();
56+
Choice ch = j.get<Choice>();
5757
}
5858
```
5959

@@ -86,7 +86,7 @@ The default value is `0`.
8686
const json j = Choice::first;
8787

8888
// uses user-defined from_json function defined by macro
89-
Choice ch = j.template get<Choice>();
89+
Choice ch = j.get<Choice>();
9090
}
9191
```
9292

@@ -109,7 +109,7 @@ The default value is `0`.
109109

110110
void from_json(const json& j, Choice& ch)
111111
{
112-
auto value = j.template get<std::string>();
112+
auto value = j.get<std::string>();
113113
if (value == "first")
114114
{
115115
ch = Choice::first;
@@ -122,7 +122,7 @@ The default value is `0`.
122122

123123
void to_json(json& j, const Choice& ch)
124124
{
125-
auto value = j.template get<std::string>();
125+
auto value = j.get<std::string>();
126126
if (value == "first")
127127
{
128128
ch = Choice::first;
@@ -139,7 +139,7 @@ The default value is `0`.
139139
const json j = Choice::first;
140140

141141
// uses user-defined from_json function
142-
Choice ch = j.template get<Choice>();
142+
Choice ch = j.get<Choice>();
143143
}
144144
```
145145

docs/mkdocs/docs/api/macros/json_use_implicit_conversions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ By default, implicit conversions are enabled.
4646

4747
```cpp
4848
json j = "Hello, world!";
49-
auto s = j.template get<std::string>();
49+
auto s = j.get<std::string>();
5050
```
5151

5252
## See also

docs/mkdocs/docs/api/macros/nlohmann_json_serialize_enum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ inline void from_json(const BasicJsonType& j, type& e);
3737
3838
!!! important "Important notes"
3939
40-
- When using [`template get<ENUM_TYPE>()`](../basic_json/get.md), undefined JSON values will default to the first specified
40+
- When using [`get<ENUM_TYPE>()`](../basic_json/get.md), undefined JSON values will default to the first specified
4141
conversion. Select this default pair carefully. See example 1 below.
4242
- If an enum or JSON value is specified in multiple conversions, the first matching conversion from the top of the
4343
list will be returned when converting to or from JSON. See example 2 below.

docs/mkdocs/docs/examples/from_json__default_constructible.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int main()
3131
j["address"] = "744 Evergreen Terrace";
3232
j["age"] = 60;
3333

34-
auto p = j.template get<ns::person>();
34+
auto p = j.get<ns::person>();
3535

3636
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
3737
}

docs/mkdocs/docs/examples/from_json__non_default_constructible.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main()
4747
j["address"] = "744 Evergreen Terrace";
4848
j["age"] = 60;
4949

50-
auto p = j.template get<ns::person>();
50+
auto p = j.get<ns::person>();
5151

5252
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
5353
}

docs/mkdocs/docs/examples/get__PointerType.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ int main()
99
json value = 17;
1010

1111
// explicitly getting pointers
12-
auto p1 = value.template get<const json::number_integer_t*>();
13-
auto p2 = value.template get<json::number_integer_t*>();
14-
auto p3 = value.template get<json::number_integer_t* const>();
15-
auto p4 = value.template get<const json::number_integer_t* const>();
16-
auto p5 = value.template get<json::number_float_t*>();
12+
auto p1 = value.get<const json::number_integer_t*>();
13+
auto p2 = value.get<json::number_integer_t*>();
14+
auto p3 = value.get<json::number_integer_t* const>();
15+
auto p4 = value.get<const json::number_integer_t* const>();
16+
auto p5 = value.get<json::number_float_t*>();
1717

1818
// print the pointees
1919
std::cout << *p1 << ' ' << *p2 << ' ' << *p3 << ' ' << *p4 << '\n';

docs/mkdocs/docs/examples/get__ValueType_const.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ int main()
2222
};
2323

2424
// use explicit conversions
25-
auto v1 = json_types["boolean"].template get<bool>();
26-
auto v2 = json_types["number"]["integer"].template get<int>();
27-
auto v3 = json_types["number"]["integer"].template get<short>();
28-
auto v4 = json_types["number"]["floating-point"].template get<float>();
29-
auto v5 = json_types["number"]["floating-point"].template get<int>();
30-
auto v6 = json_types["string"].template get<std::string>();
31-
auto v7 = json_types["array"].template get<std::vector<short>>();
32-
auto v8 = json_types.template get<std::unordered_map<std::string, json>>();
25+
auto v1 = json_types["boolean"].get<bool>();
26+
auto v2 = json_types["number"]["integer"].get<int>();
27+
auto v3 = json_types["number"]["integer"].get<short>();
28+
auto v4 = json_types["number"]["floating-point"].get<float>();
29+
auto v5 = json_types["number"]["floating-point"].get<int>();
30+
auto v6 = json_types["string"].get<std::string>();
31+
auto v7 = json_types["array"].get<std::vector<short>>();
32+
auto v8 = json_types.get<std::unordered_map<std::string, json>>();
3333

3434
// print the conversion results
3535
std::cout << v1 << '\n';

docs/mkdocs/docs/examples/nlohmann_define_type_intrusive_explicit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ int main()
4747

4848
// deserialization: json -> person
4949
json j2 = R"({"address": "742 Evergreen Terrace", "age": 40, "name": "Homer Simpson"})"_json;
50-
auto p2 = j2.template get<ns::person>();
50+
auto p2 = j2.get<ns::person>();
5151

5252
// incomplete deserialization:
5353
json j3 = R"({"address": "742 Evergreen Terrace", "name": "Maggie Simpson"})"_json;
5454
try
5555
{
56-
auto p3 = j3.template get<ns::person>();
56+
auto p3 = j3.get<ns::person>();
5757
}
5858
catch (const json::exception& e)
5959
{

0 commit comments

Comments
 (0)