Skip to content

Commit 7b6cf59

Browse files
Documentation change (#3672)
Co-authored-by: Florian Albrechtskirchinger <[email protected]>
1 parent 9e1a7c8 commit 7b6cf59

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+583
-303
lines changed

.github/workflows/ubuntu.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ jobs:
151151

152152
ci_test_documentation:
153153
runs-on: ubuntu-latest
154+
strategy:
155+
matrix:
156+
target: [ci_test_examples, ci_test_api_documentation]
154157
steps:
155158
- uses: actions/checkout@v3
156159
- name: Run CMake
157160
run: cmake -S . -B build -DJSON_CI=On
158161
- name: Build
159-
run: cmake --build build --target ci_test_documentation
162+
run: cmake --build build --target ${{ matrix.target }}

cmake/ci.cmake

+7-1
Original file line numberDiff line numberDiff line change
@@ -953,12 +953,18 @@ add_custom_target(ci_icpc
953953
# test documentation
954954
###############################################################################
955955

956-
add_custom_target(ci_test_documentation
956+
add_custom_target(ci_test_examples
957957
COMMAND make CXX="${GCC_TOOL}" check_output_portable -j8
958958
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/docs
959959
COMMENT "Check that all examples compile and create the desired output"
960960
)
961961

962+
add_custom_target(ci_test_api_documentation
963+
COMMAND ${Python3_EXECUTABLE} scripts/check_structure.py
964+
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/docs/mkdocs
965+
COMMENT "Lint the API documentation"
966+
)
967+
962968
###############################################################################
963969
# Clean up all generated files.
964970
###############################################################################
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
namespace ns
7+
{
8+
// a simple struct to model a person
9+
struct person
10+
{
11+
std::string name;
12+
std::string address;
13+
int age;
14+
};
15+
} // namespace ns
16+
17+
namespace ns
18+
{
19+
void from_json(const json& j, person& p)
20+
{
21+
j.at("name").get_to(p.name);
22+
j.at("address").get_to(p.address);
23+
j.at("age").get_to(p.age);
24+
}
25+
} // namespace ns
26+
27+
int main()
28+
{
29+
json j;
30+
j["name"] = "Ned Flanders";
31+
j["address"] = "744 Evergreen Terrace";
32+
j["age"] = 60;
33+
34+
auto p = j.get<ns::person>();
35+
36+
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ned Flanders (60) lives in 744 Evergreen Terrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
namespace ns
7+
{
8+
// a simple struct to model a person (not default constructible)
9+
struct person
10+
{
11+
person(std::string n, std::string a, int aa)
12+
: name(std::move(n)), address(std::move(a)), age(aa)
13+
{}
14+
15+
std::string name;
16+
std::string address;
17+
int age;
18+
};
19+
} // namespace ns
20+
21+
namespace nlohmann
22+
{
23+
template <>
24+
struct adl_serializer<ns::person>
25+
{
26+
static ns::person from_json(const json& j)
27+
{
28+
return {j.at("name"), j.at("address"), j.at("age")};
29+
}
30+
31+
// Here's the catch! You must provide a to_json method! Otherwise, you
32+
// will not be able to convert person to json, since you fully
33+
// specialized adl_serializer on that type
34+
static void to_json(json& j, ns::person p)
35+
{
36+
j["name"] = p.name;
37+
j["address"] = p.address;
38+
j["age"] = p.age;
39+
}
40+
};
41+
} // namespace nlohmann
42+
43+
int main()
44+
{
45+
json j;
46+
j["name"] = "Ned Flanders";
47+
j["address"] = "744 Evergreen Terrace";
48+
j["age"] = 60;
49+
50+
auto p = j.get<ns::person>();
51+
52+
std::cout << p.name << " (" << p.age << ") lives in " << p.address << std::endl;
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ned Flanders (60) lives in 744 Evergreen Terrace
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
// possible use case: use NLOHMANN_JSON_NAMESPACE instead of nlohmann
5+
using json = NLOHMANN_JSON_NAMESPACE::json;
6+
7+
// macro needed to output the NLOHMANN_JSON_NAMESPACE as string literal
8+
#define Q(x) #x
9+
#define QUOTE(x) Q(x)
10+
11+
int main()
12+
{
13+
std::cout << QUOTE(NLOHMANN_JSON_NAMESPACE) << std::endl;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nlohmann::json_v3_11_1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <iostream>
2+
#include <optional>
3+
#include <nlohmann/json.hpp>
4+
5+
// partial specialization (see https://json.nlohmann.me/features/arbitrary_types/)
6+
NLOHMANN_JSON_NAMESPACE_BEGIN
7+
template <typename T>
8+
struct adl_serializer<std::optional<T>>
9+
{
10+
static void to_json(json& j, const std::optional<T>& opt)
11+
{
12+
if (opt == std::nullopt)
13+
{
14+
j = nullptr;
15+
}
16+
else
17+
{
18+
j = *opt;
19+
}
20+
}
21+
};
22+
NLOHMANN_JSON_NAMESPACE_END
23+
24+
int main()
25+
{
26+
std::optional<int> o1 = 1;
27+
std::optional<int> o2 = std::nullopt;
28+
29+
NLOHMANN_JSON_NAMESPACE::json j;
30+
j.push_back(o1);
31+
j.push_back(o2);
32+
std::cout << j << std::endl;
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[1,null]

docs/examples/to_json.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
namespace ns
7+
{
8+
// a simple struct to model a person
9+
struct person
10+
{
11+
std::string name;
12+
std::string address;
13+
int age;
14+
};
15+
} // namespace ns
16+
17+
namespace ns
18+
{
19+
void to_json(json& j, const person& p)
20+
{
21+
j = json{ {"name", p.name}, {"address", p.address}, {"age", p.age} };
22+
}
23+
} // namespace ns
24+
25+
int main()
26+
{
27+
ns::person p = {"Ned Flanders", "744 Evergreen Terrace", 60};
28+
29+
json j = p;
30+
31+
std::cout << j << std::endl;
32+
}

docs/examples/to_json.output

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"address":"744 Evergreen Terrace","age":60,"name":"Ned Flanders"}

docs/mkdocs/docs/api/adl_serializer/from_json.md

+36-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_
1414
-> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::identity_tag<TargetType> {}))
1515
```
1616
17-
This function is usually called by the [`get()`](../basic_json/get.md) function of the
18-
[basic_json](../basic_json) class (either explicit or via conversion operators).
17+
This function is usually called by the [`get()`](../basic_json/get.md) function of the [basic_json](../basic_json)
18+
class (either explicitly or via the conversion operators).
1919
2020
1. This function is chosen for default-constructible value types.
2121
2. This function is chosen for value types which are not default-constructible.
@@ -32,9 +32,41 @@ This function is usually called by the [`get()`](../basic_json/get.md) function
3232
3333
Copy of the JSON value, converted to `ValueType`
3434
35-
!!! note
35+
## Examples
3636
37-
This documentation page is a stub.
37+
??? example "Example: (1) Default-constructible type"
38+
39+
The example below shows how a `from_json` function can be implemented for a user-defined type. This function is
40+
called by the `adl_serializer` when `get<ns::person>()` is called.
41+
42+
```cpp
43+
--8<-- "examples/from_json__default_constructible.cpp"
44+
```
45+
46+
Output:
47+
48+
```json
49+
--8<-- "examples/from_json__default_constructible.output"
50+
```
51+
52+
??? example "Example: (2) Non-default-constructible type"
53+
54+
The example below shows how a `from_json` is implemented as part of a specialization of the `adl_serializer` to
55+
realize the conversion of a non-default-constructible type.
56+
57+
```cpp
58+
--8<-- "examples/from_json__non_default_constructible.cpp"
59+
```
60+
61+
Output:
62+
63+
```json
64+
--8<-- "examples/from_json__non_default_constructible.output"
65+
```
66+
67+
## See also
68+
69+
- [to_json](to_json.md)
3870
3971
## Version history
4072

docs/mkdocs/docs/api/adl_serializer/to_json.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,26 @@ This function is usually called by the constructors of the [basic_json](../basic
1717
`val` (in)
1818
: value to read from
1919
20-
!!! note
21-
22-
This documentation page is a stub.
20+
## Examples
21+
22+
??? example
23+
24+
The example below shows how a `to_json` function can be implemented for a user-defined type. This function is called
25+
by the `adl_serializer` when the constructor `basic_json(ns::person)` is called.
26+
27+
```cpp
28+
--8<-- "examples/to_json.cpp"
29+
```
30+
31+
Output:
32+
33+
```json
34+
--8<-- "examples/to_json.output"
35+
```
36+
37+
## See also
38+
39+
- [from_json](from_json.md)
2340
2441
## Version history
2542

docs/mkdocs/docs/api/basic_json/boolean_t.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ using boolean_t = BooleanType;
66

77
The type used to store JSON booleans.
88

9-
[RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two literals
10-
`#!json true` and `#!json false`.
9+
[RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two
10+
literals `#!json true` and `#!json false`.
1111

1212
To store objects in C++, a type is defined by the template parameter `BooleanType` which chooses the type to use.
1313

docs/mkdocs/docs/api/basic_json/json_serializer.md

+17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ using json_serializer = JSONSerializer<T, SFINAE>;
1919

2020
The default values for `json_serializer` is [`adl_serializer`](../adl_serializer).
2121

22+
## Examples
23+
24+
??? example
25+
26+
The example below shows how a conversion of a non-default-constructible type is implemented via a specialization of
27+
the `adl_serializer`.
28+
29+
```cpp
30+
--8<-- "examples/from_json__non_default_constructible.cpp"
31+
```
32+
33+
Output:
34+
35+
```json
36+
--8<-- "examples/from_json__non_default_constructible.output"
37+
```
38+
2239
## Version history
2340

2441
- Since version 2.0.0.

docs/mkdocs/docs/api/basic_json/object_comparator_t.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ and [`default_object_comparator_t`](default_object_comparator_t.md) otherwise.
2828
## Version history
2929

3030
- Added in version 3.0.0.
31-
- Changed to be conditionally defined as `#!cpp typename object_t::key_compare` or `default_object_comparator_t` in version 3.11.0.
31+
- Changed to be conditionally defined as `#!cpp typename object_t::key_compare` or `default_object_comparator_t` in
32+
version 3.11.0.

docs/mkdocs/docs/api/basic_json/object_t.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ Objects are stored as pointers in a `basic_json` type. That is, for any access t
9090
The order name/value pairs are added to the object is *not* preserved by the library. Therefore, iterating an object may
9191
return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in
9292
alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to
93-
[RFC 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON objects.
93+
[RFC 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON
94+
objects.
9495

9596
## Examples
9697

docs/mkdocs/docs/api/basic_json/operator[].md

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const_reference operator[](const json_pointer& ptr) const;
2121
```
2222

2323
1. Returns a reference to the array element at specified location `idx`.
24-
2. Returns a reference to the object element with specified key `key`. The non-const qualified overload takes the key by value.
24+
2. Returns a reference to the object element with specified key `key`. The non-const qualified overload takes the key by
25+
value.
2526
3. See 2. This overload is only available if `KeyType` is comparable with `#!cpp typename object_t::key_type` and
2627
`#!cpp typename object_comparator_t::is_transparent` denotes a type.
2728
4. Returns a reference to the element with specified JSON pointer `ptr`.
@@ -234,6 +235,7 @@ Strong exception safety: if an exception occurs, the original value stays intact
234235
## Version history
235236

236237
1. Added in version 1.0.0.
237-
2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. Removed overloads for `T* key` (replaced by 3) in version 3.11.0.
238+
2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. Removed overloads for `T* key` (replaced by 3)
239+
in version 3.11.0.
238240
3. Added in version 3.11.0.
239241
4. Added in version 2.0.0.

docs/mkdocs/docs/api/basic_json/operator_eq.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class basic_json {
2020
```
2121
2222
1. Compares two JSON values for equality according to the following rules:
23-
- Two JSON values are equal if (1) neither value is discarded, or (2) they are of the same
24-
type and their stored values are the same according to their respective `operator==`.
23+
- Two JSON values are equal if (1) neither value is discarded, or (2) they are of the same type and their stored
24+
values are the same according to their respective `operator==`.
2525
- Integer and floating-point numbers are automatically converted before comparison.
2626
2727
2. Compares a JSON value and a scalar or a scalar and a JSON value for equality by converting the

0 commit comments

Comments
 (0)