diff --git a/md_doc_basics.html b/md_doc_basics.html index 584e6f8da..11c3a3cd5 100644 --- a/md_doc_basics.html +++ b/md_doc_basics.html @@ -266,7 +266,7 @@ Using the Parsed JSON
* Field Access: To get the value of the "foo" field in an object, use object["foo"]. This will scan through the object looking for the field with the matching string, doing a character-by-character comparison. It may generate the error simdjson::NO_SUCH_FIELD if there is no such key in the object, it may throw an exception (see Error Handling). For efficiency reason, you should avoid looking up the same field repeatedly: e.g., do not do object["foo"] followed by object["foo"] with the same object instance. Keep in mind that On Demand does not buffer or save the result of the parsing: if you repeatedly access object["foo"], then it must repeatedly seek the key and parse the content. The library does not provide a distinct function to check if a key is present, instead we recommend you attempt to access the key: e.g., by doing ondemand::value val{}; if (!object["foo"].get(val)) {...}, you have that val contains the requested value inside the if clause. It is your responsibility as a user to temporarily keep a reference to the value (auto v = object["foo"]), or to consume the content and store it in your own data structures. If you consume an object twice: std::string_view(object["foo"] followed by std::string_view(object["foo"] then your code is in error. Furthermore, you can only consume one field at a time, on the same object. The value instance you get from content["bids"] becomes invalid when you call content["asks"]. If you have retrieved content["bids"].get_array() and you later call content["asks"].get_array(), then the first array should no longer be accessed: it would be unsafe to do so. You can detect such mistakes by first compiling and running the code in Debug mode: an OUT_OF_ORDER_ITERATION error is generated.
-NOTE: JSON allows you to escape characters in keys. E.g., the key
+"date"may be written as"\u0064\u0061\u0074\u0065". By default, simdjson does not unescape keys when matching by default. Thus if you search for the key"date"and the JSON document uses"\u0064\u0061\u0074\u0065"as a key, it will not be recognized. This is not generally a problem. Nevertheless, if you do need to support escaped keys, the methodunescaped_key()provides the desired unescaped keys by parsing and writing out the unescaped keys to a string buffer and returning astd::string_viewinstance. You should expect a performance penalty when usingunescaped_key().NOTE: JSON allows you to escape characters in keys. E.g., the key
"date"may be written as"\u0064\u0061\u0074\u0065". By default, simdjson does not unescape keys when matching by default. Thus if you search for the key"date"and the JSON document uses"\u0064\u0061\u0074\u0065"as a key, it will not be recognized. This is not generally a problem. Nevertheless, if you do need to support escaped keys, the methodunescaped_key()provides the desired unescaped keys by parsing and writing out the unescaped keys to a string buffer and returning astd::string_viewinstance. Theunescaped_keytakes an optional Boolean value: passing it true will decode invalid Unicode sequences with replacement, meaning that the decoding always succeeds but bogus Unicode replacement characters are inserted. In general, you should expect a performance penalty when usingunescaped_key()compared tokey()because of the string processing: thekey()function just points inside the source JSON document.{c++}auto json = R"({"k\u0065y": 1})"_padded;ondemand::parser parser;@@ -295,9 +295,10 @@ Using the Parsed JSONdouble x = doc["x"]; // Success: [] loops back around to find "x"
* Array Iteration: To iterate through an array, use for (auto value : array) { ... }. This will step through each value in the JSON array.
If you know the type of the value, you can cast it right there, too! for (double value : array) { ... }.
for (auto field : object) { ... }field.unescaped_key() will get you the unescaped key string.If you know the type of the value, you can cast it right there, too! for (double value : array) { ... }.
You may also use explicit iterators: for(auto i = array.begin(); i != array.end(); i++) {}. You can check that an array is empty with the condition auto i = array.begin(); if(i == array.end()) {...}.
for (auto field : object) { ... }. You may also use explicit iterators : for(auto i = object.begin(); i != object.end(); i++) { auto field = *i; .... }. You can check that an object is empty with the condition auto i = object.begin(); if(i == object.end()) {...}.field.unescaped_key() will get you the unescaped key string. E.g., the JSON string "\u00e1" becomes the Unicode string รก. Optionally, you pass true as a parameter to the unescaped_key method if you want invalid escape sequences to be replaced by a default replacement character (e.g., \ud800\ud801\ud811): otherwise bad escape sequences lead to an immediate error.field.value() will get you the value, which you can then use all these other methods on.