Skip to content

Commit 7ed65e4

Browse files
committed
Add actual examples from basics.md to readme_examples
1 parent 835b640 commit 7ed65e4

File tree

8 files changed

+429
-353
lines changed

8 files changed

+429
-353
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ objs
103103
/perfdiff
104104
/pointercheck
105105
/readme_examples
106+
/readme_examples_noexceptions
106107
/statisticalmodel
107108
/stringparsingcheck
108109
/submodules
@@ -119,6 +120,7 @@ objs
119120
/tests/integer_tests
120121
/tests/parse_many_test
121122
/tests/readme_examples
123+
/tests/readme_examples_noexceptions
122124
/tools/json2json
123125
/tools/jsonstats
124126
/tools/minify

Makefile

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ JSON_INCLUDE:=dependencies/json/single_include/nlohmann/json.hpp
9191
EXTRAOBJECTS=ujdecode.o
9292

9393
MAINEXECUTABLES=parse minify json2json jsonstats statisticalmodel jsonpointer get_corpus_benchmark
94-
TESTEXECUTABLES=jsoncheck jsoncheck_westmere jsoncheck_fallback integer_tests numberparsingcheck stringparsingcheck pointercheck parse_many_test basictests errortests readme_examples
94+
TESTEXECUTABLES=jsoncheck jsoncheck_westmere jsoncheck_fallback integer_tests numberparsingcheck stringparsingcheck pointercheck parse_many_test basictests errortests readme_examples readme_examples_noexceptions
9595
COMPARISONEXECUTABLES=minifiercompetition parsingcompetition parseandstatcompetition distinctuseridcompetition allparserscheckfile allparsingcompetition
9696
SUPPLEMENTARYEXECUTABLES=parse_noutf8validation parse_nonumberparsing parse_nostringparsing
9797

@@ -111,9 +111,6 @@ run_basictests: basictests
111111
run_errortests: errortests
112112
./errortests
113113

114-
run_readme_examples: readme_examples
115-
./readme_examples
116-
117114
run_numberparsingcheck: numberparsingcheck
118115
./numberparsingcheck
119116

@@ -161,7 +158,7 @@ test: quicktests slowtests
161158

162159
quiettest: quicktests slowtests
163160

164-
quicktests: run_basictests run_quickstart run_readme_examples run_jsoncheck run_numberparsingcheck run_integer_tests run_stringparsingcheck run_jsoncheck run_parse_many_test run_pointercheck run_jsoncheck_westmere run_jsoncheck_fallback
161+
quicktests: run_basictests run_quickstart readme_examples readme_examples_noexceptions run_jsoncheck run_numberparsingcheck run_integer_tests run_stringparsingcheck run_jsoncheck run_parse_many_test run_pointercheck run_jsoncheck_westmere run_jsoncheck_fallback
165162

166163
slowtests: run_testjson2json_sh run_issue150_sh
167164

@@ -234,6 +231,8 @@ errortests:tests/errortests.cpp $(HEADERS) $(LIBFILES)
234231
readme_examples: tests/readme_examples.cpp $(HEADERS) $(LIBFILES)
235232
$(CXX) $(CXXFLAGS) -o readme_examples tests/readme_examples.cpp -I. $(LIBFILES) $(LIBFLAGS)
236233

234+
readme_examples_noexceptions: tests/readme_examples_noexceptions.cpp $(HEADERS) $(LIBFILES)
235+
$(CXX) $(CXXFLAGS) -o readme_examples_noexceptions tests/readme_examples_noexceptions.cpp -I. $(LIBFILES) $(LIBFLAGS) -fno-exceptions
237236

238237
numberparsingcheck: tests/numberparsingcheck.cpp $(HEADERS) src/simdjson.cpp
239238
$(CXX) $(CXXFLAGS) -o numberparsingcheck tests/numberparsingcheck.cpp -I. $(LIBFLAGS) -DJSON_TEST_NUMBERS

