Tie the new serialization code into the Json code.

Small cleanups.
This commit is contained in:
JoelKatz
2012-03-26 20:31:03 -07:00
parent ec7833d56e
commit bd9780e503
5 changed files with 56 additions and 10 deletions

View File

@@ -1,6 +1,8 @@
#include "SerializedObject.h"
#include "boost/lexical_cast.hpp"
SerializedType* STObject::makeDefaultObject(SerializedTypeID id, const char *name)
{
switch(id)
@@ -455,3 +457,19 @@ void STObject::setValueFieldTL(SOE_Field field, const std::vector<TaggedListItem
if(!cf) throw(std::runtime_error("Wrong field type"));
cf->setValue(v);
}
Json::Value STObject::getJson(int options) const
{
Json::Value ret(Json::objectValue);
int index=1;
for(boost::ptr_vector<SerializedType>::const_iterator it=mData.begin(), end=mData.end(); it!=end; ++it, ++index)
{
if(it->getType()!=STI_NOTPRESENT)
{
if(it->getName()==NULL)
ret[boost::lexical_cast<std::string>(index)]=it->getText();
else ret[it->getName()]=it->getText();
}
}
return ret;
}

View File

@@ -5,6 +5,8 @@
#include <boost/ptr_container/ptr_vector.hpp>
#include "../json/value.h"
#include "SerializedTypes.h"
enum SOE_Type
@@ -57,6 +59,7 @@ public:
void add(Serializer& s) const;
std::string getFullText() const;
std::string getText() const;
virtual Json::Value getJson(int options) const;
int addObject(const SerializedType& t) { mData.push_back(t.duplicate()); return mData.size()-1; }
int giveObject(SerializedType* t) { mData.push_back(t); return mData.size()-1; }

View File

@@ -99,6 +99,16 @@ std::vector<unsigned char> SerializedTransaction::getSignature() const
return mSignature.getValue();
}
bool SerializedTransaction::sign(CKey& key)
{
return key.Sign(getSigningHash(), mSignature.peekValue());
}
bool SerializedTransaction::checkSign(const CKey& key) const
{
return key.Verify(getSigningHash(), mSignature.getValue());
}
void SerializedTransaction::setSignature(const std::vector<unsigned char>& sig)
{
mSignature.setValue(sig);
@@ -180,4 +190,14 @@ void SerializedTransaction::makeITFieldAbsent(SOE_Field field)
{
return mInnerTxn.makeFieldAbsent(field);
}
Json::Value SerializedTransaction::getJson(int options) const
{
Json::Value ret(Json::objectValue);
ret["Type"]=mFormat->t_name;
ret["ID"]=getTransactionID().GetHex();
ret["Signature"]=mSignature.getText();
ret["Middle"]=mMiddleTxn.getJson(options);
ret["Inner"]=mInnerTxn.getJson(options);
return ret;
}

View File

@@ -89,6 +89,11 @@ public:
// whole transaction functions
int getTransaction(Serializer& s, bool include_length) const;
uint256 getTransactionID() const;
virtual Json::Value getJson(int options) const;
bool sign(CKey& key);
bool checkSign(const CKey& key) const;
};
#endif

View File

@@ -32,7 +32,7 @@ public:
virtual ~SerializedType() { ; }
void setName(const char *n) { name=n; }
const char *getName() { return name; }
const char *getName() const { return name; }
virtual int getLength() const { return 0; }
virtual SerializedTypeID getType() const { return STI_NOTPRESENT; }
@@ -63,7 +63,7 @@ public:
SerializedTypeID getType() const { return STI_UINT8; }
STUInt8 *duplicate() const { return new STUInt8(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { s.add8(value); }
void add(Serializer& s) const { s.add8(value); }
unsigned char getValue() const { return value; }
void setValue(unsigned char v) { value=v; }
@@ -87,7 +87,7 @@ public:
SerializedTypeID getType() const { return STI_UINT16; }
STUInt16 *duplicate() const { return new STUInt16(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { s.add16(value); }
void add(Serializer& s) const { s.add16(value); }
uint16 getValue() const { return value; }
void setValue(uint16 v) { value=v; }
@@ -111,7 +111,7 @@ public:
SerializedTypeID getType() const { return STI_UINT32; }
STUInt32 *duplicate() const { return new STUInt32(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { s.add32(value); }
void add(Serializer& s) const { s.add32(value); }
uint32 getValue() const { return value; }
void setValue(uint32 v) { value=v; }
@@ -135,7 +135,7 @@ public:
SerializedTypeID getType() const { return STI_UINT64; }
STUInt64 *duplicate() const { return new STUInt64(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { s.add64(value); }
void add(Serializer& s) const { s.add64(value); }
uint64 getValue() const { return value; }
void setValue(uint64 v) { value=v; }
@@ -160,7 +160,7 @@ public:
SerializedTypeID getType() const { return STI_HASH160; }
STHash160 *duplicate() const { return new STHash160(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { s.add160(value); }
void add(Serializer& s) const { s.add160(value); }
const uint160& getValue() const { return value; }
void setValue(const uint160& v) { value=v; }
@@ -185,7 +185,7 @@ public:
SerializedTypeID getType() const { return STI_HASH256; }
STHash256 *duplicate() const { return new STHash256(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { s.add256(value); }
void add(Serializer& s) const { s.add256(value); }
const uint256& getValue() const { return value; }
void setValue(const uint256& v) { value=v; }
@@ -211,7 +211,7 @@ public:
SerializedTypeID getType() const { return STI_VL; }
STVariableLength *duplicate() const { return new STVariableLength(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { s.addVL(value); }
void add(Serializer& s) const { s.addVL(value); }
const std::vector<unsigned char>& peekValue() const { return value; }
std::vector<unsigned char>& peekValue() { return value; }
@@ -239,7 +239,7 @@ public:
SerializedTypeID getType() const { return STI_TL; }
STTaggedList *duplicate() const { return new STTaggedList(name, value); }
std::string getText() const;
virtual void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); }
void add(Serializer& s) const { if(s.addTaggedList(value)<0) throw(0); }
const std::vector<TaggedListItem>& peekValue() const { return value; }
std::vector<TaggedListItem>& peekValue() { return value; }