Skip to content

Commit 5eb748a

Browse files
committed
This improves slightly the documentation, adding instructions for CMake users.
1 parent aa4340e commit 5eb748a

1 file changed

Lines changed: 61 additions & 32 deletions

File tree

doc/basics.md

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ An overview of what you need to know to use simdjson, with examples.
55

66
* [Requirements](#requirements)
77
* [Including simdjson](#including-simdjson)
8+
* [Using simdjson as a CMake dependency](#using-simdjson-as-a-cmake-dependency)
89
* [The Basics: Loading and Parsing JSON Documents](#the-basics-loading-and-parsing-json-documents)
910
* [Using the Parsed JSON](#using-the-parsed-json)
11+
* [C++17 Support](#c++17-support)
12+
* [Minifying JSON strings without parsing](#minifying-json-strings-without-parsing)
13+
* [UTF-8 validation (alone)](#utf-8-validation-alone)
1014
* [JSON Pointer](#json-pointer)
1115
* [Error Handling](#error-handling)
1216
* [Error Handling Example](#error-handling-example)
@@ -44,6 +48,31 @@ Note:
4448
- Visual Studio users should compile with the `_CRT_SECURE_NO_WARNINGS` flag to avoid warnings with respect to our use of standard C functions such as `fopen`.
4549

4650

51+
52+
53+
Using simdjson as a CMake dependency
54+
------------------
55+
56+
57+
58+
You can include the simdjson repository as a folder in your CMake project. In the parent
59+
`CMakeLists.txt` include the following lines:
60+
61+
```
62+
set(SIMDJSON_JUST_LIBRARY ON CACHE STRING "Build just the library, nothing else." FORCE)
63+
add_subdirectory(simdjson EXCLUDE_FROM_ALL)
64+
```
65+
66+
Elsewhere in your project, you can declare dependencies on simdjson with lines such as these:
67+
68+
```
69+
add_executable(myprogram myprogram.cpp)
70+
71+
target_link_libraries(myprogram simdjson)
72+
```
73+
74+
See [our CMake demonstration](https://github.com/simdjson/cmakedemo).
75+
4776
The Basics: Loading and Parsing JSON Documents
4877
----------------------------------------------
4978

@@ -168,6 +197,38 @@ And another one:
168197
cout << "number: " << v << endl;
169198
```
170199

200+
C++17 Support
201+
-------------
202+
203+
While the simdjson library can be used in any project using C++ 11 and above, it has special support
204+
for C++ 17. The APIs for field iteration and error handling in particular are designed to work
205+
nicely with C++17's destructuring syntax. For example:
206+
207+
```c++
208+
dom::parser parser;
209+
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
210+
auto [object, error] = parser.parse(json).get<dom::object>();
211+
if (error) { cerr << error << endl; return; }
212+
for (auto [key, value] : object) {
213+
cout << key << " = " << value << endl;
214+
}
215+
```
216+
217+
For comparison, here is the C++ 11 version of the same code:
218+
219+
```c++
220+
// C++ 11 version for comparison
221+
dom::parser parser;
222+
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
223+
simdjson::error_code error;
224+
dom::object object;
225+
error = parser.parse(json).get(object);
226+
if (!error) { cerr << error << endl; return; }
227+
for (dom::key_value_pair field : object) {
228+
cout << field.key << " = " << field.value << endl;
229+
}
230+
```
231+
171232
Minifying JSON strings without parsing
172233
----------------------
173234

@@ -205,38 +266,6 @@ The UTF-8 validation function merely checks that the input is valid UTF-8: it wo
205266
Your input string does not need any padding. Any string will do. The `validate_utf8` function does not do any memory allocation on the heap, and it does not throw exceptions.
206267

207268

208-
C++17 Support
209-
-------------
210-
211-
While the simdjson library can be used in any project using C++ 11 and above, it has special support
212-
for C++ 17. The APIs for field iteration and error handling in particular are designed to work
213-
nicely with C++17's destructuring syntax. For example:
214-
215-
```c++
216-
dom::parser parser;
217-
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
218-
auto [object, error] = parser.parse(json).get<dom::object>();
219-
if (error) { cerr << error << endl; return; }
220-
for (auto [key, value] : object) {
221-
cout << key << " = " << value << endl;
222-
}
223-
```
224-
225-
For comparison, here is the C++ 11 version of the same code:
226-
227-
```c++
228-
// C++ 11 version for comparison
229-
dom::parser parser;
230-
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
231-
simdjson::error_code error;
232-
dom::object object;
233-
error = parser.parse(json).get(object);
234-
if (!error) { cerr << error << endl; return; }
235-
for (dom::key_value_pair field : object) {
236-
cout << field.key << " = " << field.value << endl;
237-
}
238-
```
239-
240269
JSON Pointer
241270
------------
242271

0 commit comments

Comments
 (0)