mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Better interoperation between Json::Value and JsonObject.
* Generic functions to add entries to both object models. * Add Json::Value into JsonObjects. * Write Json::Value to string incrementally. * Get rid of ripple::RPC::New namespace
This commit is contained in:
committed by
Vinnie Falco
parent
7cfac1a91a
commit
8053598069
@@ -18,11 +18,11 @@
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/rpc/impl/JsonWriter.h>
|
||||
#include <ripple/rpc/impl/WriteJson.h>
|
||||
#include <beast/unit_test/suite.h>
|
||||
|
||||
namespace ripple {
|
||||
namespace RPC {
|
||||
namespace New {
|
||||
|
||||
namespace {
|
||||
|
||||
@@ -37,7 +37,7 @@ std::map <char, const char*> jsonSpecialCharacterEscape = {
|
||||
{'\t', "\\t"}
|
||||
};
|
||||
|
||||
static int const jsonEscapeLength = 2;
|
||||
static size_t const jsonEscapeLength = 2;
|
||||
|
||||
// All other JSON punctuation.
|
||||
const char closeBrace = '}';
|
||||
@@ -50,12 +50,24 @@ const char quote = '"';
|
||||
|
||||
const std::string none;
|
||||
|
||||
static auto const integralFloatsBecomeInts = false;
|
||||
|
||||
size_t lengthWithoutTrailingZeros (std::string const& s)
|
||||
{
|
||||
if (s.find ('.') == std::string::npos)
|
||||
auto dotPos = s.find ('.');
|
||||
if (dotPos == std::string::npos)
|
||||
return s.size();
|
||||
|
||||
return s.find_last_not_of ('0') + 1;
|
||||
auto lastNonZero = s.find_last_not_of ('0');
|
||||
auto hasDecimals = dotPos != lastNonZero;
|
||||
|
||||
if (hasDecimals)
|
||||
return lastNonZero + 1;
|
||||
|
||||
if (integralFloatsBecomeInts || lastNonZero + 2 > s.size())
|
||||
return lastNonZero;
|
||||
|
||||
return lastNonZero + 2;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -63,7 +75,8 @@ size_t lengthWithoutTrailingZeros (std::string const& s)
|
||||
class Writer::Impl
|
||||
{
|
||||
public:
|
||||
Impl (Output output) : output_(output) {}
|
||||
Impl (Output const& output) : output_(output) {}
|
||||
~Impl() = default;
|
||||
|
||||
Impl(Impl&&) = delete;
|
||||
Impl& operator=(Impl&&) = delete;
|
||||
@@ -168,6 +181,8 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
Output const& getOutput() const { return output_; }
|
||||
|
||||
private:
|
||||
// JSON collections are either arrrays, or objects.
|
||||
struct Collection
|
||||
@@ -193,13 +208,15 @@ private:
|
||||
bool isStarted_ = false;
|
||||
};
|
||||
|
||||
Writer::Writer (Output output) : impl_(std::make_unique <Impl> (output))
|
||||
Writer::Writer (Output const &output)
|
||||
: impl_(std::make_unique <Impl> (output))
|
||||
{
|
||||
}
|
||||
|
||||
Writer::~Writer()
|
||||
{
|
||||
impl_->finishAll ();
|
||||
if (impl_)
|
||||
impl_->finishAll ();
|
||||
}
|
||||
|
||||
Writer::Writer(Writer&& w)
|
||||
@@ -223,6 +240,12 @@ void Writer::output (std::string const& s)
|
||||
impl_->stringOutput (s);
|
||||
}
|
||||
|
||||
void Writer::output (Json::Value const& value)
|
||||
{
|
||||
impl_->markStarted();
|
||||
writeJson (value, impl_->getOutput());
|
||||
}
|
||||
|
||||
template <>
|
||||
void Writer::output (float f)
|
||||
{
|
||||
@@ -243,37 +266,32 @@ void Writer::output (std::nullptr_t)
|
||||
impl_->output ("null");
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void Writer::output (Type t)
|
||||
void Writer::implOutput (std::string const& s)
|
||||
{
|
||||
impl_->output (to_string (t));
|
||||
impl_->output (s);
|
||||
}
|
||||
|
||||
void Writer::finishAll ()
|
||||
{
|
||||
impl_->finishAll ();
|
||||
if (impl_)
|
||||
impl_->finishAll ();
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void Writer::append (Type t)
|
||||
void Writer::rawAppend()
|
||||
{
|
||||
impl_->nextCollectionEntry (array, "append");
|
||||
output (t);
|
||||
}
|
||||
|
||||
template <typename Type>
|
||||
void Writer::set (std::string const& tag, Type t)
|
||||
void Writer::rawSet (std::string const& tag)
|
||||
{
|
||||
check (!tag.empty(), "Tag can't be empty");
|
||||
|
||||
impl_->nextCollectionEntry (object, "set");
|
||||
impl_->writeObjectTag (tag);
|
||||
output (t);
|
||||
}
|
||||
|
||||
void Writer::startRoot (CollectionType type)
|
||||
{
|
||||
check (impl_->empty(), "stack_ not empty() in start");
|
||||
impl_->start (type);
|
||||
}
|
||||
|
||||
@@ -292,9 +310,9 @@ void Writer::startSet (CollectionType type, std::string const& key)
|
||||
|
||||
void Writer::finish ()
|
||||
{
|
||||
impl_->finish ();
|
||||
if (impl_)
|
||||
impl_->finish ();
|
||||
}
|
||||
|
||||
} // New
|
||||
} // RPC
|
||||
} // ripple
|
||||
|
||||
Reference in New Issue
Block a user