Skip to content

Commit 2d48a4d

Browse files
Add documentation for comparing json and ordered_json (#3599)
* 📝 add documentation for #3443 * Apply suggestions from code review Co-authored-by: Florian Albrechtskirchinger <[email protected]>
1 parent e91686c commit 2d48a4d

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <iomanip>
3+
#include <nlohmann/json.hpp>
4+
5+
using json = nlohmann::json;
6+
7+
int main()
8+
{
9+
nlohmann::json uj1 = {{"version", 1}, {"type", "integer"}};
10+
nlohmann::json uj2 = {{"type", "integer"}, {"version", 1}};
11+
12+
nlohmann::ordered_json oj1 = {{"version", 1}, {"type", "integer"}};
13+
nlohmann::ordered_json oj2 = {{"type", "integer"}, {"version", 1}};
14+
15+
std::cout << std::boolalpha << (uj1 == uj2) << '\n' << (oj1 == oj2) << std::endl;
16+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
true
2+
false

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,39 @@ Linear.
9999
}
100100
```
101101
102+
!!! note "Comparing different `basic_json` specializations"
103+
104+
Comparing different `basic_json` specializations can have surprising effects. For instance, the result of comparing
105+
the JSON objects
106+
107+
```json
108+
{
109+
"version": 1,
110+
"type": "integer"
111+
}
112+
```
113+
114+
and
115+
116+
```json
117+
{
118+
"type": "integer",
119+
"version": 1
120+
}
121+
```
122+
123+
depends on whether [`nlohmann::json`](../json.md) or [`nlohmann::ordered_json`](../ordered_json.md) is used:
124+
125+
```cpp
126+
--8<-- "examples/operator__equal__specializations.cpp"
127+
```
128+
129+
Output:
130+
131+
```json
132+
--8<-- "examples/operator__equal__specializations.output"
133+
```
134+
102135
## Examples
103136
104137
??? example

0 commit comments

Comments
 (0)