From 6e39b49cc2588ace8818a0a0f4508d6ada4bd398 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 30 Oct 2014 07:25:13 -0700 Subject: [PATCH] Add Json::stream to write Value to a Streambuf --- src/ripple/json/impl/json_writer.cpp | 82 ++++++++++++++++++++++++++++ src/ripple/json/json_value.h | 8 +++ 2 files changed, 90 insertions(+) diff --git a/src/ripple/json/impl/json_writer.cpp b/src/ripple/json/impl/json_writer.cpp index 79834e03f..50d7009fb 100644 --- a/src/ripple/json/impl/json_writer.cpp +++ b/src/ripple/json/impl/json_writer.cpp @@ -908,5 +908,87 @@ std::ostream& operator<< ( std::ostream& sout, const Value& root ) return sout; } +//------------------------------------------------------------------------------ + +namespace detail { + +inline +void +write_string (write_t write, std::string const& s) +{ + write(s.data(), s.size()); +} + +void +write_value (write_t write, Value const& value) +{ + switch (value.type()) + { + case nullValue: + write("null", 4); + break; + + case intValue: + write_string(write, valueToString(value.asInt())); + break; + + case uintValue: + write_string(write, valueToString(value.asUInt())); + break; + + case realValue: + write_string(write, valueToString(value.asDouble())); + break; + + case stringValue: + write_string(write, valueToQuotedString(value.asCString())); + break; + + case booleanValue: + write_string(write, valueToString(value.asBool())); + break; + + case arrayValue: + { + write("[", 1); + int const size = value.size(); + for (int index = 0; index < size; ++index) + { + if (index > 0) + write(",", 1); + write_value(write, value[index]); + } + write("]", 1); + break; + } + + case objectValue: + { + Value::Members const members = value.getMemberNames(); + write("{", 1); + for (auto it = members.begin(); it != members.end(); ++it) + { + std::string const& name = *it; + if (it != members.begin()) + write(",", 1); + + write_string(write, valueToQuotedString(name.c_str())); + write(":", 1); + write_value(write, value[name]); + } + write("}", 1); + break; + } + } +} + +} // detail + +void +stream (Json::Value const& jv, write_t write) +{ + detail::write_value(write, jv); + write("\n", 1); +} } // namespace Json diff --git a/src/ripple/json/json_value.h b/src/ripple/json/json_value.h index 5912d80dc..877746782 100644 --- a/src/ripple/json/json_value.h +++ b/src/ripple/json/json_value.h @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -1082,6 +1083,13 @@ public: } }; +//------------------------------------------------------------------------------ + +using write_t = std::function; + +/** Stream compact JSON to the specified function. */ +void +stream (Json::Value const& jv, write_t write); } // namespace Json