Skip to content

Commit cfcb315

Browse files
authored
Merge pull request simdjson#967 from simdjson/dlemire/improving_documentation
This improves slightly the documentation, adding instructions for CMake users.
2 parents 49d7023 + 8cc9f49 commit cfcb315

1 file changed

Lines changed: 53 additions & 31 deletions

File tree

doc/basics.md

Lines changed: 53 additions & 31 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)
@@ -43,6 +47,25 @@ Note:
4347
- Users on macOS and other platforms were default compilers do not provide C++11 compliant by default should request it with the appropriate flag (e.g., `c++ myproject.cpp simdjson.cpp`).
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

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

4770
The Basics: Loading and Parsing JSON Documents
4871
----------------------------------------------
@@ -168,6 +191,36 @@ And another one:
168191
cout << "number: " << v << endl;
169192
```
170193

194+
C++17 Support
195+
-------------
196+
197+
While the simdjson library can be used in any project using C++ 11 and above, field iteration has special support C++ 17's destructuring syntax. For example:
198+
199+
```c++
200+
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
201+
dom::parser parser;
202+
dom::object object;
203+
auto error = parser.parse(json).get(object);
204+
if (error) { cerr << error << endl; return; }
205+
for (auto [key, value] : object) {
206+
cout << key << " = " << value << endl;
207+
}
208+
```
209+
210+
For comparison, here is the C++ 11 version of the same code:
211+
212+
```c++
213+
// C++ 11 version for comparison
214+
padded_string json = R"( { "foo": 1, "bar": 2 } )"_padded;
215+
dom::parser parser;
216+
dom::object object;
217+
auto error = parser.parse(json).get(object);
218+
if (!error) { cerr << error << endl; return; }
219+
for (dom::key_value_pair field : object) {
220+
cout << field.key << " = " << field.value << endl;
221+
}
222+
```
223+
171224
Minifying JSON strings without parsing
172225
----------------------
173226

@@ -204,37 +257,6 @@ The UTF-8 validation function merely checks that the input is valid UTF-8: it wo
204257

205258
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.
206259

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

0 commit comments

Comments
 (0)