Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00040 #include <JSON.h>
00041
00042
00043 namespace beliefstate {
00044 JSON::JSON() {
00045 m_prRootProperty = NULL;
00046 }
00047
00048 JSON::~JSON() {
00049 if(m_prRootProperty) {
00050 delete m_prRootProperty;
00051 m_prRootProperty = NULL;
00052 }
00053 }
00054
00055 void JSON::parse(std::string strJSON, std::string strMimeType) {
00056 if(strMimeType == "") {
00057 strMimeType = "application/json";
00058 }
00059
00060 if(!m_prRootProperty) {
00061 delete m_prRootProperty;
00062 }
00063
00064 m_prRootProperty = new Property("root", Property::Object);
00065
00066 if(strMimeType == "application/json") {
00067 struct json_tokener* tok;
00068 struct json_object* jobj;
00069 enum json_tokener_error jteError;
00070
00071 tok = json_tokener_new_ex(1000);
00072
00073 if(!tok) {
00074 std::cerr << "Couldn't initialize json_tokener." << std::endl;
00075 } else {
00076 jobj = json_tokener_parse_ex(tok, strJSON.c_str(), strJSON.length());
00077 jteError = tok->err;
00078
00079 if(jteError == json_tokener_success) {
00080 if(jobj != NULL) {
00081 this->parse(jobj, m_prRootProperty);
00082 } else {
00083 std::cerr << "Failed to parse JSON: " << json_tokener_error_desc(jteError) << std::endl;
00084 }
00085
00086 jobj = NULL;
00087 } else {
00088 std::cerr << "Failed to parse JSON: " << json_tokener_error_desc(jteError) << std::endl;
00089 }
00090
00091 json_tokener_free(tok);
00092 }
00093 } else {
00094 this->parseXML(strJSON);
00095 }
00096 }
00097
00098 Property* JSON::parseXML(std::string strXML) {
00099 int nOffset = 0;
00100
00101 return this->parseXML(strXML, nOffset);
00102 }
00103
00104 Property* JSON::parseXML(std::string strXML, int& nOffset) {
00105
00106 }
00107
00108 void JSON::parse(json_object* jobj, Property* prParent) {
00109 switch(json_object_get_type(jobj)) {
00110 case json_type_boolean:
00111 prParent->set(Property::Boolean);
00112 prParent->set(json_object_get_boolean(jobj));
00113 break;
00114
00115 case json_type_double:
00116 prParent->set(Property::Double);
00117 prParent->set(json_object_get_double(jobj));
00118 break;
00119
00120 case json_type_int:
00121 prParent->set(Property::Integer);
00122 prParent->set(json_object_get_int(jobj));
00123 break;
00124
00125 case json_type_string:
00126 prParent->set(Property::String);
00127 prParent->set(std::string(json_object_get_string(jobj)));
00128 break;
00129
00130 case json_type_array:
00131 prParent->set(Property::Array);
00132 this->parseArray(jobj, NULL, prParent);
00133 break;
00134
00135 case json_type_object: {
00136 json_object_object_foreach(jobj, key, val) {
00137 Property* prChild = new Property();
00138 prParent->addSubProperty(prChild);
00139 prChild->setKey(key);
00140 prChild->set(Property::Object);
00141
00142 this->parse(val, prChild);
00143 }
00144 } break;
00145
00146 case json_type_null: {
00147 std::cout << "Null?" << std::endl;
00148 } break;
00149
00150 default:
00151 break;
00152 }
00153 }
00154
00155 void JSON::parseArray(json_object* jobj, char* key, Property* prParent) {
00156 json_object* jarray = jobj, *jvalue;
00157
00158 if(key) {
00159 jarray = json_object_object_get(jobj, key);
00160 }
00161
00162 for(int nI = 0; nI < json_object_array_length(jarray); nI++) {
00163 Property* prChild = new Property();
00164 prChild->set(Property::Array);
00165 prParent->addSubProperty(prChild);
00166
00167 if(key) {
00168 prChild->setKey(std::string(key));
00169 }
00170
00171 jvalue = json_object_array_get_idx(jarray, nI);
00172
00173 if(json_object_get_type(jvalue) == json_type_array) {
00174 this->parseArray(jvalue, NULL, prChild);
00175 } else {
00176 this->parse(jvalue, prChild);
00177 }
00178 }
00179 }
00180
00181 Property* JSON::rootProperty() {
00182 return m_prRootProperty;
00183 }
00184
00185 std::string JSON::encode(Property* prEncode) {
00186 std::string strEncoded = "";
00187
00188 if(prEncode == NULL) {
00189 prEncode = m_prRootProperty;
00190 }
00191
00192 if(prEncode->key() != "" && prEncode != m_prRootProperty) {
00193 strEncoded += "\"" + prEncode->key() + "\" : ";
00194 }
00195
00196 switch(prEncode->type()) {
00197 case Property::String:
00198 strEncoded += "\"" + prEncode->getString() + "\"";
00199 break;
00200
00201 case Property::Integer: {
00202 std::stringstream sts;
00203 sts << prEncode->getInteger();
00204 strEncoded += "\"" + sts.str() + "\"";
00205 } break;
00206
00207 case Property::Double: {
00208 std::stringstream sts;
00209 sts << prEncode->getDouble();
00210 strEncoded += "\"" + sts.str() + "\"";
00211 } break;
00212
00213 case Property::Boolean:
00214 strEncoded += std::string("\"") + (prEncode->getBoolean() ? "true" : "false") + "\"";
00215 break;
00216
00217 case Property::Object: {
00218 strEncoded += "{";
00219 std::list<Property*> lstSubProperties = prEncode->subProperties();
00220
00221 for(std::list<Property*>::iterator itP = lstSubProperties.begin();
00222 itP != lstSubProperties.end();
00223 itP++) {
00224 Property* prCurrent = *itP;
00225
00226 if(itP != lstSubProperties.begin()) {
00227 strEncoded += ", ";
00228 }
00229
00230 strEncoded += this->encode(prCurrent);
00231 }
00232
00233 strEncoded += "}";
00234 } break;
00235
00236 case Property::Array: {
00237 strEncoded += "[";
00238 std::list<Property*> lstSubProperties = prEncode->subProperties();
00239
00240 for(std::list<Property*>::iterator itP = lstSubProperties.begin();
00241 itP != lstSubProperties.end();
00242 itP++) {
00243 Property* prCurrent = *itP;
00244
00245 if(itP != lstSubProperties.begin()) {
00246 strEncoded += ", ";
00247 }
00248
00249 strEncoded += this->encode(prCurrent);
00250 }
00251
00252 strEncoded += "]";
00253 } break;
00254
00255 default:
00256 break;
00257 }
00258
00259 return strEncoded;
00260 }
00261 }