void JSONElement::append(const JSONElement& element) { if(!_json) { return; } switch(element.getType()) { case cJSON_False: cJSON_AddFalseToObject(_json, element.getName().mb_str(wxConvUTF8).data()); break; case cJSON_True: cJSON_AddTrueToObject(_json, element.getName().mb_str(wxConvUTF8).data()); break; case cJSON_NULL: cJSON_AddNullToObject(_json, element.getName().mb_str(wxConvUTF8).data()); break; case cJSON_Number: cJSON_AddNumberToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetLong()); break; case cJSON_String: cJSON_AddStringToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element.getValue().GetString().mb_str(wxConvUTF8).data()); break; case cJSON_Array: case cJSON_Object: cJSON_AddItemToObject(_json, element.getName().mb_str(wxConvUTF8).data(), element._json); break; } }
void JSONElement::arrayAppend(const JSONElement& element) { if(!_json) { return; } cJSON* p = NULL; switch(element.getType()) { case cJSON_False: p = cJSON_CreateFalse(); break; case cJSON_True: p = cJSON_CreateTrue(); break; case cJSON_NULL: p = cJSON_CreateNull(); break; case cJSON_Number: p = cJSON_CreateNumber(element.getValue().GetDouble()); break; case cJSON_String: p = cJSON_CreateString(element.getValue().GetString().mb_str(wxConvUTF8).data()); break; case cJSON_Array: case cJSON_Object: p = element._json; break; } if(p) { cJSON_AddItemToArray(_json, p); } }
string JSONUtil::getDocumentStr(const JSONElement& doc) { string jsonText; if(doc.getType()==JSONElement::JSON_OBJECT) jsonText += "{"; else jsonText += "["; if(doc.hasChildren()) { for (int var = 0; var < (int)doc.getChildren().size(); ++var) { JSONElement* child = doc.getChildren().at(var); if(doc.getType()==JSONElement::JSON_OBJECT) jsonText += "\"" + child->getName() + "\":"; if(child->getType()==JSONElement::JSON_OBJECT || child->getType()==JSONElement::JSON_ARRAY) { jsonText += getDocumentStr(*child); } else { if(child->getType()==JSONElement::JSON_STRING) jsonText += "\"" + child->getValue() + "\""; else jsonText += child->getValue(); } if(var!=(int)doc.getChildren().size()-1) { jsonText += ", "; } } } if(doc.getType()==JSONElement::JSON_OBJECT) jsonText += "}"; else jsonText += "]"; return jsonText; }
void JSONUtil::readJSON(string& json, const bool& isarray, JSONElement *par) { if(json=="") return; string name, value; if(!isarray) { size_t stn = json.find("\""); while(stn!=string::npos && stn>0 && json.at(stn-1)=='\\') { stn = json.find("\"", stn+1); if(stn==0) { stn = string::npos; } } if(stn==string::npos) throw ("invalid json - no start '\"' found for name parameter"); size_t enn = json.find("\"", stn+1); while(enn!=string::npos && enn>0 && json.at(enn-1)=='\\') { enn = json.find("\"", enn+1); if(enn==0) { enn = string::npos; } } if(enn==string::npos) throw ("invalid json - no end '\"' found for name parameter"); if(stn!=enn-1) name = json.substr(stn+1, (enn-stn-1)); //StringUtil::trim(name); json = json.substr(enn+1); StringUtil::trim(json); size_t vst = json.find(":"); if(vst==string::npos) throw ("invalid json - no ':' found"); else if(vst!=0) throw ("invalid json - invalid json - invalid string before ':' found"); json = json.substr(vst+1); } JSONElement* element = new JSONElement; element->setName(name); StringUtil::trim(json); size_t env = json.find(","); size_t obs = json.find("{"); size_t ars = json.find("["); if(obs==0) { readBalancedJSON(value, json, false, obs); element->setType(JSONElement::JSON_OBJECT); } else if(ars==0) { readBalancedJSON(value, json, true, ars); element->setType(JSONElement::JSON_ARRAY); } else if(env==string::npos) { value = json; json = ""; element->setType(JSONElement::JSON_STRING); } else { if(obs!=string::npos && env==0 && (obs<ars || ars==string::npos)) { readBalancedJSON(value, json, false, obs); element->setType(JSONElement::JSON_OBJECT); } else if(ars!=string::npos && env==0 && (ars<obs || obs==string::npos)) { readBalancedJSON(value, json, true, ars); element->setType(JSONElement::JSON_ARRAY); } else if(obs!=string::npos && obs<env && (obs<ars || ars==string::npos)) { readBalancedJSON(value, json, false, obs); element->setType(JSONElement::JSON_OBJECT); } else if(ars!=string::npos && ars<env && (ars<obs || obs==string::npos)) { readBalancedJSON(value, json, true, ars); element->setType(JSONElement::JSON_ARRAY); } else { value = json.substr(0, env); json = json.substr(env+1); element->setType(JSONElement::JSON_STRING); } } if(value=="") { string ex = "invalid json - no value object found for name "+ name; throw (ex.c_str()); } if(element->getType()!=JSONElement::JSON_OBJECT && element->getType()!=JSONElement::JSON_ARRAY) { validateSetValue(element, value); } par->addChild(element); if(element->getType()==JSONElement::JSON_OBJECT) object(value, element); else if(element->getType()==JSONElement::JSON_ARRAY) array(value, element); readJSON(json,isarray,par); }