Skip to content

Commit cd15745

Browse files
committed
Support serializing a JSON into an std::ostream
1 parent a53ce64 commit cd15745

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/base/json.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ String icinga::JsonEncode(const Value& value, bool pretty_print)
132132
return json.dump(pretty_print ? l_JsonIndentSize : -1, ' ', true);
133133
}
134134

135+
/**
136+
* Serializes an Icinga Value into a JSON object and writes it to the given output stream.
137+
*
138+
* @param value The value to be JSON serialized.
139+
* @param os The output stream to write the JSON data to.
140+
* @param pretty_print Whether to pretty print the JSON data.
141+
*/
142+
void icinga::JsonEncode(const Value& value, std::ostream& os, bool pretty_print)
143+
{
144+
using namespace nlohmann;
145+
detail::serializer<json> s(nlohmann::detail::output_adapter(os), ' ', json::error_handler_t::strict);
146+
s.dump(json(value), pretty_print, true, pretty_print ? l_JsonIndentSize : 0);
147+
}
148+
135149
Value icinga::JsonDecode(const String& data)
136150
{
137151
String sanitized (Utility::ValidateUTF8(data));

lib/base/json.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class String;
1212
class Value;
1313

1414
String JsonEncode(const Value& value, bool pretty_print = false);
15+
void JsonEncode(const Value& value, std::ostream& os, bool pretty_print = false);
1516
Value JsonDecode(const String& data);
1617

1718
}

0 commit comments

Comments
 (0)