예제 #1
0
void JSONUtil::getDocument(const string& jsonTxt, JSONElement& root)
{
	string json(jsonTxt);
	root.setType(JSONElement::JSON_OBJECT);
	root.setName("_JSON_ROOT");
	int arrst = json.find("[");
	int objst = json.find("{");
	if(json.find("{")!=string::npos && json.find("}")!=string::npos && (objst<arrst || arrst==(int)string::npos))
	{
		root.setType(JSONElement::JSON_OBJECT);
		StringUtil::replaceFirst(json, "{", "");
		StringUtil::replaceLast(json, "}", "");
		readJSON(json,false,&root);
	}
	else if(json.find("[")!=string::npos && json.find("]")!=string::npos)
	{
		root.setType(JSONElement::JSON_ARRAY);
		StringUtil::replaceFirst(json, "[", "");
		StringUtil::replaceLast(json, "]", "");
		readJSON(json,true,&root);
	}
}
예제 #2
0
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);
}