Search in sources :

Example 21 with JsonParseException

use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.

the class ParameterBindingJsonReader method visitDateTimeConstructor.

private long visitDateTimeConstructor() {
    DateFormat format = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH);
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken token = popToken();
    if (token.getType() == JsonTokenType.RIGHT_PAREN) {
        return new Date().getTime();
    } else if (token.getType() == JsonTokenType.STRING) {
        verifyToken(JsonTokenType.RIGHT_PAREN);
        String s = token.getValue(String.class);
        ParsePosition pos = new ParsePosition(0);
        Date dateTime = format.parse(s, pos);
        if (dateTime != null && pos.getIndex() == s.length()) {
            return dateTime.getTime();
        } else {
            throw new JsonParseException("JSON reader expected a date in 'EEE MMM dd yyyy HH:mm:ss z' format but found '%s'.", s);
        }
    } else if (token.getType() == JsonTokenType.INT32 || token.getType() == JsonTokenType.INT64) {
        long[] values = new long[7];
        int pos = 0;
        while (true) {
            if (pos < values.length) {
                values[pos++] = token.getValue(Long.class);
            }
            token = popToken();
            if (token.getType() == JsonTokenType.RIGHT_PAREN) {
                break;
            }
            if (token.getType() != JsonTokenType.COMMA) {
                throw new JsonParseException("JSON reader expected a ',' or a ')' but found '%s'.", token.getValue());
            }
            token = popToken();
            if (token.getType() != JsonTokenType.INT32 && token.getType() != JsonTokenType.INT64) {
                throw new JsonParseException("JSON reader expected an integer but found '%s'.", token.getValue());
            }
        }
        if (pos == 1) {
            return values[0];
        } else if (pos < 3 || pos > 7) {
            throw new JsonParseException("JSON reader expected 1 or 3-7 integers but found %d.", pos);
        }
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.set(Calendar.YEAR, (int) values[0]);
        calendar.set(Calendar.MONTH, (int) values[1]);
        calendar.set(Calendar.DAY_OF_MONTH, (int) values[2]);
        calendar.set(Calendar.HOUR_OF_DAY, (int) values[3]);
        calendar.set(Calendar.MINUTE, (int) values[4]);
        calendar.set(Calendar.SECOND, (int) values[5]);
        calendar.set(Calendar.MILLISECOND, (int) values[6]);
        return calendar.getTimeInMillis();
    } else {
        throw new JsonParseException("JSON reader expected an integer or a string but found '%s'.", token.getValue());
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(java.util.Calendar) String(java.lang.String) JsonParseException(org.bson.json.JsonParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 22 with JsonParseException

use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.

the class ParameterBindingJsonReader method visitBinDataExtendedJson.

private BsonBinary visitBinDataExtendedJson(final String firstKey) {
    Mark mark = new Mark();
    verifyToken(JsonTokenType.COLON);
    if (firstKey.equals("$binary")) {
        JsonToken nextToken = popToken();
        if (nextToken.getType() == JsonTokenType.BEGIN_OBJECT) {
            JsonToken nameToken = popToken();
            String firstNestedKey = nameToken.getValue(String.class);
            byte[] data;
            byte type;
            if (firstNestedKey.equals("base64")) {
                verifyToken(JsonTokenType.COLON);
                data = Base64Utils.decodeFromString(readStringFromExtendedJson());
                verifyToken(JsonTokenType.COMMA);
                verifyString("subType");
                verifyToken(JsonTokenType.COLON);
                type = readBinarySubtypeFromExtendedJson();
            } else if (firstNestedKey.equals("subType")) {
                verifyToken(JsonTokenType.COLON);
                type = readBinarySubtypeFromExtendedJson();
                verifyToken(JsonTokenType.COMMA);
                verifyString("base64");
                verifyToken(JsonTokenType.COLON);
                data = Base64Utils.decodeFromString(readStringFromExtendedJson());
            } else {
                throw new JsonParseException("Unexpected key for $binary: " + firstNestedKey);
            }
            verifyToken(JsonTokenType.END_OBJECT);
            verifyToken(JsonTokenType.END_OBJECT);
            return new BsonBinary(type, data);
        } else {
            mark.reset();
            return visitLegacyBinaryExtendedJson(firstKey);
        }
    } else {
        mark.reset();
        return visitLegacyBinaryExtendedJson(firstKey);
    }
}
Also used : String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 23 with JsonParseException

use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.

the class ParameterBindingJsonReader method readBsonType.

// CHECKSTYLE:OFF
@Override
public BsonType readBsonType() {
    if (isClosed()) {
        throw new IllegalStateException("This instance has been closed");
    }
    if (getState() == State.INITIAL || getState() == State.DONE || getState() == State.SCOPE_DOCUMENT) {
        // in JSON the top level value can be of any type so fall through
        setState(State.TYPE);
    }
    if (getState() != State.TYPE) {
        throwInvalidState("readBSONType", State.TYPE);
    }
    if (getContext().getContextType() == BsonContextType.DOCUMENT) {
        JsonToken nameToken = popToken();
        switch(nameToken.getType()) {
            case STRING:
            case UNQUOTED_STRING:
                // Spring Data Customization START
                setCurrentName(bindableValueFor(nameToken).getValue().toString());
                // Spring Data Customization END
                break;
            case END_OBJECT:
                setState(State.END_OF_DOCUMENT);
                return BsonType.END_OF_DOCUMENT;
            default:
                throw new JsonParseException("JSON reader was expecting a name but found '%s'.", nameToken.getValue());
        }
        JsonToken colonToken = popToken();
        if (colonToken.getType() != JsonTokenType.COLON) {
            throw new JsonParseException("JSON reader was expecting ':' but found '%s'.", colonToken.getValue());
        }
    }
    JsonToken token = popToken();
    if (getContext().getContextType() == BsonContextType.ARRAY && token.getType() == JsonTokenType.END_ARRAY) {
        setState(State.END_OF_ARRAY);
        return BsonType.END_OF_DOCUMENT;
    }
    // Spring Data Customization START
    boolean noValueFound = false;
    BindableValue bindableValue = null;
    switch(token.getType()) {
        case BEGIN_ARRAY:
            setCurrentBsonType(BsonType.ARRAY);
            break;
        case BEGIN_OBJECT:
            visitExtendedJSON();
            break;
        case DOUBLE:
            setCurrentBsonType(BsonType.DOUBLE);
            currentValue = token.getValue();
            break;
        case END_OF_FILE:
            setCurrentBsonType(BsonType.END_OF_DOCUMENT);
            break;
        case INT32:
            setCurrentBsonType(BsonType.INT32);
            currentValue = token.getValue();
            break;
        case INT64:
            setCurrentBsonType(BsonType.INT64);
            currentValue = token.getValue();
            break;
        case REGULAR_EXPRESSION:
            setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
            currentValue = bindableValueFor(token).getValue();
            break;
        case STRING:
            setCurrentBsonType(BsonType.STRING);
            currentValue = bindableValueFor(token).getValue().toString();
            break;
        case UNQUOTED_STRING:
            String value = token.getValue(String.class);
            if ("false".equals(value) || "true".equals(value)) {
                setCurrentBsonType(BsonType.BOOLEAN);
                currentValue = Boolean.parseBoolean(value);
            } else if ("Infinity".equals(value)) {
                setCurrentBsonType(BsonType.DOUBLE);
                currentValue = Double.POSITIVE_INFINITY;
            } else if ("NaN".equals(value)) {
                setCurrentBsonType(BsonType.DOUBLE);
                currentValue = Double.NaN;
            } else if ("null".equals(value)) {
                setCurrentBsonType(BsonType.NULL);
            } else if ("undefined".equals(value)) {
                setCurrentBsonType(BsonType.UNDEFINED);
            } else if ("MinKey".equals(value)) {
                visitEmptyConstructor();
                setCurrentBsonType(BsonType.MIN_KEY);
                currentValue = new MinKey();
            } else if ("MaxKey".equals(value)) {
                visitEmptyConstructor();
                setCurrentBsonType(BsonType.MAX_KEY);
                currentValue = new MaxKey();
            } else if ("BinData".equals(value)) {
                setCurrentBsonType(BsonType.BINARY);
                currentValue = visitBinDataConstructor();
            } else if ("Date".equals(value)) {
                currentValue = visitDateTimeConstructorWithOutNew();
                setCurrentBsonType(BsonType.STRING);
            } else if ("HexData".equals(value)) {
                setCurrentBsonType(BsonType.BINARY);
                currentValue = visitHexDataConstructor();
            } else if ("ISODate".equals(value)) {
                setCurrentBsonType(BsonType.DATE_TIME);
                currentValue = visitISODateTimeConstructor();
            } else if ("NumberInt".equals(value)) {
                setCurrentBsonType(BsonType.INT32);
                currentValue = visitNumberIntConstructor();
            } else if ("NumberLong".equals(value)) {
                setCurrentBsonType(BsonType.INT64);
                currentValue = visitNumberLongConstructor();
            } else if ("NumberDecimal".equals(value)) {
                setCurrentBsonType(BsonType.DECIMAL128);
                currentValue = visitNumberDecimalConstructor();
            } else if ("ObjectId".equals(value)) {
                setCurrentBsonType(BsonType.OBJECT_ID);
                currentValue = visitObjectIdConstructor();
            } else if ("Timestamp".equals(value)) {
                setCurrentBsonType(BsonType.TIMESTAMP);
                currentValue = visitTimestampConstructor();
            } else if ("RegExp".equals(value)) {
                setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
                currentValue = visitRegularExpressionConstructor();
            } else if ("DBPointer".equals(value)) {
                setCurrentBsonType(BsonType.DB_POINTER);
                currentValue = visitDBPointerConstructor();
            } else if ("UUID".equals(value) || "GUID".equals(value) || "CSUUID".equals(value) || "CSGUID".equals(value) || "JUUID".equals(value) || "JGUID".equals(value) || "PYUUID".equals(value) || "PYGUID".equals(value)) {
                setCurrentBsonType(BsonType.BINARY);
                currentValue = visitUUIDConstructor(value);
            } else if ("new".equals(value)) {
                visitNew();
            } else {
                bindableValue = bindableValueFor(token);
                if (bindableValue != null) {
                    if (bindableValue.getIndex() != -1) {
                        setCurrentBsonType(bindableValue.getType());
                    } else {
                        setCurrentBsonType(BsonType.STRING);
                    }
                    currentValue = bindableValue.getValue();
                } else {
                    noValueFound = true;
                }
            }
            break;
        default:
            noValueFound = true;
            break;
    }
    if (noValueFound) {
        throw new JsonParseException("JSON reader was expecting a value but found '%s'.", token.getValue());
    }
    if (getContext().getContextType() == BsonContextType.ARRAY || getContext().getContextType() == BsonContextType.DOCUMENT) {
        JsonToken commaToken = popToken();
        if (commaToken.getType() != JsonTokenType.COMMA) {
            pushToken(commaToken);
        }
    }
    switch(getContext().getContextType()) {
        case DOCUMENT:
        case SCOPE_DOCUMENT:
        default:
            setState(State.NAME);
            break;
        case ARRAY:
        case JAVASCRIPT_WITH_SCOPE:
        case TOP_LEVEL:
            setState(State.VALUE);
            break;
    }
    return getCurrentBsonType();
}
Also used : MinKey(org.bson.types.MinKey) MaxKey(org.bson.types.MaxKey) String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 24 with JsonParseException

use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.

the class ParameterBindingJsonReader method visitNumberIntConstructor.

private int visitNumberIntConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken valueToken = popToken();
    int value;
    if (valueToken.getType() == JsonTokenType.INT32) {
        value = valueToken.getValue(Integer.class);
    } else if (valueToken.getType() == JsonTokenType.STRING) {
        value = Integer.parseInt(valueToken.getValue(String.class));
    } else {
        throw new JsonParseException("JSON reader expected an integer or a string but found '%s'.", valueToken.getValue());
    }
    verifyToken(JsonTokenType.RIGHT_PAREN);
    return value;
}
Also used : String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 25 with JsonParseException

use of org.bson.json.JsonParseException in project spring-data-mongodb by spring-projects.

the class ParameterBindingJsonReader method visitNumberDecimalConstructor.

private Decimal128 visitNumberDecimalConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken valueToken = popToken();
    Decimal128 value;
    if (valueToken.getType() == JsonTokenType.INT32 || valueToken.getType() == JsonTokenType.INT64 || valueToken.getType() == JsonTokenType.DOUBLE) {
        value = valueToken.getValue(Decimal128.class);
    } else if (valueToken.getType() == JsonTokenType.STRING) {
        value = Decimal128.parse(valueToken.getValue(String.class));
    } else {
        throw new JsonParseException("JSON reader expected a number or a string but found '%s'.", valueToken.getValue());
    }
    verifyToken(JsonTokenType.RIGHT_PAREN);
    return value;
}
Also used : Decimal128(org.bson.types.Decimal128) String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Aggregations

JsonParseException (org.bson.json.JsonParseException)26 String (java.lang.String)19 Date (java.util.Date)4 SimpleDateFormat (java.text.SimpleDateFormat)3 DateFormat (java.text.DateFormat)2 ParsePosition (java.text.ParsePosition)2 Decimal128 (org.bson.types.Decimal128)2 MaxKey (org.bson.types.MaxKey)2 MinKey (org.bson.types.MinKey)2 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)2 Query (org.springframework.data.mongodb.core.query.Query)2 TextCriteria (org.springframework.data.mongodb.core.query.TextCriteria)2 RepositoryQuery (org.springframework.data.repository.query.RepositoryQuery)2 ReturnedType (org.springframework.data.repository.query.ReturnedType)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 SystemException (com.torodb.core.exceptions.SystemException)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1