-
I want to reuse some values in the document, but want to avoid copying those values if at all possible. Is it safe to just compute the offset of the value with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I'm not sure I understand this code ( |
Beta Was this translation helpful? Give feedback.
-
Here's a minimal example of what I'm trying to do #include "simdjson.h"
#include <iostream>
#include <string>
#include <vector>
using namespace simdjson;
struct JsonFieldRef {
size_t offset;
size_t len;
};
int main() {
std::string json = "{\"a\":1, \"b\": 2}";
padded_string json_padded = padded_string(json);
std::vector<JsonFieldRef> fields;
ondemand::parser parser;
auto doc = parser.iterate(json_padded);
auto object = doc.get_object();
for (auto field : object) {
auto value = field.value().raw_json().value();
auto offset = value.data() - json_padded.data();
auto len = value.size();
fields.emplace_back(JsonFieldRef{static_cast<size_t>(offset), len});
}
for (auto field_ref : fields) {
std::cout << std::string_view(json.data() + field_ref.offset, field_ref.len)
<< std::endl;
}
return 0;
} Given that I have access to the original Json string all the time, I want to extract the values so I can reuse them later. The catch is I want to do it just by storing their offset and length instead of copying them to a new |
Beta Was this translation helpful? Give feedback.
It is safe, but why not just capture string_view instances.
Can you review this update to our documentation:
#2206