blob: c47586f7e459b71988e17d5a2df949d4c7bb8486 [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -08001From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2From: PJ Reiniger <pj.reiniger@gmail.com>
3Date: Wed, 20 Sep 2023 02:23:10 -0400
4Subject: [PATCH 4/4] Add llvm stream support
5
6---
7 .../detail/output/output_adapters.hpp | 26 +++++++++++++++++++
8 include/nlohmann/detail/output/serializer.hpp | 11 ++++++--
9 include/nlohmann/json.hpp | 24 +++++++++++++++++
10 3 files changed, 59 insertions(+), 2 deletions(-)
11
12diff --git a/include/nlohmann/detail/output/output_adapters.hpp b/include/nlohmann/detail/output/output_adapters.hpp
13index 630bd8f73f38b7bf18be571217873f6215e6e31a..78addc557eec3b2a31cde78fb4c6f7f6efc7e777 100644
14--- a/include/nlohmann/detail/output/output_adapters.hpp
15+++ b/include/nlohmann/detail/output/output_adapters.hpp
16@@ -22,6 +22,8 @@
17
18 #include <nlohmann/detail/macro_scope.hpp>
19
20+#include <wpi/raw_ostream.h>
21+
22 NLOHMANN_JSON_NAMESPACE_BEGIN
23 namespace detail
24 {
25@@ -118,6 +120,27 @@ class output_string_adapter : public output_adapter_protocol<CharType>
26 StringType& str;
27 };
28
29+template<typename CharType>
30+class raw_ostream_adapter : public output_adapter_protocol<CharType>
31+{
32+ public:
33+ explicit raw_ostream_adapter(raw_ostream& s) noexcept
34+ : os(s) {}
35+
36+
37+ void write_character(CharType c) override {
38+ os << c;
39+ }
40+
41+ JSON_HEDLEY_NON_NULL(2)
42+ void write_characters(const CharType* s, std::size_t length) override {
43+ os.write(s, length);
44+ }
45+
46+ private:
47+ raw_ostream& os;
48+};
49+
50 template<typename CharType, typename StringType = std::basic_string<CharType>>
51 class output_adapter
52 {
53@@ -134,6 +157,9 @@ class output_adapter
54 output_adapter(StringType& s)
55 : oa(std::make_shared<output_string_adapter<CharType, StringType>>(s)) {}
56
57+ output_adapter(raw_ostream& os)
58+ : oa(std::make_shared<raw_ostream_adapter<CharType>>(os)) {}
59+
60 operator output_adapter_t<CharType>()
61 {
62 return oa;
63diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp
64index ecc4f7d500b9e0bc15917503061a4db100391366..bb392a985b57b79020c949593c155052a4271d6b 100644
65--- a/include/nlohmann/detail/output/serializer.hpp
66+++ b/include/nlohmann/detail/output/serializer.hpp
67@@ -65,15 +65,22 @@ class serializer
68 @param[in] error_handler_ how to react on decoding errors
69 */
70 serializer(output_adapter_t<char> s, const char ichar,
71- error_handler_t error_handler_ = error_handler_t::strict)
72+ error_handler_t error_handler_ = error_handler_t::strict,
73+ size_t indent_init_len = 512)
74 : o(std::move(s))
75 , loc(std::localeconv())
76 , thousands_sep(loc->thousands_sep == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->thousands_sep)))
77 , decimal_point(loc->decimal_point == nullptr ? '\0' : std::char_traits<char>::to_char_type(* (loc->decimal_point)))
78 , indent_char(ichar)
79- , indent_string(512, indent_char)
80+ , indent_string(indent_init_len, indent_char)
81 , error_handler(error_handler_)
82 {}
83+
84+ serializer(raw_ostream& os, const char ichar,
85+ size_t indent_init_len = 512,
86+ error_handler_t error_handler_ = error_handler_t::strict)
87+ : serializer(output_adapter<char>(os), ichar, error_handler_, indent_init_len)
88+ {}
89
90 // delete because of pointer members
91 serializer(const serializer&) = delete;
92diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp
93index c462cade8a7167a00697f6f940be35c5609a283c..ad98956ba880f844ed1a17765266880f6ea08b2f 100644
94--- a/include/nlohmann/json.hpp
95+++ b/include/nlohmann/json.hpp
96@@ -1275,6 +1275,24 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
97 return result;
98 }
99
100+ void dump(raw_ostream& os, const int indent = -1,
101+ const char indent_char = ' ',
102+ const bool ensure_ascii = false,
103+ const error_handler_t error_handler = error_handler_t::strict) const {
104+ serializer s(os, indent_char);
105+
106+ if (indent >= 0)
107+ {
108+ s.dump(*this, true, ensure_ascii, static_cast<unsigned int>(indent));
109+ }
110+ else
111+ {
112+ s.dump(*this, false, ensure_ascii, 0);
113+ }
114+
115+ os.flush();
116+ }
117+
118 /// @brief return the type of the JSON value (explicit)
119 /// @sa https://json.nlohmann.me/api/basic_json/type/
120 constexpr value_t type() const noexcept
121@@ -3990,6 +4008,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
122 return o << j;
123 }
124 #endif // JSON_NO_IO
125+
126+ friend raw_ostream& operator<<(raw_ostream& o, const basic_json& j)
127+ {
128+ j.dump(o, 0);
129+ return o;
130+ }
131 /// @}
132
133