diff options
Diffstat (limited to 'src/core/common')
-rw-r--r-- | src/core/common/VariantConverter.cpp | 4 | ||||
-rw-r--r-- | src/core/common/VariantWriter.cpp | 31 | ||||
-rw-r--r-- | src/core/common/VariantWriter.hpp | 22 |
3 files changed, 45 insertions, 12 deletions
diff --git a/src/core/common/VariantConverter.cpp b/src/core/common/VariantConverter.cpp index a9ca467..b43d04e 100644 --- a/src/core/common/VariantConverter.cpp +++ b/src/core/common/VariantConverter.cpp @@ -262,7 +262,7 @@ bool VariantConverter::toString(Variant &var, Logger &logger, Mode mode) // Print cardinality syntax Variant::cardinalityType card = var.asCardinality(); std::stringstream ss; - ss << "<cardinality {"; + ss << "{"; bool first = true; for (Variant::rangeType r : card.getRanges()) { if (first) { @@ -288,7 +288,7 @@ bool VariantConverter::toString(Variant &var, Logger &logger, Mode mode) ss << ">" << std::to_string(r.start - 1); } } - ss << "}>"; + ss << "}"; var = ss.str().c_str(); return true; } 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 <char ObjectStart, char ObjectEnd, char Equals> +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<ObjectStart, ObjectEnd, Equals>( + 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<ObjectStart, ObjectEnd, Equals>( + 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); }; } |