Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

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 field.value().data() - paddedJson, and access them later in the json string with this offset?

You must be logged in to vote

It is safe, but why not just capture string_view instances.

Can you review this update to our documentation:

#2206

Replies: 2 comments · 2 replies

Comment options

I'm not sure I understand this code (field.value().data() - paddedJson). Do you have a complete example of what you want to do?

You must be logged in to vote
0 replies
Comment options

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 std::string. I'm just wondering if simdjson does anything that would make offset computation auto offset = value.data() - json_padded.data() yield the wrong result. My impression is that it doesn't because of the fact that parser::iterate takes a const char*, and padding the string does not modify the original string.

You must be logged in to vote
2 replies
@lemire
Comment options

It is safe, but why not just capture string_view instances.

Can you review this update to our documentation:

#2206

Answer selected by Gun9niR
@Gun9niR
Comment options

Right, in this example using string_view is fine, in my real code though when I use the value later it's on a JSON string with a different starting address but same content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.