From bb330acf3c16b341c9a7076f44b3e2ae5331f9da Mon Sep 17 00:00:00 2001 From: leico Date: Tue, 30 Aug 2016 22:57:28 +0900 Subject: [PATCH 1/2] Add Convert Function usage: ofxJSON json; json["Rect"] = ofxJSON :: Convert( ofRectangle(100, 200, 300, 400) ); -------- { "Rect"{ "x" : 100 , "y" : 200 , "width" : 300 , "height" : 400 } } --- src/ofxJSONElement.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++ src/ofxJSONElement.h | 5 +++++ 2 files changed, 53 insertions(+) diff --git a/src/ofxJSONElement.cpp b/src/ofxJSONElement.cpp index 244f626..f514941 100755 --- a/src/ofxJSONElement.cpp +++ b/src/ofxJSONElement.cpp @@ -153,3 +153,51 @@ std::string ofxJSONElement::toString(Json::ValueType type) return "unknown"; } } + + + +/* ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||| Convert Series |||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * + * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ + +/* =============================================================== * + * const ofxJSONElement Convert(const ofPoint &data); * + * =============================================================== */ +const ofxJSONElement ofxJSONElement :: Convert(const ofPoint& data){ + ofxJSONElement json; + + json["x"] = data.x; + json["y"] = data.y; + json["z"] = data.z; + + return json; +} + +/* =============================================================== * + * const ofxJSONElement Convert(const ofVec2f &data); * + * =============================================================== */ +const ofxJSONElement ofxJSONElement :: Convert(const ofVec2f& data){ + ofxJSONElement json; + + json["x"] = data.x; + json["y"] = data.y; + + return json; +} + +/* =============================================================== * + * const ofxJSONElement Convert(const ofRectangle &data); * + * =============================================================== */ +const ofxJSONElement ofxJSONElement :: Convert(const ofRectangle& data){ + ofxJSONElement json; + + json["x"] = data.x; + json["y"] = data.y; + json["width"] = data.width; + json["height"] = data.height; + + return json; +} + diff --git a/src/ofxJSONElement.h b/src/ofxJSONElement.h index bab2e13..cbc665c 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,8 @@ class ofxJSONElement: public Json::Value static std::string toString(Json::ValueType type); + static const ofxJSONElement Convert(const ofPoint& data); + static const ofxJSONElement Convert(const ofVec2f& data); + static const ofxJSONElement Convert(const ofRectangle& data); + }; From eca76985ed20ad93323135e11a1fa2596b32eed4 Mon Sep 17 00:00:00 2001 From: leico Date: Wed, 31 Aug 2016 18:49:57 +0900 Subject: [PATCH 2/2] Change Convert -> Encode & Create Decode function usage: ofxJSON json; json.open("data.json"); ofRectangle rect = ofxJSON :: Decode(json["rect"]); --- src/ofxJSONElement.cpp | 117 ++++++++++++++++++++++++++++++++++++++--- src/ofxJSONElement.h | 9 ++-- 2 files changed, 116 insertions(+), 10 deletions(-) diff --git a/src/ofxJSONElement.cpp b/src/ofxJSONElement.cpp index f514941..7d02804 100755 --- a/src/ofxJSONElement.cpp +++ b/src/ofxJSONElement.cpp @@ -158,14 +158,14 @@ std::string ofxJSONElement::toString(Json::ValueType type) /* ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * - * ||| Convert Series |||||||||||||||||||||||||||||||||||||||| * + * ||| Encode Series ||||||||||||||||||||||||||||||||||||||||| * * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| * * ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| */ /* =============================================================== * - * const ofxJSONElement Convert(const ofPoint &data); * + * const ofxJSONElement Encode(const ofPoint &data); * * =============================================================== */ -const ofxJSONElement ofxJSONElement :: Convert(const ofPoint& data){ +const ofxJSONElement ofxJSONElement :: Encode(const ofPoint& data){ ofxJSONElement json; json["x"] = data.x; @@ -176,9 +176,9 @@ const ofxJSONElement ofxJSONElement :: Convert(const ofPoint& data){ } /* =============================================================== * - * const ofxJSONElement Convert(const ofVec2f &data); * + * const ofxJSONElement Encode(const ofVec2f &data); * * =============================================================== */ -const ofxJSONElement ofxJSONElement :: Convert(const ofVec2f& data){ +const ofxJSONElement ofxJSONElement :: Encode(const ofVec2f& data){ ofxJSONElement json; json["x"] = data.x; @@ -188,9 +188,9 @@ const ofxJSONElement ofxJSONElement :: Convert(const ofVec2f& data){ } /* =============================================================== * - * const ofxJSONElement Convert(const ofRectangle &data); * + * const ofxJSONElement Encode(const ofRectangle &data); * * =============================================================== */ -const ofxJSONElement ofxJSONElement :: Convert(const ofRectangle& data){ +const ofxJSONElement ofxJSONElement :: Encode(const ofRectangle& data){ ofxJSONElement json; json["x"] = data.x; @@ -201,3 +201,106 @@ const ofxJSONElement ofxJSONElement :: Convert(const ofRectangle& data){ 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 cbc665c..f7743a8 100755 --- a/src/ofxJSONElement.h +++ b/src/ofxJSONElement.h @@ -41,8 +41,11 @@ class ofxJSONElement: public Json::Value static std::string toString(Json::ValueType type); - static const ofxJSONElement Convert(const ofPoint& data); - static const ofxJSONElement Convert(const ofVec2f& data); - static const ofxJSONElement Convert(const ofRectangle& data); + 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); };