Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions src/ofxJSONElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
const T ofxJSONElement :: Decode(const ofxJSONElement& json){

T data;

data = static_cast<T>(
json.isNull () ? 0 :
json.isArray () ? 0 :
json.isObject() ? 0 :
json.isString() ? 0 : json.asInt()
);

return data;
}


/* ================================================================= *
* const ofPoint Decode<ofPoint>(const ofxJSONElement& json); *
* ================================================================= */
template<>
const ofPoint ofxJSONElement :: Decode<ofPoint>(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<ofVec2f>(const ofxJSONElement& json); *
* ================================================================= */
template<>
const ofVec2f ofxJSONElement :: Decode<ofVec2f>(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<ofRectangle(const ofxJSONElement& json); *
* ================================================================= */
template<>
const ofRectangle ofxJSONElement :: Decode<ofRectangle>(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;

}
8 changes: 8 additions & 0 deletions src/ofxJSONElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <string>
#include "json/json.h"
#include "ofMain.h"
#include "ofLog.h"
#include "ofURLFileLoader.h"

Expand All @@ -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<typename T>
static const T Decode(const ofxJSONElement& json);

};