From 98d8823be0ab59a68659b4ffcb294b262608480f Mon Sep 17 00:00:00 2001 From: JoelKatz Date: Mon, 1 Oct 2012 02:19:17 -0700 Subject: [PATCH] Get ready for Json decode. --- src/FieldNames.cpp | 2 +- src/FieldNames.h | 6 +++--- src/SerializedObject.cpp | 15 ++++++++++++++- src/SerializedObject.h | 2 ++ src/SerializedTypes.cpp | 2 +- src/SerializedTypes.h | 2 +- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/FieldNames.cpp b/src/FieldNames.cpp index d631fb7f01..e6af94ad26 100644 --- a/src/FieldNames.cpp +++ b/src/FieldNames.cpp @@ -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 ""; diff --git a/src/FieldNames.h b/src/FieldNames.h index c738e6cefa..4d966c547c 100644 --- a/src/FieldNames.h +++ b/src/FieldNames.h @@ -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; } diff --git a/src/SerializedObject.cpp b/src/SerializedObject.cpp index a2980efa21..af7ac0c47b 100644 --- a/src/SerializedObject.cpp +++ b/src/SerializedObject.cpp @@ -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(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 diff --git a/src/SerializedObject.h b/src/SerializedObject.h index 98ffc8407e..bfcc7c090c 100644 --- a/src/SerializedObject.h +++ b/src/SerializedObject.h @@ -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 deserialize(SerializerIterator& sit, SField::ref name); diff --git a/src/SerializedTypes.cpp b/src/SerializedTypes.cpp index 570a2e80ce..4cee5f5c9b 100644 --- a/src/SerializedTypes.cpp +++ b/src/SerializedTypes.cpp @@ -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 += " = "; diff --git a/src/SerializedTypes.h b/src/SerializedTypes.h index 9852735e35..33706ae456 100644 --- a/src/SerializedTypes.h +++ b/src/SerializedTypes.h @@ -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 clone() const { return std::auto_ptr(duplicate()); }