Exemplo n.º 1
0
    MessagePtr JsonMsgDecoder::decodeMsg() {

        this->checkNext('{');
        this->checkNext('"');
        std::string key = this->nextKey();

        if(key != Message::MSG_HEADER) {
            throw JsonException("invalid msg-header!");
        }

        this->checkNext(':');
        this->checkNext('"');

        std::string msgkey = this->nextKey();

        this->checkNext(',');
        this->checkNext('"');
        key = this->nextKey();

        if(key != Message::DATA_HEADER) {
            throw JsonException("invalid data-header!");
        }

        this->checkNext(':');
        JsonItemPtr o = this->nextValue();
        this->checkNext('}');
        return MessagePtr(new Message(Message::convertToMsgType(msgkey), o));
    }
Exemplo n.º 2
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");
	}
}
Exemplo n.º 3
0
void Collection::checkWritable (std::string const& label)
{
    if (!enabled_)
        throw JsonException (label + ": not enabled");
    if (!writer_)
        throw JsonException (label + ": not writable");
}
Exemplo n.º 4
0
    JsonStringPtr JsonDecoder::nextString() {
        char c;
        std::stringstream sb;
        for(int i = 0; i < JsonDecoder::MAX_STRING_LENGTH; ++i) {
            c = this->next();
            switch(c) {
                case '\n':
                case '\r':
                    throw JsonException("invalid char");

                case '\\':
                    c = this->next();
                    switch (c) {
                        case 'b':
                            sb << '\b';
                            break;

                        case 't':
                            sb << '\t';
                            break;

                        case 'n':
                            sb << '\n';
                            break;

                        case 'f':
                            sb << '\f';
                            break;

                        case 'r':
                            sb << '\r';
                            break;

                        case 'u':
                            //FIXME sb.append((char)Integer.parseInt(this->next(4), 16));
                            break;

                        case '"':
                        case '\\':
                        case '/':
                            sb << c;
                            break;

                        default:
                            throw JsonException("invalid escape-sequence");
                    }
                    break;

                case '"':
                    return toJsonStringPtr(sb.str());

                default:
                    sb << c;
                    break;
            }
        }
        throw JsonException("maximum string-length reached!");
    }
