diff --git a/src/Amount.cpp b/src/Amount.cpp index d4898101d0..9ceaa11975 100644 --- a/src/Amount.cpp +++ b/src/Amount.cpp @@ -3,6 +3,8 @@ #include #include +#include +#include #include #include "Config.h" @@ -54,6 +56,77 @@ std::string STAmount::getHumanCurrency() const return createHumanCurrency(mCurrency); } +STAmount::STAmount(SField::ref n, const Json::Value& v) + : SerializedType(n), mValue(0), mOffset(0), mIsNative(false), mIsNegative(false) +{ + Json::Value value, currency, issuer; + + if (v.isArray()) + { + value = v["value"]; + currency = v["currency"]; + issuer = v["issuer"]; + } + else if (v.isString()) + { + std::string val = v.asString(); + std::vector elements; + boost::split(elements, val, boost::is_any_of("\t\n\r ,/")); + + if ((elements.size() < 0) || (elements.size() > 3)) + throw std::runtime_error("invalid amount string"); + + value = elements[0]; + if (elements.size() > 0) + currency = elements[1]; + if (elements.size() > 1) + issuer = elements[2]; + } + else + value = v; + + if (value.isInt()) + { + if (value.asInt() >= 0) + mValue = value.asInt(); + else + { + mValue = -value.asInt(); + mIsNegative = true; + } + } + else if (value.isUInt()) + mValue = v.asUInt(); + else if (value.isDouble()) + { + // WRITEME + } + else if (value.isString()) + { // FIXME: If it has a '.' we have to process it specially! + int64 val = lexical_cast_st(value.asString()); + if (val >= 0) + mValue = val; + else + { + mValue = -val; + mIsNegative = true; + } + } + else + throw std::runtime_error("invalid amount type"); + + if (!currency.isString() || currency.asString().empty() || (currency.asString() == SYSTEM_CURRENCY_CODE)) + { + mIsNative = true; + return; + } + + // parse currency and issuer + // WRITEME + + canonicalize(); +} + std::string STAmount::createHumanCurrency(const uint160& uCurrency) { std::string sCurrency; diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp index 5dc6b29b6c..74137bcc37 100644 --- a/src/SerializedObject.cpp +++ b/src/SerializedObject.cpp @@ -938,12 +938,12 @@ std::auto_ptr STObject::parseJson(const Json::Value& object, SField::r case STI_VL: if (!value.isString()) - throw std::runtime_error("Incorrect type"); data.push_back(new STVariableLength(field, strUnHex(value.asString()))); break; case STI_AMOUNT: - // WRITEME + data.push_back(new STAmount(field, value)); + break; case STI_VECTOR256: // WRITEME @@ -951,6 +951,7 @@ std::auto_ptr STObject::parseJson(const Json::Value& object, SField::r case STI_PATHSET: // WRITEME break; + case STI_ACCOUNT: { if (!value.isString())