This commit is contained in:
nlohmann committed 2026-10-26 21:24:35 +00:00
1 parent 41c601a7d1
commit d2e264bd84
1400 files changed
+11859 -5440

No files matched your search

@@ -37,6 +37,10 @@ Important notes
- If an enum or JSON value is specified in multiple conversions, the first matching conversion from the top of the list will be returned when converting to or from JSON. See example 2 below.
- Maps with enum keys (e.g., `std::map<ENUM_TYPE, T>`) are stored as arrays of `[key, value]` pairs by default. Define [`JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS`](https://json.nlohmann.me/api/macros/json_use_objects_for_enum_keyed_maps/index.md) to store them as objects with the converted keys. Such maps can be read from both forms.
Views
The generated `from_json()` also reads the views of [`json_document`](https://json.nlohmann.me/api/json_document/index.md)s (`<nlohmann/json_view.hpp>`), comparing the view with the entries of the mapping, without building a `json` value first.
## Examples
Example: Basic usage
@@ -47,7 +51,7 @@ The example shows how `NLOHMANN_JSON_SERIALIZE_ENUM_STRICT` can be used to seria
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -85,7 +89,7 @@ int main()
json j_stopped = ns::TS_STOPPED;
json j_red = ns::Color::red;
std::cout << "ns::TS_STOPPED -> " << j_stopped
<< ", ns::Color::red -> " << j_red << std::endl;
<< ", ns::Color::red -> " << j_red << '\n';
// deserialization
json j_running = "running";
@@ -93,7 +97,7 @@ int main()
auto running = j_running.get<ns::TaskState>();
auto blue = j_blue.get<ns::Color>();
std::cout << j_running << " -> " << running
<< ", " << j_blue << " -> " << static_cast<int>(blue) << std::endl;
<< ", " << j_blue << " -> " << static_cast<int>(blue) << '\n';
}
```
@@ -113,7 +117,7 @@ The example shows how to use multiple conversions for a single enumerator. In th
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -134,14 +138,14 @@ int main()
{
// serialization
json j_red = ns::Color::red;
std::cout << static_cast<int>(ns::Color::red) << " -> " << j_red << std::endl;
std::cout << static_cast<int>(ns::Color::red) << " -> " << j_red << '\n';
// deserialization
json j_rot = "rot";
auto rot = j_rot.get<ns::Color>();
auto red = j_red.get<ns::Color>();
std::cout << j_rot << " -> " << static_cast<int>(rot) << std::endl;
std::cout << j_red << " -> " << static_cast<int>(red) << std::endl;
std::cout << j_rot << " -> " << static_cast<int>(rot) << '\n';
std::cout << j_red << " -> " << static_cast<int>(red) << '\n';
}
```
@@ -161,7 +165,7 @@ The example shows how an invalid serialization causes an exception to be thrown.
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -194,7 +198,7 @@ int main()
}
catch (const json::exception e)
{
std::cout << "deserialization failed: " << e.what() << std::endl;
std::cout << "deserialization failed: " << e.what() << '\n';
}
// invalid deserialization
@@ -206,7 +210,7 @@ int main()
}
catch (const json::exception e)
{
std::cout << "deserialization failed: " << e.what() << std::endl;
std::cout << "deserialization failed: " << e.what() << '\n';
}
return 0;