Exemplo n.º 5
0
JsonObject::JsonObject(const QByteArray& json) {
    QJsonParseError pe;
    QJsonDocument doc = QJsonDocument::fromJson(json, &pe);
    if (pe.error != QJsonParseError::NoError)
        throw JsonException("cannot parse JSON: " + pe.errorString());
    if ( ! doc.isObject()) {
        throw JsonException("expected object");
    }
    o = doc.object();
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
0
    JsonItemPtr JsonDecoder::parseValue(const std::string &s) {
        if(!s.length()) {
            throw JsonException("empty value");
        }

        if(boost::iequals(s, "true")) {
            return toJsonBoolPtr(true);
        }
        if(boost::iequals(s, "false")) {
            return toJsonBoolPtr(false);
        }
        if(boost::iequals(s, "null")) {
            return toJsonNULLPtr();
        }
        char b = s[0];
        try {
            if((b >= '0' && b <= '9') || b == '-') {
                if(
                    b == '0' &&
                    s.length() > 2 &&
                    (s[1] == 'x' || s[1] == 'X')
                ) {
                    return JsonItemPtr(
                        new JsonNumber<long int>(strtol(&(s.c_str()[2]), NULL, 16))
                    );
                }

                if(
                    s.find('.') != std::string::npos ||
                    s.find('e') != std::string::npos ||
                    s.find('E') != std::string::npos
                ) {
                    std::stringstream buffer;
                    double output;
                    buffer << s;
                    buffer >> output;
                    return toJsonNumberPtr(output);
                }
                return toJsonNumberPtr(atol(s.c_str()));
            }
        } catch(std::exception e) {
            throw JsonException(
                std::string("parsing, error could not determine value: <") +
                std::string(e.what()) + ">"
            );
        }
        std::cout << "---" << s << "---" << std::endl;

        throw JsonException("parsing error, could not determine value" );
    }
Exemplo n.º 8
0
void JsonExpressFormater::WriteExpression(stringx name, ref<JsonNode> node, ref<StringBuilder> sb, int level)
{
	switch (node->GetType())
	{
	case JSN_String:
		WriteValue(name, '\"' + node->TextValue() + '\"', sb, level);
		break;
	case JSN_Integer:
		WriteValue(name, Converter::ToString(node->IntValue()), sb, level);
		break;
	case JSN_Boolean:
		WriteValue(name, node->BoolValue() ? __X("true") : __X("false"), sb, level);
		break;
	case JSN_None:
		WriteValue(name, __X("null"), sb, level);
		break;
	case JSN_Dictionary:
		WriteSubDictionary(name, node, sb, level);
		break;
	case JSN_NodeList:
		WriteSubList(name, node, sb, level);
		break;
	default:
		cdec_throw(JsonException(EC_JSON_NotImplemented, 0));
	}
}
Exemplo n.º 9
0
    JsonArrayPtr JsonDecoder::nextArray() {
        JsonArrayPtr v(new JsonArray());

        char c = this->next();

        if(c == ']') {
            return v;
        }
        this->lastChar = c;
        v->push_back(this->nextValue());

        while(true) {
            c = this->next();

            switch(c) {
                case ',':
                    v->push_back(this->nextValue());
                    break;

                case ']':
                    return v;

                default:
                    throw JsonException("expected ',' or ']'");
            }
        }
    }
Exemplo n.º 10
0
JsonValue JsonValue::read_number(const std::string &json, size_t &pos)
{
	int start_pos = pos;
	if (json[pos] == '-')
		pos++;
	while (pos < json.length() && json[pos] >= '0' && json[pos] <= '9')
		pos++;
	if (pos != json.length() && json[pos] == '.')
		pos++;
	while (pos < json.length() && json[pos] >= '0' && json[pos] <= '9')
		pos++;
	if (pos != json.length() && (json[pos] == 'e' || json[pos] == 'E'))
	{
		pos++;
		if (pos != json.length() && (json[pos] == '+' || json[pos] == '-'))
			pos++;
		while (pos < json.length() && json[pos] >= '0' && json[pos] <= '9')
			pos++;
	}
	int end_pos = pos;

	std::string number_string = json.substr(start_pos, end_pos - start_pos);
	if (number_string.empty())
		throw JsonException("Unexpected character in JSON data");

	double result = 0.0;
	sscanf(number_string.c_str(), "%lf", &result);
	return result;
}
Exemplo n.º 11
0
JsonValue JsonValue::read_boolean(const std::string &json, size_t &pos)
{
	if (json[pos] == 't')
	{
		if (pos + 4 > json.length() || memcmp(&json[pos], "true", 4) != 0)
			throw JsonException("Unexpected character in JSON data");
		pos += 4;
		return JsonValue::boolean(true);
	}
	else
	{
		if (pos + 5 > json.length() || memcmp(&json[pos], "false", 5) != 0)
			throw JsonException("Unexpected character in JSON data");
		pos += 5;
		return JsonValue::boolean(false);
	}
}
 cJSON *IncludeResolver::readToJson(const std::string &jsonFileName)
 {
     std::ifstream fin((m_path + jsonFileName).c_str());
     if (!fin)
         throw JsonException("Error Json Referenced File not present " + m_path + jsonFileName);
     std::stringstream buffer;
     buffer << fin.rdbuf();
     std::string str = buffer.str();
     return cJSON_Parse(str.c_str());
 }
Exemplo n.º 13
0
    JsonObjectPtr JsonDecoder::nextObject() {
        JsonObjectPtr map(new JsonObject());
        std::string key;
        char c;

        for(int i = 0; i < JsonDecoder::MAX_STRING_LENGTH; ++i) {
            c = this->next(!this->strict);
            switch(c) {
                case '}':
                    return map;

                case '"':
                    key = this->nextKey();
                    break;

                default:
                    throw JsonException("invalid key");
            }
            this->checkNext(':');

            if(map->find(key) != map->end()) {
                throw JsonException(std::string("duplicate key <") + key + ">");
            }
            (*map)[key] = this->nextValue();

            switch(this->next(!this->strict)) {
                case ',':
                    c = this->next(!this->strict);
                    if(c == '}'){
                        throw JsonException("expected new key");
                    }
                    this->lastChar = c;
                    break;

                case '}':
                    return map;

                default:
                    throw JsonException("expected a ',' or '}'");
            }
        }
        throw JsonException("maximum string-length reached!");
    }
Exemplo n.º 14
0
 void JsonDecoder::checkNext(const char x) {
     char c = this->next(!this->strict);
     if(c != x) {
         throw JsonException(
             std::string("expected '") +
             std::string(1, x) + "' found '" +
             std::string(1, c) + "'!"
         );
     }
 }
Exemplo n.º 15
0
    std::string JsonDecoder::nextKey() {
        std::stringstream sb;

        for(int i = 0; i < JsonDecoder::MAX_STRING_LENGTH; ++i) {
            char c = this->next();

            if(isspace(c) || !(isalnum(c) || c == '_' || c == '"')) {
                throw JsonException("key contains invalide char!");
            }

            if(c == '"') {
                std::string s = sb.str();
                boost::algorithm::trim(s);
                if(s.length() == 0) {
                    throw JsonException("key is empty");
                }
                return s;
            }
            sb << c;
        }
        throw JsonException("maximum string-length reached!");
    }
Exemplo n.º 16
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;
}
Exemplo n.º 17
0
    JsonItemPtr JsonDecoder::nextJValue()  {
        char c;
        std::stringstream sb;
        for(int i = 0; i < JsonDecoder::MAX_STRING_LENGTH; ++i) {
            c = this->next();

            if(c == ',' || c == ']' || c == '}') {
                this->lastChar = c;
                return this->parseValue(sb.str());
            }

            if(
                (c >= 'A' && c <= 'Z') ||
                (c >= 'a' && c <= 'z') ||
                (c >= '0' && c <= '9') ||
                c == '-' || c == '+'
            ) {
                sb << c;
                continue;
            }
            throw JsonException("parsing error");
        }
        throw JsonException("maximum string-length reached!");
    }
