Example #1
0
JsonValue JsonValue::read_object(const std::string &json, size_t &pos)
{
	JsonValue result(Type::object);

	pos++;

	read_whitespace(json, pos);

	if (pos == json.length())
		throw JsonException("Unexpected end of JSON data");

	while (pos != json.length() && json[pos] != '}')
	{
		std::string key = read_string(json, pos);

		read_whitespace(json, pos);

		if (pos == json.length())
			throw JsonException("Unexpected end of JSON data");
		else if (json[pos] != ':')
			throw JsonException("Unexpected character in JSON data");
		pos++;

		read_whitespace(json, pos);

		result.members[key] = read(json, pos);

		read_whitespace(json, pos);

		if (pos == json.length())
		{
			throw JsonException("Unexpected end of JSON data");
		}
		else if (json[pos] == '}')
		{
			pos++;
			break;
		}
		else if (json[pos] == ',')
		{
			pos++;
			read_whitespace(json, pos);
		}
		else
		{
			throw JsonException("Unexpected character in JSON data");
		}
	}

	return result;
}
Example #2
0
std::vector<parse_tree> extract_trees(std::istream& stream)
{
    std::vector<parse_tree> results;

    read_whitespace(stream);

    while (stream)
    {
        results.emplace_back(read_tree(stream));
        read_whitespace(stream);
    }

    return results;
}
Example #3
0
JsonValue JsonValue::read(const std::string &json, size_t &pos)
{
	read_whitespace(json, pos);

	if (pos == json.length())
		throw JsonException("Unexpected end of JSON data");

	switch (json[pos])
	{
	case '{':
		return read_object(json, pos);
	case '[':
		return read_array(json, pos);
	case '"':
		return read_string(json, pos);
	case '-':
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
	case '9':
		return read_number(json, pos);
	case 'f':
	case 't':
		return read_boolean(json, pos);
	default:
		throw JsonException("Unexpected character in JSON data");
	}
}
Example #4
0
/* read the next number from the file */
double read_number(FILE *fp) {
    double x;
    read_whitespace(fp);
    if (fscanf(fp, "%lf", &x) != 1) {
	fprintf(stderr, "expecting a number at line %d of %s\n",
	    read_line, read_filename);
	exit(1);
    }
    return x;
}
Example #5
0
JsonValue JsonValue::read_array(const std::string &json, size_t &pos)
{
	JsonValue result(Type::array);

	pos++;

	read_whitespace(json, pos);

	if (pos == json.length())
		throw JsonException("Unexpected end of JSON data");

	while (json[pos] != ']')
	{
		read_whitespace(json, pos);
		result.items.push_back(read(json, pos));
		read_whitespace(json, pos);

		if (pos == json.length())
		{
			throw JsonException("Unexpected end of JSON data");
		}
		else if (json[pos] == ']')
		{
			break;
		}
		else if (json[pos] == ',')
		{
			pos++;
			read_whitespace(json, pos);
		}
		else
		{
			throw JsonException("Unexpected character in JSON data");
		}
	}
	pos++;

	return result;
}
Example #6
0
/* read the next token from the file */
char *read_token(FILE *fp) {
    static char word[80];
    if (!read_whitespace(fp) || fscanf(fp, "%s", word)!=1)
	strcpy(word, END_OF_FILE_TOKEN);
    return word;
}