mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
deploy: d65acbd47b
This commit is contained in:
+31
-1
@@ -239,7 +239,7 @@ Using the Parsed JSON</h1>
|
||||
<li>Because On Demand is really just an iterator, you must fully consume the current object or array before accessing a sibling object or array.</li>
|
||||
<li>Values can only be consumed once, you should get the values and store them if you plan to need them multiple times. You are expected to access the keys of an object just once. You are expected to go through the values of an array just once.</li>
|
||||
</ol>
|
||||
<p>The simdjson library makes generous use of <code>std::string_view</code> instances. If you are unfamiliar with <code>std::string_view</code> in C++, make sure to <a href="#string_view">read the section on std::string_view</a>.</p>
|
||||
<p>The simdjson library makes generous use of <code>std::string_view</code> instances. If you are unfamiliar with <code>std::string_view</code> in C++, make sure to <a href="#string_view">read the section on std::string_view</a>. They behave much like an immutable <code>std::string</code> but they require no memory allocation. You can create a <code>std::string</code> instance from an <code>std::string_view</code> when you need it.</p>
|
||||
<p>The following specific instructions indicate how to use the JSON when exceptions are enabled, but simdjson has full, idiomatic support for users who avoid exceptions. See <a href="basics.md#error-handling">the simdjson error handling documentation</a> for more.</p>
|
||||
<ul>
|
||||
<li><b>Validate What You Use:</b> When calling <code>iterate</code>, the document is quickly indexed. If it is not a valid Unicode (UTF-8) string or if there is an unclosed string, an error may be reported right away. However, it is not fully validated. On Demand only fully validates the values you use and the structure leading to it.</li>
|
||||
@@ -452,6 +452,36 @@ Using the Parsed JSON: Additional examples</h2>
|
||||
<div class="line"> }</div>
|
||||
<div class="line"> cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;</div>
|
||||
<div class="line">}</div>
|
||||
</div><!-- fragment --><p>The previous example had an array of objects, but we can use essentially the same approach with an object of objects.</p>
|
||||
<div class="fragment"><div class="line"> {c++}</div>
|
||||
<div class="line">ondemand::parser parser;</div>
|
||||
<div class="line">auto cars_json = R"( {</div>
|
||||
<div class="line"> "identifier1":{ "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },</div>
|
||||
<div class="line"> "identifier2":{ "make": "Kia", "model": "Soul", "year": 2012, "tire_pressure": [ 30.1, 31.0, 28.6, 28.7 ] },</div>
|
||||
<div class="line"> "identifier3":{ "make": "Toyota", "model": "Tercel", "year": 1999, "tire_pressure": [ 29.8, 30.0, 30.2, 30.5 ] }</div>
|
||||
<div class="line">} )"_padded;</div>
|
||||
<div class="line">// Iterating through an array of objects</div>
|
||||
<div class="line">ondemand::document doc = parser.iterate(cars_json);</div>
|
||||
<div class="line">for (ondemand::field key_car : doc.get_object()) {</div>
|
||||
<div class="line"> // If I need a string_view and/or, I can use key_car.unescaped_key() instead, but</div>
|
||||
<div class="line"> // key_car.key() will be more performant otherwise.</div>
|
||||
<div class="line"> cout << "identifier : " << key_car.key() << std::endl;</div>
|
||||
<div class="line"> // I can now access the subobject:</div>
|
||||
<div class="line"> ondemand::object car = key_car.value();</div>
|
||||
<div class="line"> // Accessing a field by name</div>
|
||||
<div class="line"> cout << "Make/Model: " << std::string_view(car["make"]) << "/" << std::string_view(car["model"]) << endl;</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> // Casting a JSON element to an integer</div>
|
||||
<div class="line"> uint64_t year = car["year"];</div>
|
||||
<div class="line"> cout << "- This car is " << 2020 - year << "years old." << endl;</div>
|
||||
<div class="line"> </div>
|
||||
<div class="line"> // Iterating through an array of floats</div>
|
||||
<div class="line"> double total_tire_pressure = 0;</div>
|
||||
<div class="line"> for (double tire_pressure : car["tire_pressure"]) {</div>
|
||||
<div class="line"> total_tire_pressure += tire_pressure;</div>
|
||||
<div class="line"> }</div>
|
||||
<div class="line"> cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;</div>
|
||||
<div class="line">}</div>
|
||||
</div><!-- fragment --><p>The following example illustrates how you may also iterate through object values, effectively visiting all key-value pairs in the object.</p>
|
||||
<div class="fragment"><div class="line"> {C++}</div>
|
||||
<div class="line">#include <iostream></div>
|
||||
|
||||
Reference in New Issue
Block a user