Skip to content

Commit 6fa5abc

Browse files
committed
Replace x.get<T>() with x.get(v) or T(x)
1 parent 1b1a122 commit 6fa5abc

File tree

12 files changed

+368
-415
lines changed

12 files changed

+368
-415
lines changed

benchmark/bench_dom_api.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static void iterator_twitter_default_profile(State& state) {
407407
set<string_view> default_users;
408408
ParsedJson::Iterator iter(pj);
409409

410-
// for (dom::object tweet : doc["statuses"].get<dom::array>()) {
410+
// for (dom::object tweet : doc["statuses"]) {
411411
if (!(iter.move_to_key("statuses") && iter.is_array())) { return; }
412412
if (iter.down()) { // first status
413413
do {
@@ -480,7 +480,7 @@ static void iterator_twitter_image_sizes(State& state) {
480480
set<tuple<uint64_t, uint64_t>> image_sizes;
481481
ParsedJson::Iterator iter(pj);
482482

483-
// for (dom::object tweet : doc["statuses"].get<dom::array>()) {
483+
// for (dom::object tweet : doc["statuses"]) {
484484
if (!(iter.move_to_key("statuses") && iter.is_array())) { return; }
485485
if (iter.down()) { // first status
486486
do {
@@ -492,7 +492,7 @@ static void iterator_twitter_image_sizes(State& state) {
492492
if (iter.move_to_key("media")) {
493493
if (!iter.is_array()) { return; }
494494

495-
// for (dom::object image : media.get<dom::array>()) {
495+
// for (dom::object image : media) {
496496
if (iter.down()) { // first media
497497
do {
498498

benchmark/distinctuseridcompetition.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ void print_vec(const std::vector<int64_t> &v) {
4040

4141
// simdjson_recurse below come be implemented like so but it is slow:
4242
/*void simdjson_recurse(std::vector<int64_t> & v, simdjson::dom::element element) {
43-
if (element.is<simdjson::dom::array>()) {
44-
auto [array, array_error] = element.get<simdjson::dom::array>();
43+
error_code error;
44+
if (element.is_array()) {
45+
dom::array array;
46+
error = element.get(array);
4547
for (auto child : array) {
4648
if (child.is<simdjson::dom::array>() || child.is<simdjson::dom::object>()) {
4749
simdjson_recurse(v, child);
4850
}
4951
}
50-
} else if (element.is<simdjson::dom::object>()) {
51-
auto [object, error] = element.get<simdjson::dom::object>();
52+
} else if (element.is_object()) {
5253
int64_t id;
53-
error = object["user"]["id"].get(id);
54+
error = element["user"]["id"].get(id);
5455
if(!error) {
5556
v.push_back(id);
5657
}

benchmark/parseandstatcompetition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ static void GenStatPlus(Stat &stat, const dom::element &v) {
154154
break;
155155
case dom::element_type::STRING: {
156156
stat.stringCount++;
157-
std::string_view sv = v.get<std::string_view>();
157+
auto sv = std::string_view(v);
158158
stat.stringLength += sv.size();
159159
} break;
160160
case dom::element_type::BOOL:
161-
if (v.get<bool>()) {
161+
if (bool(v)) {
162162
stat.trueCount++;
163163
} else {
164164
stat.falseCount++;

doc/basics.md

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ And another one:
164164
auto abstract_json = R"(
165165
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
166166
dom::parser parser;
167-
double v = parser.parse(abstract_json)["str"]["123"]["abc"].get<double>();
167+
double v = parser.parse(abstract_json)["str"]["123"]["abc"];
168168
cout << "number: " << v << endl;
169169
```
170170

@@ -191,14 +191,13 @@ Though it does not validate the JSON input, it will detect when the document end
191191
C++17 Support
192192
-------------
193193
194-
While the simdjson library can be used in any project using C++ 11 and above, it has special support
195-
for C++ 17. The APIs for field iteration and error handling in particular are designed to work
196-
nicely with C++17's destructuring syntax. For example:
194+
While the simdjson library can be used in any project using C++ 11 and above, field iteration has special support C++ 17's destructuring syntax. For example:
197195
198196
```c++
199-
dom::parser parser;
200197
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
201-
auto [object, error] = parser.parse(json).get<dom::object>();
198+
dom::parser parser;
199+
dom::object object;
200+
auto error = parser.parse(json).get(object);
202201
if (error) { cerr << error << endl; return; }
203202
for (auto [key, value] : object) {
204203
cout << key << " = " << value << endl;
@@ -209,11 +208,10 @@ For comparison, here is the C++ 11 version of the same code:
209208

210209
```c++
211210
// C++ 11 version for comparison
212-
dom::parser parser;
213211
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
214-
simdjson::error_code error;
212+
dom::parser parser;
215213
dom::object object;
216-
error = parser.parse(json).get(object);
214+
auto error = parser.parse(json).get(object);
217215
if (!error) { cerr << error << endl; return; }
218216
for (dom::key_value_pair field : object) {
219217
cout << field.key << " = " << field.value << endl;
@@ -378,8 +376,7 @@ And another one:
378376
cout << "number: " << v << endl;
379377
```
380378
381-
Notice how we can string several operation (`parser.parse(abstract_json)["str"]["123"]["abc"].get<double>()`) and only check for the error once, a strategy we call *error chaining*.
382-
379+
Notice how we can string several operations (`parser.parse(abstract_json)["str"]["123"]["abc"].get(v)`) and only check for the error once, a strategy we call *error chaining*.
383380
384381
The next two functions will take as input a JSON document containing an array with a single element, either a string or a number. They return true upon success.
385382

include/simdjson/dom/element.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ class element {
336336
* The key will be matched against **unescaped** JSON:
337337
*
338338
* dom::parser parser;
339-
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().value == 1
340-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error == NO_SUCH_FIELD
339+
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().first == 1
340+
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error() == NO_SUCH_FIELD
341341
*
342342
* @return The value associated with this field, or:
343343
* - NO_SUCH_FIELD if the field does not exist in the object
@@ -351,8 +351,8 @@ class element {
351351
* The key will be matched against **unescaped** JSON:
352352
*
353353
* dom::parser parser;
354-
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().value == 1
355-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error == NO_SUCH_FIELD
354+
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().first == 1
355+
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error() == NO_SUCH_FIELD
356356
*
357357
* @return The value associated with this field, or:
358358
* - NO_SUCH_FIELD if the field does not exist in the object
@@ -391,8 +391,8 @@ class element {
391391
* The key will be matched against **unescaped** JSON:
392392
*
393393
* dom::parser parser;
394-
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().value == 1
395-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error == NO_SUCH_FIELD
394+
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().first == 1
395+
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error() == NO_SUCH_FIELD
396396
*
397397
* @return The value associated with this field, or:
398398
* - NO_SUCH_FIELD if the field does not exist in the object

include/simdjson/dom/object.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class object {
101101
* The key will be matched against **unescaped** JSON:
102102
*
103103
* dom::parser parser;
104-
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().value == 1
105-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error == NO_SUCH_FIELD
104+
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().first == 1
105+
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error() == NO_SUCH_FIELD
106106
*
107107
* This function has linear-time complexity: the keys are checked one by one.
108108
*
@@ -118,8 +118,8 @@ class object {
118118
* The key will be matched against **unescaped** JSON:
119119
*
120120
* dom::parser parser;
121-
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().value == 1
122-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error == NO_SUCH_FIELD
121+
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().first == 1
122+
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error() == NO_SUCH_FIELD
123123
*
124124
* This function has linear-time complexity: the keys are checked one by one.
125125
*
@@ -151,8 +151,8 @@ class object {
151151
* The key will be matched against **unescaped** JSON:
152152
*
153153
* dom::parser parser;
154-
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().value == 1
155-
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error == NO_SUCH_FIELD
154+
* parser.parse(R"({ "a\n": 1 })")["a\n"].get<uint64_t>().first == 1
155+
* parser.parse(R"({ "a\n": 1 })")["a\\n"].get<uint64_t>().error() == NO_SUCH_FIELD
156156
*
157157
* This function has linear-time complexity: the keys are checked one by one.
158158
*

0 commit comments

Comments
 (0)