diff --git a/src/LedgerFormats.cpp b/src/LedgerFormats.cpp index 9f0ed403a..56c8a39a2 100644 --- a/src/LedgerFormats.cpp +++ b/src/LedgerFormats.cpp @@ -89,13 +89,18 @@ LedgerEntryFormat* getLgrFormat(LedgerEntryType t) LedgerEntryFormat* getLgrFormat(int t) { - LedgerEntryFormat* f = LedgerFormats; - while (f->t_name != NULL) - { + for (LedgerEntryFormat* f = LedgerFormats; f->t_name != NULL; ++f) if (f->t_type == t) return f; - ++f; - } 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 diff --git a/src/LedgerFormats.h b/src/LedgerFormats.h index 7b4a95683..348116880 100644 --- a/src/LedgerFormats.h +++ b/src/LedgerFormats.h @@ -48,6 +48,7 @@ struct LedgerEntryFormat extern LedgerEntryFormat LedgerFormats[]; extern LedgerEntryFormat* getLgrFormat(LedgerEntryType t); +extern LedgerEntryFormat* getLgrFormat(const std::string& t); extern LedgerEntryFormat* getLgrFormat(int t); #endif // vim:ts=4 diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp index a217c0268..7584d53dc 100644 --- a/src/SerializedObject.cpp +++ b/src/SerializedObject.cpp @@ -8,6 +8,8 @@ #include "../json/writer.h" #include "Log.h" +#include "LedgerFormats.h" +#include "TransactionFormats.h" std::auto_ptr STObject::makeDefaultObject(SerializedTypeID id, SField::ref name) { @@ -813,21 +815,23 @@ STArray* STArray::construct(SerializerIterator& sit, SField::ref field) return new STArray(field, value); } -std::auto_ptr STObject::parseJson(const Json::Value& object, SField::ref name, int depth) +std::auto_ptr STObject::parseJson(const Json::Value& object, SField::ref inName, int depth) { if (!object.isObject()) throw std::runtime_error("Value is not an object"); + SField::ptr name = &inName; + boost::ptr_vector data; Json::Value::Members members(object.getMemberNames()); for (Json::Value::Members::iterator it = members.begin(), end = members.end(); it != end; ++it) { - const std::string& name = *it; - const Json::Value& value = object[name]; + const std::string& fieldName = *it; + const Json::Value& value = object[fieldName]; - SField::ref field = SField::getField(name); + SField::ref field = SField::getField(fieldName); if (field == sfInvalid) - throw std::runtime_error("Unknown field: " + name); + throw std::runtime_error("Unknown field: " + fieldName); switch (field.fieldType) { @@ -846,20 +850,31 @@ std::auto_ptr STObject::parseJson(const Json::Value& object, SField::r if (value.isString()) { 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) { - // WRITEME + TransactionFormat* f = getTxnFormat(strValue); + if (!f) + throw std::runtime_error("Unknown transaction type"); + data.push_back(new STUInt16(field, static_cast(f->t_type))); + if (*name == sfGeneric) + name = &sfTransaction; } 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(f->t_type))); + if (*name == sfGeneric) + name = &sfLedgerEntry; } else throw std::runtime_error("Invalid field data"); } - data.push_back(new STUInt16(field, boost::lexical_cast(strValue))); + else + data.push_back(new STUInt16(field, boost::lexical_cast(strValue))); } else if (value.isInt()) data.push_back(new STUInt16(field, boost::lexical_cast(value.asInt()))); @@ -893,13 +908,25 @@ std::auto_ptr STObject::parseJson(const Json::Value& object, SField::r 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: - // WRITEME + if (value.isString()) + data.push_back(new STHash160(field, value.asString())); + else + throw std::runtime_error("Incorrect type"); + break; 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: // WRITEME @@ -932,7 +959,7 @@ std::auto_ptr STObject::parseJson(const Json::Value& object, SField::r throw std::runtime_error("Invalid field type"); } } - return std::auto_ptr(new STObject(name, data)); + return std::auto_ptr(new STObject(*name, data)); } #if 0 diff --git a/src/SerializedObject.h b/src/SerializedObject.h index a60a3efba..5255c463e 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -41,7 +41,7 @@ public: STObject(SOElement::ptrList type, SerializerIterator& sit, SField::ref name) : SerializedType(name) { set(sit); setType(type); } - static std::auto_ptr parseJson(const Json::Value& value, SField::ref name, int depth = 0); + static std::auto_ptr parseJson(const Json::Value& value, SField::ref name = sfGeneric, int depth = 0); virtual ~STObject() { ; } diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 246e4cde1..046c39e52 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -377,6 +377,8 @@ public: STHash128(const uint128& v) : 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() { ; } static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name) @@ -405,6 +407,8 @@ public: STHash160(const uint160& v) : 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() { ; } static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name) @@ -433,6 +437,8 @@ public: STHash256(const uint256& v) : 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() { ; } static std::auto_ptr deserialize(SerializerIterator& sit, SField::ref name) diff --git a/src/TransactionFormats.cpp b/src/TransactionFormats.cpp index cebc37a49..12d614de2 100644 --- a/src/TransactionFormats.cpp +++ b/src/TransactionFormats.cpp @@ -10,7 +10,7 @@ { sfSigningPubKey, SOE_REQUIRED }, \ { sfTxnSignature, SOE_OPTIONAL }, -TransactionFormat InnerTxnFormats[]= +TransactionFormat TxnFormats[]= { { "AccountSet", ttACCOUNT_SET, { TF_BASE { sfEmailHash, SOE_OPTIONAL }, @@ -30,7 +30,7 @@ TransactionFormat InnerTxnFormats[]= }, { "CreditSet", ttCREDIT_SET, { TF_BASE { sfLimitAmount, SOE_OPTIONAL }, - { sfQualityIn, SOE_OPTIONAL }, + { sfQualityIn, SOE_OPTIONAL }, { sfQualityOut, SOE_OPTIONAL }, { sfInvalid, SOE_END } } }, @@ -107,13 +107,18 @@ TransactionFormat* getTxnFormat(TransactionType t) TransactionFormat* getTxnFormat(int t) { - TransactionFormat* f = InnerTxnFormats; - while (f->t_name != NULL) - { - if (f->t_type == t) + for (TransactionFormat* f = TxnFormats; f->t_name != NULL; ++f) + if (t == f->t_type) return f; - ++f; - } 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 diff --git a/src/TransactionFormats.h b/src/TransactionFormats.h index 57b155285..dd385bad5 100644 --- a/src/TransactionFormats.h +++ b/src/TransactionFormats.h @@ -44,8 +44,9 @@ const uint32 tfPartialPayment = 0x00020000; const uint32 tfLimitQuality = 0x00040000; const uint32 tfNoRippleDirect = 0x00080000; -extern TransactionFormat InnerTxnFormats[]; +extern TransactionFormat TxnFormats[]; extern TransactionFormat* getTxnFormat(TransactionType t); +extern TransactionFormat* getTxnFormat(const std::string& t); extern TransactionFormat* getTxnFormat(int t); #endif // vim:ts=4