New JSON functionality.

This commit is contained in:
JoelKatz
2012-10-01 16:53:54 -07:00
parent e04b695434
commit e7c9ee09f6
7 changed files with 73 additions and 28 deletions

View File

@@ -89,13 +89,18 @@ LedgerEntryFormat* getLgrFormat(LedgerEntryType t)
LedgerEntryFormat* getLgrFormat(int t) LedgerEntryFormat* getLgrFormat(int t)
{ {
LedgerEntryFormat* f = LedgerFormats; for (LedgerEntryFormat* f = LedgerFormats; f->t_name != NULL; ++f)
while (f->t_name != NULL)
{
if (f->t_type == t) if (f->t_type == t)
return f; return f;
++f;
}
return NULL; return NULL;
} }
LedgerEntryFormat* getLgrFormat(const std::string& t)
{
for (LedgerEntryFormat* f = LedgerFormats; f->t_name != NULL; ++f)
if (t == f->t_name)
return f;
return NULL;
}
// vim:ts=4 // vim:ts=4

View File

@@ -48,6 +48,7 @@ struct LedgerEntryFormat
extern LedgerEntryFormat LedgerFormats[]; extern LedgerEntryFormat LedgerFormats[];
extern LedgerEntryFormat* getLgrFormat(LedgerEntryType t); extern LedgerEntryFormat* getLgrFormat(LedgerEntryType t);
extern LedgerEntryFormat* getLgrFormat(const std::string& t);
extern LedgerEntryFormat* getLgrFormat(int t); extern LedgerEntryFormat* getLgrFormat(int t);
#endif #endif
// vim:ts=4 // vim:ts=4

View File

@@ -8,6 +8,8 @@
#include "../json/writer.h" #include "../json/writer.h"
#include "Log.h" #include "Log.h"
#include "LedgerFormats.h"
#include "TransactionFormats.h"
std::auto_ptr<SerializedType> STObject::makeDefaultObject(SerializedTypeID id, SField::ref name) std::auto_ptr<SerializedType> STObject::makeDefaultObject(SerializedTypeID id, SField::ref name)
{ {
@@ -813,21 +815,23 @@ STArray* STArray::construct(SerializerIterator& sit, SField::ref field)
return new STArray(field, value); return new STArray(field, value);
} }
std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::ref name, int depth) std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::ref inName, int depth)
{ {
if (!object.isObject()) if (!object.isObject())
throw std::runtime_error("Value is not an object"); throw std::runtime_error("Value is not an object");
SField::ptr name = &inName;
boost::ptr_vector<SerializedType> data; boost::ptr_vector<SerializedType> data;
Json::Value::Members members(object.getMemberNames()); Json::Value::Members members(object.getMemberNames());
for (Json::Value::Members::iterator it = members.begin(), end = members.end(); it != end; ++it) for (Json::Value::Members::iterator it = members.begin(), end = members.end(); it != end; ++it)
{ {
const std::string& name = *it; const std::string& fieldName = *it;
const Json::Value& value = object[name]; const Json::Value& value = object[fieldName];
SField::ref field = SField::getField(name); SField::ref field = SField::getField(fieldName);
if (field == sfInvalid) if (field == sfInvalid)
throw std::runtime_error("Unknown field: " + name); throw std::runtime_error("Unknown field: " + fieldName);
switch (field.fieldType) switch (field.fieldType)
{ {
@@ -846,19 +850,30 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
if (value.isString()) if (value.isString())
{ {
std::string strValue = value.asString(); std::string strValue = value.asString();
if (!strValue.empty() && (strValue[0]<'0' || strValue[0]>'9')) if (!strValue.empty() && ((strValue[0] < '0') || (strValue[0] > '9')))
{ {
if (field == sfTransactionType) if (field == sfTransactionType)
{ {
// WRITEME TransactionFormat* f = getTxnFormat(strValue);
if (!f)
throw std::runtime_error("Unknown transaction type");
data.push_back(new STUInt16(field, static_cast<uint16>(f->t_type)));
if (*name == sfGeneric)
name = &sfTransaction;
} }
else if (field == sfLedgerEntryType) else if (field == sfLedgerEntryType)
{ {
// WRITEME LedgerEntryFormat* f = getLgrFormat(strValue);
if (!f)
throw std::runtime_error("Unknown ledger entry type");
data.push_back(new STUInt16(field, static_cast<uint16>(f->t_type)));
if (*name == sfGeneric)
name = &sfLedgerEntry;
} }
else else
throw std::runtime_error("Invalid field data"); throw std::runtime_error("Invalid field data");
} }
else
data.push_back(new STUInt16(field, boost::lexical_cast<uint16>(strValue))); data.push_back(new STUInt16(field, boost::lexical_cast<uint16>(strValue)));
} }
else if (value.isInt()) else if (value.isInt())
@@ -893,13 +908,25 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
case STI_HASH128: case STI_HASH128:
// WRITEME if (value.isString())
data.push_back(new STHash128(field, value.asString()));
else
throw std::runtime_error("Incorrect type");
break;
case STI_HASH160: case STI_HASH160:
// WRITEME if (value.isString())
data.push_back(new STHash160(field, value.asString()));
else
throw std::runtime_error("Incorrect type");
break;
case STI_HASH256: case STI_HASH256:
// WRITEME if (value.isString())
data.push_back(new STHash256(field, value.asString()));
else
throw std::runtime_error("Incorrect type");
break;
case STI_VL: case STI_VL:
// WRITEME // WRITEME
@@ -932,7 +959,7 @@ std::auto_ptr<STObject> STObject::parseJson(const Json::Value& object, SField::r
throw std::runtime_error("Invalid field type"); throw std::runtime_error("Invalid field type");
} }
} }
return std::auto_ptr<STObject>(new STObject(name, data)); return std::auto_ptr<STObject>(new STObject(*name, data));
} }
#if 0 #if 0

View File

@@ -41,7 +41,7 @@ public:
STObject(SOElement::ptrList type, SerializerIterator& sit, SField::ref name) : SerializedType(name) STObject(SOElement::ptrList type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
{ set(sit); setType(type); } { set(sit); setType(type); }
static std::auto_ptr<STObject> parseJson(const Json::Value& value, SField::ref name, int depth = 0); static std::auto_ptr<STObject> parseJson(const Json::Value& value, SField::ref name = sfGeneric, int depth = 0);
virtual ~STObject() { ; } virtual ~STObject() { ; }

View File

@@ -377,6 +377,8 @@ public:
STHash128(const uint128& v) : value(v) { ; } STHash128(const uint128& v) : value(v) { ; }
STHash128(SField::ref n, const uint128& v) : SerializedType(n), value(v) { ; } STHash128(SField::ref n, const uint128& v) : SerializedType(n), value(v) { ; }
STHash128(SField::ref n, const char *v) : SerializedType(n) { value.SetHex(v); }
STHash128(SField::ref n, const std::string &v) : SerializedType(n) { value.SetHex(v); }
STHash128(SField::ref n) : SerializedType(n) { ; } STHash128(SField::ref n) : SerializedType(n) { ; }
STHash128() { ; } STHash128() { ; }
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name) static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name)
@@ -405,6 +407,8 @@ public:
STHash160(const uint160& v) : value(v) { ; } STHash160(const uint160& v) : value(v) { ; }
STHash160(SField::ref n, const uint160& v) : SerializedType(n), value(v) { ; } STHash160(SField::ref n, const uint160& v) : SerializedType(n), value(v) { ; }
STHash160(SField::ref n, const char *v) : SerializedType(n) { value.SetHex(v); }
STHash160(SField::ref n, const std::string &v) : SerializedType(n) { value.SetHex(v); }
STHash160(SField::ref n) : SerializedType(n) { ; } STHash160(SField::ref n) : SerializedType(n) { ; }
STHash160() { ; } STHash160() { ; }
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name) static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name)
@@ -433,6 +437,8 @@ public:
STHash256(const uint256& v) : value(v) { ; } STHash256(const uint256& v) : value(v) { ; }
STHash256(SField::ref n, const uint256& v) : SerializedType(n), value(v) { ; } STHash256(SField::ref n, const uint256& v) : SerializedType(n), value(v) { ; }
STHash256(SField::ref n, const char *v) : SerializedType(n) { value.SetHex(v); }
STHash256(SField::ref n, const std::string &v) : SerializedType(n) { value.SetHex(v); }
STHash256(SField::ref n) : SerializedType(n) { ; } STHash256(SField::ref n) : SerializedType(n) { ; }
STHash256() { ; } STHash256() { ; }
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name) static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name)

