Skip to content

Commit

Permalink
adding another example (#2206)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Lemire <dlemire@lemire.me>
  • Loading branch information
lemire and Daniel Lemire committed Jun 27, 2024
1 parent 0e8311f commit 3e94eea
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions doc/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,33 @@ string representation.
```
You can use `raw_json()` to capture the content of some JSON values as `std::string_view`
instances which can be safely used later. The `std::string_view` instances point inside
the original document and do not depend in any way on simdjson. In the following example,
we store the `std::string_view` instances inside a `std::vector<std::string_view>` instance
and print the out after the parsing is concluded:
```cpp
padded_string json_padded = "{\"a\":[1,2,3], \"b\": 2, \"c\": \"hello\"}"_padded;
std::vector<std::string_view> fields;
ondemand::parser parser;
auto doc = parser.iterate(json_padded);
auto object = doc.get_object();
for (auto field : object) {
fields.push_back(field.value().raw_json());
}
// Output the fields
// Expected output:
// [1,2,3]
// 2
// "hello"
for (std::string_view field_ref : fields) {
std::cout << field_ref << std::endl;
}
```
Storing directly into an existing string instance
-----------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions tests/ondemand/ondemand_readme_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ simdjson_inline simdjson_result<Car> simdjson::ondemand::document::get() & noexc

#if SIMDJSON_EXCEPTIONS

void main_capture() {
padded_string json_padded = "{\"a\":[1,2,3], \"b\": 2, \"c\": \"hello\"}"_padded;
std::vector<std::string_view> fields;

ondemand::parser parser;
auto doc = parser.iterate(json_padded);
auto object = doc.get_object();
for (auto field : object) {
fields.push_back(field.value().raw_json());
}
// Output the fields
// Expected output:
// [1,2,3]
// 2
// "hello"
for (std::string_view field_ref : fields) {
std::cout << field_ref << std::endl;
}
}

int custom_type_on_document() {
padded_string json = R"( { "make": "Toyota", "model": "Camry", "year": 2018,
Expand Down

0 comments on commit 3e94eea

Please sign in to comment.