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

+16 -8
View File
@@ -45,6 +45,12 @@ inline void from_json(const BasicJsonType& j, type& e);
Define [`JSON_USE_OBJECTS_FOR_ENUM_KEYED_MAPS`](json_use_objects_for_enum_keyed_maps.md) to store them as objects
with the converted keys. Such maps can be read from both forms.
!!! info "Views"
The generated `from_json()` also reads the views of [`json_document`](../json_document.md)s
(`<nlohmann/json_view.hpp>`), comparing the view with the entries of the mapping, without building a `json` value
first.
## Examples
??? example "Example: Basic usage"
@@ -56,7 +62,7 @@ inline void from_json(const BasicJsonType& j, type& e);
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -94,7 +100,7 @@ inline void from_json(const BasicJsonType& j, type& e);
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";
@@ -102,7 +108,7 @@ inline void from_json(const BasicJsonType& j, type& e);
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';
// deserializing undefined JSON value to enum
// (where the first map entry above is the default)
@@ -110,7 +116,7 @@ inline void from_json(const BasicJsonType& j, type& e);
auto invalid = j_pi.get<ns::TaskState>();
auto unknown = j_pi.get<ns::Color>();
std::cout << j_pi << " -> " << invalid << ", "
<< j_pi << " -> " << static_cast<int>(unknown) << std::endl;
<< j_pi << " -> " << static_cast<int>(unknown) << '\n';
}
```
@@ -134,7 +140,7 @@ inline void from_json(const BasicJsonType& j, type& e);
#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using nlohmann::json;
namespace ns
{
@@ -155,14 +161,14 @@ inline void from_json(const BasicJsonType& j, type& e);
{
// 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';
}
```
@@ -186,3 +192,5 @@ inline void from_json(const BasicJsonType& j, type& e);
## Version history
Added in version 3.4.0.
The generated `from_json()` reads the views of `json_document`s since version 3.13.0.