Add Json::stream to write Value to a Streambuf

This commit is contained in:
Vinnie Falco
2014-10-30 07:25:13 -07:00
parent 71c34ed4e0
commit 6e39b49cc2
2 changed files with 90 additions and 0 deletions

View File

@@ -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

View File

@@ -23,6 +23,7 @@
#include <ripple/json/json_config.h>
#include <ripple/json/json_forwards.h>
#include <beast/strings/String.h>
#include <functional>
#include <map>
#include <vector>
@@ -1082,6 +1083,13 @@ public:
}
};
//------------------------------------------------------------------------------
using write_t = std::function<void(void const*, std::size_t)>;
/** Stream compact JSON to the specified function. */
void
stream (Json::Value const& jv, write_t write);
} // namespace Json