diff --git a/.circleci/config.yml b/.circleci/config.yml index 02a0af4f..71d26273 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -192,7 +192,7 @@ workflows: - clang6 # libc++ - - libcpp-clang9 + # - libcpp-clang9 # disabled due to too many errors # full single-implementation tests - sanitize-gcc9 diff --git a/doc/basics.md b/doc/basics.md index 9be3776a..68992014 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -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().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().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().tie(innerobj, error); + if (error) { cerr << error << endl; exit(1); } + + double va; + innerobj["a"].get().tie(va, error); + if (error) { cerr << error << endl; exit(1); } + cout << "a: " << va << ", "; + + double vb; + innerobj["b"].get().tie(vb, error); + if (error) { cerr << error << endl; exit(1); } + cout << "b: " << vb << ", "; + + int64_t vc; + innerobj["c"].get().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` to the desired type: diff --git a/include/simdjson/inline/padded_string.h b/include/simdjson/inline/padded_string.h index 748e2c47..0624194b 100644 --- a/include/simdjson/inline/padded_string.h +++ b/include/simdjson/inline/padded_string.h @@ -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) diff --git a/include/simdjson/inline/parsedjson_iterator.h b/include/simdjson/inline/parsedjson_iterator.h index 5b36d4b4..dad89218 100644 --- a/include/simdjson/inline/parsedjson_iterator.h +++ b/include/simdjson/inline/parsedjson_iterator.h @@ -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])); diff --git a/include/simdjson/padded_string.h b/include/simdjson/padded_string.h index 2f60131f..977d9260 100644 --- a/include/simdjson/padded_string.h +++ b/include/simdjson/padded_string.h @@ -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 diff --git a/tests/readme_examples.cpp b/tests/readme_examples.cpp index bee89dac..27aeed8c 100644 --- a/tests/readme_examples.cpp +++ b/tests/readme_examples.cpp @@ -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; } diff --git a/tests/readme_examples_noexceptions.cpp b/tests/readme_examples_noexceptions.cpp index 0b5cd677..de365f8e 100644 --- a/tests/readme_examples_noexceptions.cpp +++ b/tests/readme_examples_noexceptions.cpp @@ -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().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().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().tie(innerobj, error); + if (error) { cerr << error << endl; exit(1); } + + double va; + innerobj["a"].get().tie(va, error); + if (error) { cerr << error << endl; exit(1); } + cout << "a: " << va << ", "; + + double vb; + innerobj["b"].get().tie(vb, error); + if (error) { cerr << error << endl; exit(1); } + cout << "b: " << vb << ", "; + + int64_t vc; + innerobj["c"].get().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; }