You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/leaf.adoc
+94-28Lines changed: 94 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1813,28 +1813,28 @@ To support multiple output formats, pass multiple functions to `h.dispatch`:
1813
1813
[source,c++]
1814
1814
----
1815
1815
h.dispatch(
1816
-
[&](nlohmann_writer & nw) { write_nested(nw, e, name); },
1816
+
[&](json_writer_nlohmann & w) { write_nested(w, e, name); },
1817
1817
[&](xml_writer & xw) { write_nested(xw, e, name); }
1818
1818
);
1819
1819
----
1820
1820
1821
1821
==== JSON Serialization
1822
1822
1823
-
LEAF provides `nlohmann_writer`, a class template for JSON serialization. The `nlohmann_writer` class defines `write` and `write_nested` friend functions (see <<tutorial-serialization>>) that work with:
1823
+
LEAF provides writers for JSON serialization with two popular JSON libraries:
1824
1824
1825
-
* Types for which `to_json` overloads can be found via ADL
1826
-
* Types for which a `write` overload can be found via ADL (e.g. LEAF types)
1825
+
* <<json_writer_nlohmann>> for https://github.com/nlohmann/json[nlohmann/json]
1826
+
* <<json_writer_boost>> for https://www.boost.org/doc/libs/release/libs/json/[Boost.JSON]
1827
1827
1828
-
This interface is compatible with https://github.com/nlohmann/json[nlohmann/json], we just need to define the required `serialize` function template (see <<custom-writers>>):
1828
+
Below is an example using `json_writer_nlohmann`. We just need to define the required `serialize` function template (see <<custom-writers>>):
// Enabled if x is assignable to boost::json::value, or
4492
+
// if tag_invoke is defined for boost::json::value_from_tag.
4493
+
template <class T>
4494
+
friend void write( json_writer_boost &, T const & x );
4495
+
4496
+
template <class T>
4497
+
friend void write_nested( json_writer_boost &, T const &, char const * name );
4498
+
};
4499
+
}
4500
+
4501
+
} }
4502
+
----
4503
+
4504
+
The `json_writer_boost` type serializes error objects to JSON format using https://www.boost.org/doc/libs/release/libs/json/[Boost.JSON]. The `write` function is enabled for:
4505
+
4506
+
* Types directly assignable to `boost::json::value`
4507
+
* Types for which a `tag_invoke` overload for `value_from_tag` can be found via ADL
// Enabled for types for which a suitable to_json overload
4461
-
// can be found via ADL, but a suitable write overload cannot.
4528
+
// Enabled if to_json is defined for Json and T.
4462
4529
template <class T>
4463
-
friend auto write( nlohmann_writer &, T const & x )
4464
-
-> decltype(to_json(std::declval<Json &>(), x));
4530
+
friend void write( json_writer_nlohmann &, T const & x );
4465
4531
4466
4532
template <class T>
4467
-
friend void write_nested( nlohmann_writer &, T const &, char const * name );
4533
+
friend void write_nested( json_writer_nlohmann &, T const &, char const * name );
4468
4534
};
4469
4535
}
4470
4536
4471
4537
} }
4472
4538
----
4473
4539
4474
-
The `nlohmann_writer` class template serializes error objects to JSON format using unqualified calls to `to_json`. This is compatible with https://github.com/nlohmann/json[nlohmann/json]; See <<tutorial-serialization>>.
4540
+
The `json_writer_nlohmann` serializes error objects to JSON format using unqualified calls to `to_json`. This is compatible with https://github.com/nlohmann/json[nlohmann/json]; See <<tutorial-serialization>>.
0 commit comments