Skip to content

Commit 3fa40b8

Browse files
authored
Adding an example corresponding to issue 1316 (documentation enhancement) (simdjson#1317)
* Adding an example. * Updated other doc file. * Trying to take into account @jkeiser's comments. * Some people prefer empty final lines.
1 parent dc69bc2 commit 3fa40b8

File tree

5 files changed

+146
-4
lines changed

5 files changed

+146
-4
lines changed

doc/basics.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,31 @@ When you use the code this way, it is your responsibility to check for error bef
373373
result: if there is an error, the result value will not be valid and using it will caused undefined
374374
behavior.
375375
376-
We can write a "quick start" example where we attempt to parse a file and access some data, without triggering exceptions:
376+
We can write a "quick start" example where we attempt to parse the following JSON file and access some data, without triggering exceptions:
377+
```JavaScript
378+
{
379+
"statuses": [
380+
{
381+
"id": 505874924095815700
382+
},
383+
{
384+
"id": 505874922023837700
385+
}
386+
],
387+
"search_metadata": {
388+
"count": 100
389+
}
390+
}
391+
```
392+
393+
Our program loads the file, selects value corresponding to key "search_metadata" which expected to be an object, and then
394+
it selects the key "count" within that object.
377395

378396
```C++
379397
#include "simdjson.h"
380398

381399
int main(void) {
382400
simdjson::dom::parser parser;
383-
384401
simdjson::dom::element tweets;
385402
auto error = parser.load("twitter.json").get(tweets);
386403
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
@@ -394,6 +411,31 @@ int main(void) {
394411
}
395412
```
396413
414+
The following is a similar example where one wants to get the id of the first tweet without
415+
triggering exceptions. To do this, we use `["statuses"].at(0)["id"]`. We break that expression down:
416+
417+
- Get the list of tweets (the `"statuses"` key of the document) using `["statuses"]`). The result is expected to be an array.
418+
- Get the first tweet using `.at(0)`. The result is expected to be an object.
419+
- Get the id of the tweet using ["id"]. We expect the value to be a non-negative integer.
420+
421+
Observe how we use the `at` method when querying an index into an array, and not the bracket operator.
422+
423+
```C++
424+
#include "simdjson.h"
425+
426+
int main(void) {
427+
simdjson::dom::parser parser;
428+
simdjson::dom::element tweets;
429+
auto error = parser.load("twitter.json").get(tweets);
430+
if(error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
431+
uint64_t identifier;
432+
error = tweets["statuses"].at(0)["id"].get(identifier);
433+
if(error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
434+
std::cout << identifier << std::endl;
435+
return EXIT_SUCCESS;
436+
}
437+
```
438+
397439
### Error Handling Example
398440

399441
This is how the example in "Using the Parsed JSON" could be written using only error code checking:
@@ -525,6 +567,21 @@ dom::element doc = parser.parse(json); // Throws an exception if there was an er
525567
When used this way, a `simdjson_error` exception will be thrown if an error occurs, preventing the
526568
program from continuing if there was an error.
527569

570+
571+
If one is willing to trigger exceptions, it is possible to write simpler code:
572+
573+
```C++
574+
#include "simdjson.h"
575+
576+
int main(void) {
577+
simdjson::dom::parser parser;
578+
simdjson::dom::element tweets = parser.load("twitter.json");
579+
std::cout << "ID: " << tweets["statuses"].at(0)["id"] << std::endl;
580+
return EXIT_SUCCESS;
581+
}
582+
```
583+
584+
528585
Tree Walking and JSON Element Types
529586
-----------------------------------
530587

doc/basics_doxygen.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,33 @@ When you use the code this way, it is your responsibility to check for error bef
354354
result: if there is an error, the result value will not be valid and using it will caused undefined
355355
behavior.
356356

357-
We can write a "quick start" example where we attempt to parse a file and access some data, without triggering exceptions:
358357