View File

@@ -10,7 +10,7 @@
{ sfSigningPubKey, SOE_REQUIRED }, \ { sfSigningPubKey, SOE_REQUIRED }, \
{ sfTxnSignature, SOE_OPTIONAL }, { sfTxnSignature, SOE_OPTIONAL },
TransactionFormat InnerTxnFormats[]= TransactionFormat TxnFormats[]=
{ {
{ "AccountSet", ttACCOUNT_SET, { TF_BASE { "AccountSet", ttACCOUNT_SET, { TF_BASE
{ sfEmailHash, SOE_OPTIONAL }, { sfEmailHash, SOE_OPTIONAL },
@@ -107,13 +107,18 @@ TransactionFormat* getTxnFormat(TransactionType t)
TransactionFormat* getTxnFormat(int t) TransactionFormat* getTxnFormat(int t)
{ {
TransactionFormat* f = InnerTxnFormats; for (TransactionFormat* f = TxnFormats; f->t_name != NULL; ++f)
while (f->t_name != NULL) if (t == f->t_type)
{
if (f->t_type == t)
return f; return f;
++f;
}
return NULL; return NULL;
} }
TransactionFormat* getTxnFormat(const std::string& format)
{
for (TransactionFormat* f = TxnFormats; f->t_name != NULL; ++f)
if (format == f->t_name)
return f;
return NULL;
}
// vim:ts=4 // vim:ts=4

View File

@@ -44,8 +44,9 @@ const uint32 tfPartialPayment = 0x00020000;
const uint32 tfLimitQuality = 0x00040000; const uint32 tfLimitQuality = 0x00040000;
const uint32 tfNoRippleDirect = 0x00080000; const uint32 tfNoRippleDirect = 0x00080000;
extern TransactionFormat InnerTxnFormats[]; extern TransactionFormat TxnFormats[];
extern TransactionFormat* getTxnFormat(TransactionType t); extern TransactionFormat* getTxnFormat(TransactionType t);
extern TransactionFormat* getTxnFormat(const std::string& t);
extern TransactionFormat* getTxnFormat(int t); extern TransactionFormat* getTxnFormat(int t);
#endif #endif
// vim:ts=4 // vim:ts=4