Cleanups to Json Object code.

* Replace Json::JsonException with std::logic_error.
* Move two functions definitions to Object.cpp.
* Fix include guards.
This commit is contained in:
Tom Ritchford
2015-01-29 13:07:19 -05:00
parent e9b7003cf5
commit 0825bd7076
11 changed files with 117 additions and 102 deletions

View File

@@ -62,9 +62,9 @@ Collection::Collection (Collection&& that) noexcept
void Collection::checkWritable (std::string const& label)
{
if (!enabled_)
throw JsonException (label + ": not enabled");
throw std::logic_error (label + ": not enabled");
if (!writer_)
throw JsonException (label + ": not writable");
throw std::logic_error (label + ": not writable");
}
//------------------------------------------------------------------------------
@@ -125,6 +125,70 @@ Object::Proxy Object::operator[] (Json::StaticString const& key)
return Proxy (*this, std::string (key));
}
//------------------------------------------------------------------------------
void Array::append (Json::Value const& v)
{
auto t = v.type();
switch (t)
{
case Json::nullValue: return append (nullptr);
case Json::intValue: return append (v.asInt());
case Json::uintValue: return append (v.asUInt());
case Json::realValue: return append (v.asDouble());
case Json::stringValue: return append (v.asString());
case Json::booleanValue: return append (v.asBool());
case Json::objectValue:
{
auto object = appendObject ();
copyFrom (object, v);
return;
}
case Json::arrayValue:
{
auto array = appendArray ();
for (auto& item: v)
array.append (item);
return;
}
}
assert (false); // Can't get here.
}
void Object::set (std::string const& k, Json::Value const& v)
{
auto t = v.type();
switch (t)
{
case Json::nullValue: return set (k, nullptr);
case Json::intValue: return set (k, v.asInt());
case Json::uintValue: return set (k, v.asUInt());
case Json::realValue: return set (k, v.asDouble());
case Json::stringValue: return set (k, v.asString());
case Json::booleanValue: return set (k, v.asBool());
case Json::objectValue:
{
auto object = setObject (k);
copyFrom (object, v);
return;
}
case Json::arrayValue:
{
auto array = setArray (k);
for (auto& item: v)
array.append (item);
return;
}
}
assert (false); // Can't get here.
}
//------------------------------------------------------------------------------
namespace {
template <class Object>

View File

@@ -21,6 +21,7 @@
#include <ripple/json/Output.h>
#include <ripple/json/Writer.h>
#include <beast/unit_test/suite.h>
#include <stack>
namespace Json {
@@ -146,7 +147,7 @@ public:
void writeObjectTag (std::string const& tag)
{
#ifdef DEBUG
#ifndef NDEBUG
// Make sure we haven't already seen this tag.
auto& tags = stack_.top ().tags;
check (tags.find (tag) == tags.end (), "Already seen tag " + tag);
@@ -194,7 +195,7 @@ private:
* If false, we have to emit a , before we write the next entry. */
bool isFirst = true;
#ifdef DEBUG
#ifndef NDEBUG
/** What tags have we already seen in this collection? */
std::set <std::string> tags;
#endif
@@ -246,26 +247,28 @@ void Writer::output (Json::Value const& value)
outputJson (value, impl_->getOutput());
}
template <>
void Writer::output (float f)
{
auto s = ripple::to_string (f);
impl_->output ({s.data (), lengthWithoutTrailingZeros (s)});
}
template <>
void Writer::output (double f)
{
auto s = ripple::to_string (f);
impl_->output ({s.data (), lengthWithoutTrailingZeros (s)});
}
template <>
void Writer::output (std::nullptr_t)
{
impl_->output ("null");
}
void Writer::output (bool b)
{
impl_->output (b ? "true" : "false");
}
void Writer::implOutput (std::string const& s)
{
impl_->output (s);