358+
359+
We can write a "quick start" example where we attempt to parse the following JSON file and access some data, without triggering exceptions:
360+
```JavaScript
361+
{
362+
"statuses": [
363+
{
364+
"id": 505874924095815700
365+
},
366+
{
367+
"id": 505874922023837700
368+
}
369+
],
370+
"search_metadata": {
371+
"count": 100
372+
}
373+
}
359374
```
375+
376+
Our program loads the file, selects value corresponding to key "search_metadata" which expected to be an object, and then
377+
it selects the key "count" within that object.
378+
379+
```C++
360380
#include "simdjson.h"
361381

362382
int main(void) {
363383
simdjson::dom::parser parser;
364-
365384
simdjson::dom::element tweets;
366385
auto error = parser.load("twitter.json").get(tweets);
367386
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
@@ -375,6 +394,32 @@ int main(void) {
375394
}
376395
```
377396
397+
398+
The following is a similar example where one wants to get the id of the first tweet without
399+
triggering exceptions. To do this, we use `["statuses"].at(0)["id"]`. We break that expression down:
400+
401+
- Get the list of tweets (the `"statuses"` key of the document) using `["statuses"]`). The result is expected to be an array.
402+
- Get the first tweet using `.at(0)`. The result is expected to be an object.
403+
- Get the id of the tweet using ["id"]. We expect the value to be a non-negative integer.
404+
405+
Observe how we use the `at` method when querying an index into an array, and not the bracket operator.
406+
407+
```
408+
#include "simdjson.h"
409+
410+
int main(void) {
411+
simdjson::dom::parser parser;
412+
simdjson::dom::element tweets;
413+
auto error = parser.load("twitter.json").get(tweets);
414+
if(error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
415+
uint64_t identifier;
416+
error = tweets["statuses"].at(0)["id"].get(identifier);
417+
if(error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
418+
std::cout << identifier << std::endl;
419+
return EXIT_SUCCESS;
420+
}
421+
```
422+
378423
### Error Handling Example
379424
380425
This is how the example in "Using the Parsed JSON" could be written using only error code checking:
@@ -506,6 +551,21 @@ dom::element doc = parser.parse(json); // Throws an exception if there was an er
506551
When used this way, a `simdjson_error` exception will be thrown if an error occurs, preventing the
507552
program from continuing if there was an error.
508553
554+
555+
556+
If one is willing to trigger exceptions, it is possible to write simpler code:
557+
558+
```
559+
#include "simdjson.h"
560+
561+
int main(void) {
562+
simdjson::dom::parser parser;
563+
simdjson::dom::element tweets = parser.load("twitter.json");
564+
std::cout << "ID: " << tweets["statuses"].at(0)["id"] << std::endl;
565+
return EXIT_SUCCESS;
566+
}
567+
```
568+
509569
Tree Walking and JSON Element Types
510570
-----------------------------------
511571

examples/quickstart/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
5757
add_quickstart_test(quickstart_noexceptions quickstart_noexceptions.cpp "" true)
5858
add_quickstart_test(quickstart_noexceptions11 quickstart_noexceptions.cpp c++11 true)
5959
set_property( TEST quickstart_noexceptions APPEND PROPERTY LABELS acceptance compile )
60+
61+
add_quickstart_test(quickstart2_noexceptions quickstart2_noexceptions.cpp "" true)
62+
add_quickstart_test(quickstart2_noexceptions11 quickstart2_noexceptions.cpp c++11 true)
63+
set_property( TEST quickstart2_noexceptions APPEND PROPERTY LABELS acceptance compile )
64+
6065
endif()

examples/quickstart/quickstart2.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "simdjson.h"
2+
3+
int main(void) {
4+
simdjson::dom::parser parser;
5+
simdjson::dom::element tweets = parser.load("twitter.json");
6+
std::cout << "ID: " << tweets["statuses"].at(0)["id"] << std::endl;
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "simdjson.h"
2+
3+
int main(void) {
4+
simdjson::dom::parser parser;
5+
simdjson::dom::element tweets;
6+
auto error = parser.load("twitter.json").get(tweets);
7+
if(error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
8+
uint64_t identifier;
9+
error = tweets["statuses"].at(0)["id"].get(identifier);
10+
if(error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
11+
std::cout << identifier << std::endl;
12+
return EXIT_SUCCESS;
13+
}

0 commit comments

Comments
 (0)