Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for comparing json and ordered_json #3599

Merged
merged 2 commits into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/examples/operator__equal__specializations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
#include <iomanip>
#include <nlohmann/json.hpp>

using json = nlohmann::json;

int main()
{
nlohmann::json uj1 = {{"version", 1}, {"type", "integer"}};
nlohmann::json uj2 = {{"type", "integer"}, {"version", 1}};

nlohmann::ordered_json oj1 = {{"version", 1}, {"type", "integer"}};
nlohmann::ordered_json oj2 = {{"type", "integer"}, {"version", 1}};

std::cout << std::boolalpha << (uj1 == uj2) << '\n' << (oj1 == oj2) << std::endl;
}
2 changes: 2 additions & 0 deletions docs/examples/operator__equal__specializations.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
true
false
33 changes: 33 additions & 0 deletions docs/mkdocs/docs/api/basic_json/operator_eq.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ Linear.
}
```

!!! note "Comparing different `basic_json` specializations"

Comparing different `basic_json` specializations can have surprising results. For instance, comparing the JSON
objects
nlohmann marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"version": 1,
"type": "integer"
}
```

and

```json
{
"type": "integer",
"version": 1
}
```

depends on whether [`nlohmann::json`](../json.md) order [`nlohmann::ordered_json`](../ordered_json.md) is used:
nlohmann marked this conversation as resolved.
Show resolved Hide resolved

```cpp
--8<-- "examples/operator__equal__specializations.cpp"
```

Output:

```json
--8<-- "examples/operator__equal__specializations.output"
```

## Examples

??? example
Expand Down