Exemplo n.º 18
0
    char JsonDecoder::next(bool ignoreWhitespace) {
        if(this->lastChar != 0) {
            char t = this->lastChar;
            this->lastChar = 0;
            return t;
        }
        char c;
        do {
            c = this->read();
        } while(::isspace(c) && ignoreWhitespace);

        if(c == -1 || c == 0) {
            throw JsonException("input stream corrupted!");
        }
        return c;
    }
Exemplo n.º 19
0
std::string JsonValue::read_string(const std::string &json, size_t &pos)
{
	pos++;
	if (pos == json.length())
		throw JsonException("Unexpected end of JSON data");

	std::string result;
	while (true)
	{
		if (pos == json.length())
		{
			throw JsonException("Unexpected end of JSON data");
		}
		else if (json[pos] == '"')
		{
			break;
		}
		else if (json[pos] == '\\')
		{
			pos++;
			if (pos == json.length())
				throw JsonException("Unexpected end of JSON data");

			unsigned codepoint;
			switch (json[pos])
			{
			case '"':
				result.push_back('"');
				break;
			case '\\':
				result.push_back('\\');
				break;
			case '/':
				result.push_back('/');
				break;
			case 'b':
				result.push_back('\b');
				break;
			case 'f':
				result.push_back('\f');
				break;
			case 'n':
				result.push_back('\n');
				break;
			case 'r':
				result.push_back('\r');
				break;
			case 't':
				result.push_back('\t');
				break;
			case 'u':
				if (pos + 5 > json.length())
					throw JsonException("Unexpected end of JSON data");
				
				codepoint = 0;
				for (int i = 0; i < 4; i++)
				{
					char c = json[pos + 1 + i];
					if (c >= '0' && c <= '9')
					{
						codepoint <<= 4;
						codepoint |= c - '0';
					}
					else if (c >= 'a' && c <= 'f')
					{
						codepoint <<= 4;
						codepoint |= c - 'a' + 10;
					}
					else if (c >= 'A' && c <= 'F')
					{
						codepoint <<= 4;
						codepoint |= c - 'A' + 10;
					}
					else
					{
						throw JsonException("Invalid unicode escape");
					}
				}
				result += StringHelp::unicode_to_utf8(codepoint);
				pos += 4;
				break;
			}
			pos++;
		}
		else
		{
			result.push_back(json[pos]);
			pos++;
		}
	}

	pos++;

	return result;
}
Exemplo n.º 20
0
    DefinitionsPtr JsonSchema::readRef(std::string m_ref)
    {
        std::string delimiter1 = "#";
        std::string delimiter2 = "/";
        std::string fileName;
        if (! m_ref.empty())
        {
            std::size_t pos = m_ref.find(delimiter1);
            if ( (pos = m_ref.find(delimiter1)) != std::string::npos)
            {
                fileName = m_ref.substr(0, pos);
                m_ref.erase(0, pos);
            }
            m_ref.erase(0, delimiter1 .length());
            std::string defName;

            if (! m_ref.empty())
            {
                m_ref.erase(0, delimiter2 .length());
                std::string keyName;
                if ( (pos = m_ref.find(delimiter2)) != std::string::npos)
                {
                    keyName = m_ref.substr(0, pos);
                    m_ref.erase(0, pos + delimiter2.length());
                    if (keyName == "definitions")
                    {
                        if ( (pos = m_ref.find(delimiter2)) != std::string::npos)
                        {
                            defName = m_ref.substr(0, pos);
                        }
                        else if (! m_ref.empty())
                        {
                            defName = m_ref;
                        }
                    }
                }
            }
            if (!fileName.empty())
            {
                if (!(defName.empty()))
                {
                    cJSON *m_json = m_includeResolver->readToJson(fileName);
                    JsonSchemaPtr Refparser = std::make_shared<JsonSchema>(m_json, m_includeResolver);
                    DefinitionsPtr definition = Refparser->getDefinition(defName);
                    if (definition == nullptr)
                        throw JsonException("Definition Name Incorrect");
                    return definition;
                }
            }
            else
            {
                if (!(defName.empty()))
                {
                    if (getDefinition(defName) == nullptr)
                        throw JsonException("Definition Name Incorrect");
                    return getDefinition(defName);
                }
            }
        }
        throw JsonException("Definition Name Empty");
        return nullptr;
    }
Exemplo n.º 21
0
void JsonObject::checkContains(const QString& key) const {
    if ( ! contains(key))
        throw JsonException("expected value for " + key);
}
Exemplo n.º 22
0
void wrongType(const QString& type, const QString& key) {
    throw JsonException("expected " + type + " for " + key);
}