diff --git a/src/ofxJSONElement.cpp b/src/ofxJSONElement.cpp index 244f626..7d02804 100755 --- a/src/ofxJSONElement.cpp +++ b/src/ofxJSONElement.cpp @@ -153,3 +153,154 @@ std::string ofxJSONElement::toString(Json::ValueType type) return "unknown"; } } + + + +/* ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||| Encode Series ||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ + +/* =============================================================== * + * const ofxJSONElement Encode(const ofPoint &data); * + * =============================================================== */ +const ofxJSONElement ofxJSONElement :: Encode(const ofPoint& data){ + ofxJSONElement json; + + json["x"] = data.x; + json["y"] = data.y; + json["z"] = data.z; + + return json; +} + +/* =============================================================== * + * const ofxJSONElement Encode(const ofVec2f &data); * + * =============================================================== */ +const ofxJSONElement ofxJSONElement :: Encode(const ofVec2f& data){ + ofxJSONElement json; + + json["x"] = data.x; + json["y"] = data.y; + + return json; +} + +/* =============================================================== * + * const ofxJSONElement Encode(const ofRectangle &data); * + * =============================================================== */ +const ofxJSONElement ofxJSONElement :: Encode(const ofRectangle& data){ + ofxJSONElement json; + + json["x"] = data.x; + json["y"] = data.y; + json["width"] = data.width; + json["height"] = data.height; + + return json; +} + +/* ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||| Decode Series ||||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ + +/* ================================================================= * + * const T Decode(const ofxJSONElement& json); * + * ================================================================= */ +template +const T ofxJSONElement :: Decode(const ofxJSONElement& json){ + + T data; + + data = static_cast( + json.isNull () ? 0 : + json.isArray () ? 0 : + json.isObject() ? 0 : + json.isString() ? 0 : json.asInt() + ); + + return data; +} + + +/* ================================================================= * + * const ofPoint Decode(const ofxJSONElement& json); * + * ================================================================= */ +template<> +const ofPoint ofxJSONElement :: Decode(const ofxJSONElement& json){ + ofPoint data; + + data.x = json["x"].isNull () ? 0 : + json["x"].isArray () ? 0 : + json["x"].isObject() ? 0 : + json["x"].isString() ? 0 : json["x"].asFloat(); + + data.y = json["y"].isNull () ? 0 : + json["y"].isArray () ? 0 : + json["y"].isObject() ? 0 : + json["y"].isString() ? 0 : json["y"].asFloat(); + + data.z = json["z"].isNull () ? 0 : + json["z"].isArray () ? 0 : + json["z"].isObject() ? 0 : + json["z"].isString() ? 0 : json["z"].asFloat(); + + return data; +} + +/* ================================================================= * + * const ofVec2f Decode(const ofxJSONElement& json); * + * ================================================================= */ +template<> +const ofVec2f ofxJSONElement :: Decode(const ofxJSONElement& json){ + + ofVec2f data; + + data.x = json["x"].isNull () ? 0 : + json["x"].isArray () ? 0 : + json["x"].isObject() ? 0 : + json["x"].isString() ? 0 : json["x"].asFloat(); + + data.y = json["y"].isNull () ? 0 : + json["y"].isArray () ? 0 : + json["y"].isObject() ? 0 : + json["y"].isString() ? 0 : json["y"].asFloat(); + + return data; + +} + +/* ================================================================= * + * const ofRectangle Decode +const ofRectangle ofxJSONElement :: Decode(const ofxJSONElement& json){ + + ofRectangle data; + + data.x = json["x"].isNull () ? 0 : + json["x"].isArray () ? 0 : + json["x"].isObject() ? 0 : + json["x"].isString() ? 0 : json["x"].asFloat(); + + data.y = json["y"].isNull () ? 0 : + json["y"].isArray () ? 0 : + json["y"].isObject() ? 0 : + json["y"].isString() ? 0 : json["y"].asFloat(); + + data.width = json["width"].isNull () ? 0 : + json["width"].isArray () ? 0 : + json["width"].isObject() ? 0 : + json["width"].isString() ? 0 : json["width"].asFloat(); + + data.height = json["height"].isNull () ? 0 : + json["height"].isArray () ? 0 : + json["height"].isObject() ? 0 : + json["height"].isString() ? 0 : json["height"].asFloat(); + + return data; + +} diff --git a/src/ofxJSONElement.h b/src/ofxJSONElement.h index bab2e13..f7743a8 100755 --- a/src/ofxJSONElement.h +++ b/src/ofxJSONElement.h @@ -15,6 +15,7 @@ #include #include "json/json.h" +#include "ofMain.h" #include "ofLog.h" #include "ofURLFileLoader.h" @@ -40,4 +41,11 @@ class ofxJSONElement: public Json::Value static std::string toString(Json::ValueType type); + static const ofxJSONElement Encode(const ofPoint& data); + static const ofxJSONElement Encode(const ofVec2f& data); + static const ofxJSONElement Encode(const ofRectangle& data); + + template + static const T Decode(const ofxJSONElement& json); + };