Skip to content

Commit dc918d7

Browse files
authored
Merge pull request simdjson#646 from simdjson/jkeiser/quickstart-example
Compile all .md examples in CI
2 parents 9906887 + 2115596 commit dc918d7

12 files changed

Lines changed: 522 additions & 349 deletions

File tree

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ objs
7070
/corpus.zip
7171
/distinctuseridcompetition
7272
/errortests
73+
/examples/quickstart/quickstart
74+
/examples/quickstart/simdjson.cpp
75+
/examples/quickstart/simdjson.h
76+
/examples/quickstart/twitter.json
7377
/fuzz/fuzz_dump
7478
/fuzz/fuzz_dump_raw_tape
7579
/fuzz/fuzz_parser
@@ -99,6 +103,7 @@ objs
99103
/perfdiff
100104
/pointercheck
101105
/readme_examples
106+
/readme_examples_noexceptions
102107
/statisticalmodel
103108
/stringparsingcheck
104109
/submodules
@@ -115,6 +120,7 @@ objs
115120
/tests/integer_tests
116121
/tests/parse_many_test
117122
/tests/readme_examples
123+
/tests/readme_examples_noexceptions
118124
/tools/json2json
119125
/tools/jsonstats
120126
/tools/minify

Makefile

Lines changed: 14 additions & 8 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

@@ -141,6 +138,12 @@ run_pointercheck: pointercheck
141138
run_issue150_sh: allparserscheckfile
142139
./scripts/issue150.sh
143140

141+
quickstart:
142+
cd examples/quickstart && make quickstart
143+
144+
run_quickstart:
145+
cd examples/quickstart && make test
146+
144147
run_testjson2json_sh: minify json2json
145148
./scripts/testjson2json.sh
146149

@@ -150,12 +153,12 @@ $(FEATURE_JSON_FILES): benchmark/genfeaturejson.rb
150153
run_benchfeatures: benchfeatures $(FEATURE_JSON_FILES)
151154
./benchfeatures -n 1000
152155

153-
test: run_basictests run_readme_examples run_jsoncheck run_numberparsingcheck run_integer_tests run_stringparsingcheck run_parse_many_test run_pointercheck run_testjson2json_sh run_issue150_sh run_jsoncheck_westmere run_jsoncheck_fallback
156+
test: quicktests slowtests
154157
@echo "It looks like the code is good!"
155158

156-
quiettest: run_basictests run_readme_examples run_jsoncheck run_numberparsingcheck run_integer_tests run_stringparsingcheck run_jsoncheck run_parse_many_test run_pointercheck run_testjson2json_sh run_issue150_sh run_jsoncheck_westmere run_jsoncheck_fallback
159+
quiettest: quicktests slowtests
157160

158-
quicktests: run_basictests 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
159162

160163
slowtests: run_testjson2json_sh run_issue150_sh
161164

@@ -228,6 +231,8 @@ errortests:tests/errortests.cpp $(HEADERS) $(LIBFILES)
228231
readme_examples: tests/readme_examples.cpp $(HEADERS) $(LIBFILES)
229232
$(CXX) $(CXXFLAGS) -o readme_examples tests/readme_examples.cpp -I. $(LIBFILES) $(LIBFLAGS)
230233

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
231236

