Exemplo n.º 1
0
bool json::Reader::ParseObject(ConfigValue& value)
{
	value.SetEmptyObject();
	
	_cur++; // Skip '{'
	SkipSpaces();
	if(*_cur == '}') // Empty object
	{
		_cur++;
		return true;
	}	

	std::string name;
	while(1)
	{
		SkipSpaces();

		name = "";
		if(!ParseString(name))
		{
			Error("Failed to parse string");
			break; // Failed to parse string
		}

		SkipSpaces();
		if(*_cur != '=' && *_cur != ':')
		{
			Error("Expected '=' or ':'");
			return false;
		}
		_cur++;

		ConfigValue& elem = value[name.c_str()];
		if(!ParseValue(elem))
			break; // Failed to parse value

		SkipSpaces();

		char c = *_cur;
		if(c == ',') // Separator between elements (Optional)
		{
			_cur++;
			continue;
		}
		if(c == '}') // End of object
		{
			_cur++;
			return true;
		}		
	}	

	return false;
}
Exemplo n.º 2
0
//-------------------------------------------------------------------------------
bool json::Reader::Read(const char* doc, int64_t length, ConfigValue& root)
{
	_cur = _begin = doc;
	_end = doc + length;

	SkipSpaces();
	if(*_cur == '{')
		return ParseObject(root);
	
	// Assume root is an object
	root.SetEmptyObject();

	std::string name;
	while(1)
	{
		SkipSpaces();
		if(_cur == _end)
			break;


		name = "";
		if(!ParseString(name))
		{
			Error("Failed to parse string");
			return false;
		}

		SkipSpaces();
		if(*_cur != '=' && *_cur != ':')
		{
			Error("Expected '=' or ':'");
			return false;
		}
		_cur++;
		
		ConfigValue& elem = root[name.c_str()];
		if(!ParseValue(elem))
		{
			return false; // Failed to parse value
		}

		SkipSpaces();

		char c = *_cur;
		if(c == ',') // Separator between elements (Optional)
		{
			_cur++;
			continue;
		}
	}

	return true;
}