doc/basics.md

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,6 @@ for (dom::object car : cars) {
102102
}
103103
```
104104

105-
106-
107105
JSON Pointer
108106
------------
109107

@@ -144,7 +142,7 @@ behavior.
144142
>
145143
> ```c++
146144
> dom::element doc;
147-
> error_code error;
145+
> simdjson::error_code error;
148146
> parser.parse(json).tie(doc, error); // <-- Assigns to doc and error just like "auto [doc, error]"
149147
> ```
150148
@@ -154,48 +152,50 @@ This is how the example in "Using the Parsed JSON" could be written using only e
154152
155153
```c++
156154
auto cars_json = R"( [
157-
{ "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
158-
{ "make": "Kia", "model": "Soul", "year": 2012, "tire_pressure": [ 30.1, 31.0, 28.6, 28.7 ] },
159-
{ "make": "Toyota", "model": "Tercel", "year": 1999, "tire_pressure": [ 29.8, 30.0, 30.2, 30.5 ] }
155+
{ "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
156+
{ "make": "Kia", "model": "Soul", "year": 2012, "tire_pressure": [ 30.1, 31.0, 28.6, 28.7 ] },
157+
{ "make": "Toyota", "model": "Tercel", "year": 1999, "tire_pressure": [ 29.8, 30.0, 30.2, 30.5 ] }
160158
] )"_padded;
161159
dom::parser parser;
162-
auto [doc, error] = parser.parse(cars_json);
160+
auto [cars, error] = parser.parse(cars_json).get<dom::array>();
163161
if (error) { cerr << error << endl; exit(1); }
164162
165163
// Iterating through an array of objects
166164
for (dom::element car_element : cars) {
167-
dom::object car;
168-
car_element.get<dom::object>().tie(car, error);
169-
if (error) { cerr << error << endl; exit(1); }
165+
dom::object car;
166+
car_element.get<dom::object>().tie(car, error);
167+
if (error) { cerr << error << endl; exit(1); }
170168
171-
// Accessing a field by name
172-
dom::element make, model;
173-
car["make"].tie(make, error);
174-
if (error) { cerr << error << endl; exit(1); }
175-
car["model"].tie(model, error);
176-
if (error) { cerr << error << endl; exit(1); }
177-
cout << "Make/Model: " << make << "/" << model << endl;
169+
// Accessing a field by name
170+
dom::element make, model;
171+
car["make"].tie(make, error);
172+
if (error) { cerr << error << endl; exit(1); }
173+
car["model"].tie(model, error);
174+
if (error) { cerr << error << endl; exit(1); }
175+
cout << "Make/Model: " << make << "/" << model << endl;
178176
179-
// Casting a JSON element to an integer
180-
uint64_t year;
181-
car["year"].get<uint64_t>().tie(year, error);
182-
if (error) { cerr << error << endl; exit(1); }
183-
cout << "- This car is " << 2020 - year << "years old." << endl;
177+
// Casting a JSON element to an integer
178+
uint64_t year;
179+
car["year"].get<uint64_t>().tie(year, error);
180+
if (error) { cerr << error << endl; exit(1); }
181+
cout << "- This car is " << 2020 - year << "years old." << endl;
184182
185-
// Iterating through an array of floats
186-
double total_tire_pressure = 0;
187-
for (dom::element tire_pressure_element : car["tire_pressure"]) {
183+
// Iterating through an array of floats
184+
double total_tire_pressure = 0;
185+
dom::array tire_pressure_array;
186+
car["tire_pressure"].get<dom::array>().tie(tire_pressure_array, error);
187+
if (error) { cerr << error << endl; exit(1); }
188+
for (dom::element tire_pressure_element : tire_pressure_array) {
188189
double tire_pressure;
189-
tire_pressure_element.get<uint64_t>().tie(tire_pressure, error);
190+
tire_pressure_element.get<double>().tie(tire_pressure, error);
190191
if (error) { cerr << error << endl; exit(1); }
191192
total_tire_pressure += tire_pressure;
192-
}
193-
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
193+
}
194+
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
194195
195-
// Writing out all the information about the car
196-
for (auto [key, value] : car) {
196+
// Writing out all the information about the car
197+
for (auto [key, value] : car) {
197198
cout << "- " << key << ": " << value << endl;
198-
}
199199
}
200200
```
201201

@@ -215,14 +215,15 @@ Newline-Delimited JSON (ndjson) and JSON lines
215215

216216
The simdjson library also support multithreaded JSON streaming through a large file containing many smaller JSON documents in either [ndjson](http://ndjson.org) or [JSON lines](http://jsonlines.org) format. If your JSON documents all contain arrays or objects, we even support direct file concatenation without whitespace. The concatenated file has no size restrictions (including larger than 4GB), though each individual document must be less than 4GB.
217217

218-
Here is a simple example:
218+
Here is a simple example, given "x.json" with this content:
219219

220-
```cpp
221-
auto ndjson = R"(
220+
```json
222221
{ "foo": 1 }
223222
{ "foo": 2 }
224223
{ "foo": 3 }
225-
)"_padded;
224+
```
225+
226+
```c++
226227
dom::parser parser;
227228
for (dom::element doc : parser.load_many(filename)) {
228229
cout << doc["foo"] << endl;

0 commit comments

Comments
 (0)