Get ready for Json decode.

This commit is contained in:
JoelKatz
2012-10-01 02:19:17 -07:00
parent d37db0219d
commit 98d8823be0
6 changed files with 22 additions and 7 deletions

View File

@@ -75,7 +75,7 @@ SField::ref SField::getField(int type, int value)
std::string SField::getName() const
{
if (fieldName != NULL)
if (!fieldName.empty())
return fieldName;
if (fieldValue == 0)
return "";

View File

@@ -48,20 +48,20 @@ public:
const int fieldCode; // (type<<16)|index
const SerializedTypeID fieldType; // STI_*
const int fieldValue; // Code number for protocol
const char* fieldName;
std::string fieldName;
SField(int fc, SerializedTypeID tid, int fv, const char* fn) :
fieldCode(fc), fieldType(tid), fieldValue(fv), fieldName(fn)
{ codeToField[fc] = this; }
SField(int fc) : fieldCode(fc), fieldType(STI_UNKNOWN), fieldValue(0), fieldName(NULL) { ; }
SField(int fc) : fieldCode(fc), fieldType(STI_UNKNOWN), fieldValue(0) { ; }
static SField::ref getField(int fieldCode);
static SField::ref getField(int fieldType, int fieldValue);
static SField::ref getField(SerializedTypeID type, int value) { return getField(FIELD_CODE(type, value)); }
std::string getName() const;
bool hasName() const { return (fieldName != NULL) || (fieldValue != 0); }
bool hasName() const { return !fieldName.empty(); }
bool isGeneric() const { return fieldCode == 0; }
bool isInvalid() const { return fieldCode == -1; }

View File

@@ -703,7 +703,7 @@ Json::Value STObject::getJson(int options) const
{
if (it.getSType() != STI_NOTPRESENT)
{
if (it.getName() == NULL)
if (!it.getFName().hasName())
ret[boost::lexical_cast<std::string>(index)] = it.getJson(options);
else
ret[it.getName()] = it.getJson(options);
@@ -777,6 +777,19 @@ STArray* STArray::construct(SerializerIterator& sit, SField::ref field)
return new STArray(field, value);
}
STObject::STObject(const Json::Value& object, SField::ref name) : SerializedType(name)
{
if (!object.isObject())
throw std::runtime_error("Value is not an object");
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];
// WRITEME
}
}
#if 0

View File

@@ -40,6 +40,8 @@ public:
STObject(SOElement::ptrList type, SerializerIterator& sit, SField::ref name) : SerializedType(name)
{ set(sit); setType(type); }
STObject(const Json::Value& value, SField::ref name);
virtual ~STObject() { ; }
static std::auto_ptr<SerializedType> deserialize(SerializerIterator& sit, SField::ref name);

View File

@@ -19,7 +19,7 @@ std::string SerializedType::getFullText() const
std::string ret;
if (getSType() != STI_NOTPRESENT)
{
if(fName != NULL)
if(fName->hasName())
{
ret = fName->fieldName;
ret += " = ";

View File

@@ -50,7 +50,7 @@ public:
void setFName(SField::ref n) { fName = &n; assert(fName); }
SField::ref getFName() const { return *fName; }
const char *getName() const { return fName->fieldName; }
std::string getName() const { return fName->fieldName; }
virtual SerializedTypeID getSType() const { return STI_NOTPRESENT; }
std::auto_ptr<SerializedType> clone() const { return std::auto_ptr<SerializedType>(duplicate()); }