From 1f459bc05a45ae25c0e68c9b28b14444f09a53d6 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Wed, 18 Feb 2015 16:33:18 +0100 Subject: added writeOusia methods to VariantWriter. --- src/core/common/VariantWriter.cpp | 31 ++++++++++++++++++++++--------- src/core/common/VariantWriter.hpp | 22 +++++++++++++++++++++- 2 files changed, 43 insertions(+), 10 deletions(-) (limited to 'src/core/common') diff --git a/src/core/common/VariantWriter.cpp b/src/core/common/VariantWriter.cpp index 427ac5d..b1bafe4 100644 --- a/src/core/common/VariantWriter.cpp +++ b/src/core/common/VariantWriter.cpp @@ -104,8 +104,9 @@ static void writeLinebreak(std::ostream &stream, bool pretty) * @param pretty if true, the resulting value is properly indented. * @param level is the current indentation level. */ -static void writeJsonInternal(const Variant &var, std::ostream &stream, - bool pretty, int level) +template +static void writeInternal(const Variant &var, std::ostream &stream, bool pretty, + int level) { switch (var.getType()) { case VariantType::NULLPTR: @@ -127,7 +128,8 @@ static void writeJsonInternal(const Variant &var, std::ostream &stream, const Variant::arrayType &arr = var.asArray(); for (size_t i = 0; i < arr.size(); i++) { writeIndentation(stream, pretty, level + 1); - writeJsonInternal(arr[i], stream, pretty, level + 1); + writeInternal( + arr[i], stream, pretty, level + 1); if (i + 1 != arr.size()) { stream << ","; } @@ -139,21 +141,22 @@ static void writeJsonInternal(const Variant &var, std::ostream &stream, } case VariantType::MAP: { writeIndentation(stream, pretty, level); - stream << "{"; + stream << ObjectStart; writeLinebreak(stream, pretty); const Variant::mapType &map = var.asMap(); for (auto it = map.cbegin(); it != map.cend();) { writeIndentation(stream, pretty, level + 1); writeJsonString(it->first, stream); - stream << (pretty ? ": " : ":"); - writeJsonInternal(it->second, stream, pretty, level + 1); + stream << Equals << (pretty ? " " : ""); + writeInternal( + it->second, stream, pretty, level + 1); if ((++it) != map.cend()) { stream << ","; } writeLinebreak(stream, pretty); } writeIndentation(stream, pretty, level); - stream << "}"; + stream << ObjectEnd; return; } } @@ -162,7 +165,7 @@ static void writeJsonInternal(const Variant &var, std::ostream &stream, void VariantWriter::writeJson(const Variant &var, std::ostream &stream, bool pretty) { - writeJsonInternal(var, stream, pretty, 0); + writeInternal<'{', '}', ':'>(var, stream, pretty, 0); } std::string VariantWriter::writeJsonToString(const Variant &var, bool pretty) @@ -172,6 +175,16 @@ std::string VariantWriter::writeJsonToString(const Variant &var, bool pretty) return ss.str(); } - +void VariantWriter::writeOusia(const Variant &var, std::ostream &stream, + bool pretty) +{ + writeInternal<'[', ']', '='>(var, stream, pretty, 0); } +std::string VariantWriter::writeOusiaToString(const Variant &var, bool pretty) +{ + std::stringstream ss; + writeOusia(var, ss, pretty); + return ss.str(); +} +} \ No newline at end of file diff --git a/src/core/common/VariantWriter.hpp b/src/core/common/VariantWriter.hpp index 7fe32fb..12f4bba 100644 --- a/src/core/common/VariantWriter.hpp +++ b/src/core/common/VariantWriter.hpp @@ -59,8 +59,28 @@ public: * @param var is the variant that should be serialized. * @param pretty if true, the resulting value is properly indented. */ - static std::string writeJsonToString(const Variant &var, bool pretty = true); + static std::string writeJsonToString(const Variant &var, + bool pretty = true); + /** + * Dumps the Variant as re-readable ousia data. Note that the resulting + * data is invalid if the Variant consists of function or object references. + * + * @param var is the variant that should be serialized. + * @param stream is the stream the result should be written to. + * @param pretty if true, the resulting value is properly indented. + */ + static void writeOusia(const Variant &var, std::ostream &stream, + bool pretty = true); + + /** + * Dumps the Variant as re-readable ousia data to a string. + * + * @param var is the variant that should be serialized. + * @param pretty if true, the resulting value is properly indented. + */ + static std::string writeOusiaToString(const Variant &var, + bool pretty = true); }; } -- cgit v1.2.3