mirror of
https://github.com/nlohmann/json.git
synced 2026-10-13 01:47:11 +00:00
deploy: 40a62947d3
This commit is contained in:
1 parent
79119509a9
commit
22fba27145
555 files changed
+17342
-1394
No files matched your search
@@ -46,13 +46,93 @@ same name. Hidden members remain accessible via [`as_base_class`](as_base_class.
|
||||
The following code shows how to inject custom data and methods for each node.
|
||||
|
||||
```cpp
|
||||
--8<-- "examples/json_base_class_t.cpp"
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
class visitor_adaptor_with_metadata
|
||||
{
|
||||
public:
|
||||
template <class Fnc>
|
||||
void visit(const Fnc& fnc) const;
|
||||
|
||||
int metadata = 42;
|
||||
private:
|
||||
template <class Ptr, class Fnc>
|
||||
void do_visit(const Ptr& ptr, const Fnc& fnc) const;
|
||||
};
|
||||
|
||||
using json = nlohmann::json::with_base_class_t<visitor_adaptor_with_metadata>;
|
||||
|
||||
template <class Fnc>
|
||||
void visitor_adaptor_with_metadata::visit(const Fnc& fnc) const
|
||||
{
|
||||
do_visit(json::json_pointer{}, fnc);
|
||||
}
|
||||
|
||||
template <class Ptr, class Fnc>
|
||||
void visitor_adaptor_with_metadata::do_visit(const Ptr& ptr, const Fnc& fnc) const
|
||||
{
|
||||
using value_t = nlohmann::detail::value_t;
|
||||
const json& j = *static_cast<const json*>(this);
|
||||
switch (j.type())
|
||||
{
|
||||
case value_t::object:
|
||||
fnc(ptr, j);
|
||||
for (const auto& entry : j.items())
|
||||
{
|
||||
entry.value().do_visit(ptr / entry.key(), fnc);
|
||||
}
|
||||
break;
|
||||
case value_t::array:
|
||||
fnc(ptr, j);
|
||||
for (std::size_t i = 0; i < j.size(); ++i)
|
||||
{
|
||||
j.at(i).do_visit(ptr / std::to_string(i), fnc);
|
||||
}
|
||||
break;
|
||||
case value_t::null:
|
||||
case value_t::string:
|
||||
case value_t::boolean:
|
||||
case value_t::number_integer:
|
||||
case value_t::number_unsigned:
|
||||
case value_t::number_float:
|
||||
case value_t::binary:
|
||||
fnc(ptr, j);
|
||||
break;
|
||||
case value_t::discarded:
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// create a json object
|
||||
json j;
|
||||
j["null"];
|
||||
j["object"]["uint"] = 1U;
|
||||
j["object"].metadata = 21;
|
||||
|
||||
// visit and output
|
||||
j.visit(
|
||||
[&](const json::json_pointer & p,
|
||||
const json & j)
|
||||
{
|
||||
std::cout << (p.empty() ? std::string{"/"} : p.to_string())
|
||||
<< " - metadata = " << j.metadata << " -> " << j.dump() << '\n';
|
||||
});
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Output:
|
||||
|
||||
```json
|
||||
--8<-- "examples/json_base_class_t.output"
|
||||
/ - metadata = 42 -> {"null":null,"object":{"uint":1}}
|
||||
/null - metadata = 42 -> null
|
||||
/object - metadata = 21 -> {"uint":1}
|
||||
/object/uint - metadata = 42 -> 1
|
||||
|
||||
```
|
||||
|
||||
## See also
|
||||
@@ -62,5 +142,5 @@ same name. Hidden members remain accessible via [`as_base_class`](as_base_class.
|
||||
|
||||
## Version history
|
||||
|
||||
- Added in version 3.12.0.
|
||||
- Added in version 3.11.3.
|
||||
- Made a public member type in version 3.13.0; it was private before, so it could not be named outside the class.
|
||||
Reference in new issue
Block a user