Пример #1
0
JSONObject::JSONObject(JSONHelper &reader) {
  while (true) {
    Token type = reader.nextToken();
    if (type == TokenObjectClose)
      break;
    if (type != TokenString)
      throw JSONParseException("Expected quoted string", reader.location());
    QString key = reader.readString();
    if (key.length() == 0)
      throw JSONParseException("Empty object key", reader.location());
    if (reader.nextToken() != TokenKeySeparator)
      throw JSONParseException("Expected ':'", reader.location());
    JSONData *value;
    switch (reader.nextToken()) {
      case TokenNULL: value = NULL; break;
      case TokenTRUE: value = new JSONBool(true); break;
      case TokenFALSE: value = new JSONBool(false); break;
      case TokenString: value = new JSONString(reader.readString()); break;
      case TokenNumber: value = new JSONNumber(reader.readDouble()); break;
      case TokenObject: value = new JSONObject(reader); break;
      case TokenArray: value = new JSONArray(reader); break;
      default: throw JSONParseException("Expected value", reader.location());
    }
    children[key] = value;
    type = reader.nextToken();  // comma or end
    if (type == TokenObjectClose)
      break;
    if (type != TokenValueSeparator)
      throw JSONParseException("Expected ',' or '}'", reader.location());
  }
}
Пример #2
0
bool JSONObject::load(JSONHelper &reader)
{
	while (true)
	{
		Token token=reader.nextToken();
		if (token==TokenObjectClose)
			break;
		if (token!=TokenString) //error: object doesn't start with key
			return false;
		QString key=reader.readString();
		if (key.length()==0) //error: empty key
			return false;
		if (reader.nextToken()!=TokenKeySeparator) //error: no colon after key
			return false;
		JSON *child=parse(reader);
		if (!child)  //error: child failed to parse
			return false;
		children.insert(key,child);
		token=reader.nextToken();
		if (token==TokenObjectClose)
			break;
		if (token!=TokenValueSeparator) //error: no comma
			return false;
	}
	return true;
}
Пример #3
0
JSONArray::JSONArray(JSONHelper &reader) {
  while (true) {
    Token type = reader.nextToken();
    if (type == TokenArrayClose)
      break;
    JSONData *value;
    switch (type) {
      case TokenNULL: value = NULL; break;
      case TokenTRUE: value = new JSONBool(true); break;
      case TokenFALSE: value = new JSONBool(false); break;
      case TokenString: value = new JSONString(reader.readString()); break;
      case TokenNumber: value = new JSONNumber(reader.readDouble()); break;
      case TokenObject: value = new JSONObject(reader); break;
      case TokenArray: value = new JSONArray(reader); break;
      default: throw JSONParseException("Expected Value", reader.location());
    }
    data.append(value);
    type = reader.nextToken();  // comma or end
    if (type == TokenArrayClose)
      break;
    if (type != TokenValueSeparator)
      throw JSONParseException("Expected ',' or ']'", reader.location());
  }
}