Merge branch 'master' of github.com:simdjson/simdjson

This commit is contained in:
Daniel Lemire 2020-04-23 18:39:56 -04:00
commit f3ac0be0e6
7 changed files with 137 additions and 5 deletions

View File

@ -192,7 +192,7 @@ workflows:
- clang6
# libc++
- libcpp-clang9
# - libcpp-clang9 # disabled due to too many errors
# full single-implementation tests
- sanitize-gcc9

View File

@ -124,6 +124,27 @@ for (dom::object car : parser.parse(cars_json)) {
}
```
Here is a different example illustrating the same ideas:
```C++
auto abstract_json = R"( [
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
] )"_padded;
dom::parser parser;
// Parse and iterate through an array of objects
for (dom::object obj : parser.parse(abstract_json)) {
for(const auto& key_value : obj) {
cout << "key: " << key_value.key << " : ";
dom::object innerobj = key_value.value;
cout << "a: " << double(innerobj["a"]) << ", ";
cout << "b: " << double(innerobj["b"]) << ", ";
cout << "c: " << int64_t(innerobj["c"]) << endl;
}
}
```
C++17 Support
-------------
@ -277,6 +298,49 @@ for (auto field : car) {
}
```
Here is another example:
```C++
auto abstract_json = R"( [
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
] )"_padded;
dom::parser parser;
dom::array rootarray;
simdjson::error_code error;
parser.parse(abstract_json).get<dom::array>().tie(rootarray, error);
if (error) { cerr << error << endl; exit(1); }
// Iterate through an array of objects
for (dom::element elem : rootarray) {
dom::object obj;
elem.get<dom::object>().tie(obj, error);
if (error) { cerr << error << endl; exit(1); }
for(auto & key_value : obj) {
cout << "key: " << key_value.key << " : ";
dom::object innerobj;
key_value.value.get<dom::object>().tie(innerobj, error);
if (error) { cerr << error << endl; exit(1); }
double va;
innerobj["a"].get<double>().tie(va, error);
if (error) { cerr << error << endl; exit(1); }
cout << "a: " << va << ", ";
double vb;
innerobj["b"].get<double>().tie(vb, error);
if (error) { cerr << error << endl; exit(1); }
cout << "b: " << vb << ", ";
int64_t vc;
innerobj["c"].get<int64_t>().tie(vc, error);
if (error) { cerr << error << endl; exit(1); }
cout << "c: " << vc << endl;
}
}
```
### Exceptions
Users more comfortable with an exception flow may choose to directly cast the `simdjson_result<T>` to the desired type:

View File

@ -34,7 +34,7 @@ inline char *allocate_padded_buffer(size_t length) noexcept {
} // namespace internal
inline padded_string::padded_string() noexcept : viable_size(0), data_ptr(nullptr) {}
inline padded_string::padded_string() noexcept {}
inline padded_string::padded_string(size_t length) noexcept
: viable_size(length), data_ptr(internal::allocate_padded_buffer(length)) {
if (data_ptr != nullptr)

View File

@ -247,8 +247,7 @@ dom::parser::Iterator::Iterator(
location(o.location),
tape_length(o.tape_length),
current_type(o.current_type),
current_val(o.current_val),
depth_index()
current_val(o.current_val)
{
depth_index = new scopeindex_t[max_depth+1];
memcpy(depth_index, o.depth_index, (depth + 1) * sizeof(depth_index[0]));

View File

@ -106,7 +106,7 @@ private:
padded_string &operator=(const padded_string &o) = delete;
padded_string(const padded_string &o) = delete;
size_t viable_size;
size_t viable_size{0};
char *data_ptr{nullptr};
}; // padded_string

View File

@ -52,6 +52,8 @@ void basics_dom_1() {
}
}
void basics_dom_2() {
auto cars_json = R"( [
{ "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
@ -63,6 +65,27 @@ void basics_dom_2() {
cout << cars.at("0/tire_pressure/1") << endl; // Prints 39.9
}
void basics_dom_3() {
auto abstract_json = R"( [
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
] )"_padded;
dom::parser parser;
// Parse and iterate through an array of objects
for (dom::object obj : parser.parse(abstract_json)) {
for(const auto& key_value : obj) {
cout << "key: " << key_value.key << " : ";
dom::object innerobj = key_value.value;
cout << "a: " << double(innerobj["a"]) << ", ";
cout << "b: " << double(innerobj["b"]) << ", ";
cout << "c: " << int64_t(innerobj["c"]) << endl;
}
}
}
namespace treewalk_1 {
void print_json(dom::element element) {
switch (element.type()) {
@ -210,5 +233,8 @@ SIMDJSON_POP_DISABLE_WARNINGS
#endif
int main() {
basics_dom_1();
basics_dom_2();
basics_dom_3();
return 0;
}

View File

@ -77,6 +77,46 @@ void basics_error_3() {
}
}
}
void basics_error_4() {
auto abstract_json = R"( [
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
] )"_padded;
dom::parser parser;
dom::array rootarray;
simdjson::error_code error;
parser.parse(abstract_json).get<dom::array>().tie(rootarray, error);
if (error) { cerr << error << endl; exit(1); }
// Iterate through an array of objects
for (dom::element elem : rootarray) {
dom::object obj;
elem.get<dom::object>().tie(obj, error);
if (error) { cerr << error << endl; exit(1); }
for(auto & key_value : obj) {
cout << "key: " << key_value.key << " : ";
dom::object innerobj;
key_value.value.get<dom::object>().tie(innerobj, error);
if (error) { cerr << error << endl; exit(1); }
double va;
innerobj["a"].get<double>().tie(va, error);
if (error) { cerr << error << endl; exit(1); }
cout << "a: " << va << ", ";
double vb;
innerobj["b"].get<double>().tie(vb, error);
if (error) { cerr << error << endl; exit(1); }
cout << "b: " << vb << ", ";
int64_t vc;
innerobj["c"].get<int64_t>().tie(vc, error);
if (error) { cerr << error << endl; exit(1); }
cout << "c: " << vc << endl;
}
}
}
#ifdef SIMDJSON_CPLUSPLUS17
void basics_error_3_cpp17() {
@ -131,5 +171,8 @@ void basics_error_3_cpp17() {
#endif
int main() {
basics_error_2();
basics_error_3();
basics_error_4();
return 0;
}