Search in sources :

Example 1 with JsonException

use of org.activityinfo.json.JsonException in project activityinfo by bedatadriven.

the class JsonTokenizer method parseObject.

JsonValue parseObject() throws JsonException {
    final JsonValue object = jsonFactory.createObject();
    int c = nextNonWhitespace();
    if (c != '{') {
        throw new JsonException("Payload does not begin with '{'.  Got " + c + "(" + Character.valueOf((char) c) + ")");
    }
    while (true) {
        c = nextNonWhitespace();
        switch(c) {
            case '}':
                // We're done.
                return object;
            case '"':
            case '\'':
                back(c);
                // Ready to start a key.
                final String key = nextString(c);
                if (nextNonWhitespace() != ':') {
                    throw new JsonException("Invalid object: expecting \":\"");
                }
                // TODO(knorton): Make sure this key is not already set.
                object.put(key, (JsonValue) nextValue());
                switch(nextNonWhitespace()) {
                    case ',':
                        break;
                    case '}':
                        return object;
                    default:
                        throw new JsonException("Invalid object: expecting } or ,");
                }
                break;
            case ',':
                break;
            default:
                if (lenient && (Character.isDigit((char) c) || Character.isLetterOrDigit((char) c))) {
                    StringBuilder keyBuffer = new StringBuilder();
                    keyBuffer.append(c);
                    while (true) {
                        c = next();
                        if (Character.isDigit((char) c) || Character.isLetterOrDigit((char) c)) {
                            keyBuffer.append(c);
                        } else {
                            back(c);
                            break;
                        }
                    }
                    if (nextNonWhitespace() != ':') {
                        throw new JsonException("Invalid object: expecting \":\"");
                    }
                    // TODO(knorton): Make sure this key is not already set.
                    object.put(keyBuffer.toString(), (JsonValue) nextValue());
                    switch(nextNonWhitespace()) {
                        case ',':
                            break;
                        case '}':
                            return object;
                        default:
                            throw new JsonException("Invalid object: expecting } or ,");
                    }
                } else {
                    throw new JsonException("Invalid object: ");
                }
        }
    }
}
Also used : JsonException(org.activityinfo.json.JsonException) JsonValue(org.activityinfo.json.JsonValue)

Example 2 with JsonException

use of org.activityinfo.json.JsonException in project activityinfo by bedatadriven.

the class JsonTokenizer method parseArray.

JsonValue parseArray() throws JsonException {
    final JsonValue array = jsonFactory.createArray();
    int c = nextNonWhitespace();
    assert c == '[';
    while (true) {
        c = nextNonWhitespace();
        switch(c) {
            case ']':
                return array;
            default:
                back(c);
                array.set(array.length(), (JsonValue) nextValue());
                final int d = nextNonWhitespace();
                switch(d) {
                    case ']':
                        return array;
                    case ',':
                        break;
                    default:
                        throw new JsonException("Invalid array: expected , or ]");
                }
        }
    }
}
Also used : JsonException(org.activityinfo.json.JsonException) JsonValue(org.activityinfo.json.JsonValue)

Aggregations

JsonException (org.activityinfo.json.JsonException)2 JsonValue (org.activityinfo.json.JsonValue)2