232237
numberparsingcheck: tests/numberparsingcheck.cpp $(HEADERS) src/simdjson.cpp
233238
$(CXX) $(CXXFLAGS) -o numberparsingcheck tests/numberparsingcheck.cpp -I. $(LIBFLAGS) -DJSON_TEST_NUMBERS
@@ -283,10 +288,11 @@ allparserscheckfile: tests/allparserscheckfile.cpp $(HEADERS) $(LIBFILES) $(EXTR
283288
cppcheck:
284289
cppcheck --enable=all src/*.cpp benchmarks/*.cpp tests/*.cpp -Iinclude -I. -Ibenchmark/linux
285290

286-
everything: $(MAINEXECUTABLES) $(EXTRA_EXECUTABLES) $(TESTEXECUTABLES) $(COMPARISONEXECUTABLES) $(SUPPLEMENTARYEXECUTABLES)
291+
everything: $(MAINEXECUTABLES) $(EXTRA_EXECUTABLES) $(TESTEXECUTABLES) $(COMPARISONEXECUTABLES) $(SUPPLEMENTARYEXECUTABLES) quickstart
287292

288293
clean:
289294
rm -f submodules $(EXTRAOBJECTS) $(MAINEXECUTABLES) $(EXTRA_EXECUTABLES) $(TESTEXECUTABLES) $(COMPARISONEXECUTABLES) $(SUPPLEMENTARYEXECUTABLES)
295+
cd examples/quickstart && make clean
290296

291297
cleandist:
292298
rm -f submodules $(EXTRAOBJECTS) $(MAINEXECUTABLES) $(EXTRA_EXECUTABLES) $(TESTEXECUTABLES) $(COMPARISONEXECUTABLES) $(SUPPLEMENTARYEXECUTABLES)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The simdjson library is easily consumable with a single .h and .cpp file.
4343
```
4444
wget https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.h https://raw.githubusercontent.com/simdjson/simdjson/master/singleheader/simdjson.cpp https://raw.githubusercontent.com/simdjson/simdjson/master/jsonexamples/twitter.json
4545
```
46-
2. Create `parser.cpp`:
46+
2. Create `quickstart.cpp`:
4747

4848
```c++
4949
#include "simdjson.h"
@@ -53,8 +53,8 @@ The simdjson library is easily consumable with a single .h and .cpp file.
5353
std::cout << tweets["search_metadata"]["count"] << " results." << std::endl;
5454
}
5555
```
56-
3. `c++ -o parser parser.cpp simdjson.cpp -std=c++17`
57-
4. `./parser`
56+
3. `c++ -o quickstart quickstart.cpp simdjson.cpp -std=c++17`
57+
4. `./quickstart`
5858
```
5959
100 results.
6060
```

doc/basics.md

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ behavior.
142142
>
143143
> ```c++
144144
> dom::element doc;
145-
> error_code error;
145+
> simdjson::error_code error;
146146
> parser.parse(json).tie(doc, error); // <-- Assigns to doc and error just like "auto [doc, error]"
147147
> ```
148148
@@ -152,48 +152,50 @@ This is how the example in "Using the Parsed JSON" could be written using only e
152152
153153
```c++
154154
auto cars_json = R"( [
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 ] }
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 ] }
158158
] )"_padded;
159159
dom::parser parser;
160-
auto [doc, error] = parser.parse(cars_json);
160+
auto [cars, error] = parser.parse(cars_json).get<dom::array>();
161161
if (error) { cerr << error << endl; exit(1); }
162162
163163
// Iterating through an array of objects
164164
for (dom::element car_element : cars) {
165-
dom::object car;
166-
car_element.get<dom::object>().tie(car, error);
167-
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); }
168168
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;
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;
176176
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;
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;
182182
183-
// Iterating through an array of floats
184-
double total_tire_pressure = 0;
185-
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) {
186189
double tire_pressure;
187-
tire_pressure_element.get<uint64_t>().tie(tire_pressure, error);
190+
tire_pressure_element.get<double>().tie(tire_pressure, error);
188191
if (error) { cerr << error << endl; exit(1); }
189192
total_tire_pressure += tire_pressure;
190-
}
191-
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
193+
}
194+
cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
192195
193-
// Writing out all the information about the car
194-
for (auto [key, value] : car) {
196+
// Writing out all the information about the car
197+
for (auto [key, value] : car) {
195198
cout << "- " << key << ": " << value << endl;
196-
}
197199
}
198200
```
199201

@@ -217,14 +219,15 @@ format. If your JSON documents all contain arrays or objects, we even support di
217219
concatenation without whitespace. The concatenated file has no size restrictions (including larger
218220
than 4GB), though each individual document must be less than 4GB.
219221

220-
Here is a simple example:
222+
Here is a simple example, given "x.json" with this content:
221223

222-
```cpp
223-
auto ndjson = R"(
224+
```json
224225
{ "foo": 1 }
225226
{ "foo": 2 }
226227
{ "foo": 3 }
227-
)"_padded;
228+
```
229+
230+
```c++
228231
dom::parser parser;
229232
for (dom::element doc : parser.load_many(filename)) {
230233
cout << doc["foo"] << endl;

doc/performance.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ without bound:
7777
7878
```c++
7979
dom::parser parser(0); // This parser will refuse to automatically grow capacity
80-
parser.set_capacity(1024*1024); // This allocates enough capacity to handle documents <= 1MB
80+
simdjson::error_code allocate_error = parser.set_capacity(1024*1024); // This allocates enough capacity to handle documents <= 1MB
81+
if (allocate_error) { cerr << allocate_error << endl; exit(1); }
82+
8183
for (web_request request : listen()) {
8284
auto [doc, error] = parser.parse(request.body);
8385
// If the document was above our limit, emit 413 = payload too large

examples/quickstart/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ROOT=../..
2+
SINGLEHEADER=$(ROOT)/singleheader
3+
JSONEXAMPLES=$(ROOT)/jsonexamples
4+
5+
test: quickstart twitter.json
6+
./quickstart
7+
simdjson.cpp: $(SINGLEHEADER)/simdjson.cpp
8+
cp $(SINGLEHEADER)/simdjson.cpp .
9+
simdjson.h: $(SINGLEHEADER)/simdjson.h
10+
cp $(SINGLEHEADER)/simdjson.h .
11+
twitter.json: $(JSONEXAMPLES)/twitter.json
12+
cp $(JSONEXAMPLES)/twitter.json .
13+
quickstart: quickstart.cpp simdjson.cpp simdjson.h
14+
c++ -o ./quickstart quickstart.cpp simdjson.cpp -std=c++17
15+
clean:
16+
rm -f simdjson.cpp simdjson.h twitter.json quickstart

examples/quickstart/quickstart.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "simdjson.h"
2+
int main(void) {
3+
simdjson::dom::parser parser;
4+
simdjson::dom::element tweets = parser.load("twitter.json");
5+
std::cout << tweets["search_metadata"]["count"] << " results." << std::endl;
6+
}

0 commit comments

Comments
 (0)