Search in sources :

Example 11 with JsonParseException

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

the class ParameterBindingJsonReader method visitISODateTimeConstructor.

private long visitISODateTimeConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken token = popToken();
    if (token.getType() == JsonTokenType.RIGHT_PAREN) {
        return new Date().getTime();
    } else if (token.getType() != JsonTokenType.STRING) {
        throw new JsonParseException("JSON reader expected a string but found '%s'.", token.getValue());
    }
    verifyToken(JsonTokenType.RIGHT_PAREN);
    String[] patterns = { "yyyy-MM-dd", "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ss.SSSz" };
    SimpleDateFormat format = new SimpleDateFormat(patterns[0], Locale.ENGLISH);
    ParsePosition pos = new ParsePosition(0);
    String s = token.getValue(String.class);
    if (s.endsWith("Z")) {
        s = s.substring(0, s.length() - 1) + "GMT-00:00";
    }
    for (final String pattern : patterns) {
        format.applyPattern(pattern);
        format.setLenient(true);
        pos.setIndex(0);
        Date date = format.parse(s, pos);
        if (date != null && pos.getIndex() == s.length()) {
            return date.getTime();
        }
    }
    throw new JsonParseException("Invalid date format.");
}
Also used : String(java.lang.String) JsonParseException(org.bson.json.JsonParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) ParsePosition(java.text.ParsePosition)

Example 12 with JsonParseException

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

the class ParameterBindingJsonReader method visitHexDataConstructor.

private BsonBinary visitHexDataConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken subTypeToken = popToken();
    if (subTypeToken.getType() != JsonTokenType.INT32) {
        throw new JsonParseException("JSON reader expected a binary subtype but found '%s'.", subTypeToken.getValue());
    }
    verifyToken(JsonTokenType.COMMA);
    String hex = readStringFromExtendedJson();
    verifyToken(JsonTokenType.RIGHT_PAREN);
    if ((hex.length() & 1) != 0) {
        hex = "0" + hex;
    }
    for (final BsonBinarySubType subType : BsonBinarySubType.values()) {
        if (subType.getValue() == subTypeToken.getValue(Integer.class)) {
            return new BsonBinary(subType, decodeHex(hex));
        }
    }
    return new BsonBinary(decodeHex(hex));
}
Also used : String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 13 with JsonParseException

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

the class ParameterBindingJsonReader method visitDbPointerExtendedJson.

private BsonDbPointer visitDbPointerExtendedJson() {
    verifyToken(JsonTokenType.COLON);
    verifyToken(JsonTokenType.BEGIN_OBJECT);
    String ref;
    ObjectId oid;
    String firstKey = readStringFromExtendedJson();
    if (firstKey.equals("$ref")) {
        verifyToken(JsonTokenType.COLON);
        ref = readStringFromExtendedJson();
        verifyToken(JsonTokenType.COMMA);
        verifyString("$id");
        oid = readDbPointerIdFromExtendedJson();
        verifyToken(JsonTokenType.END_OBJECT);
    } else if (firstKey.equals("$id")) {
        oid = readDbPointerIdFromExtendedJson();
        verifyToken(JsonTokenType.COMMA);
        verifyString("$ref");
        verifyToken(JsonTokenType.COLON);
        ref = readStringFromExtendedJson();
    } else {
        throw new JsonParseException("Expected $ref and $id fields in $dbPointer document but found " + firstKey);
    }
    verifyToken(JsonTokenType.END_OBJECT);
    return new BsonDbPointer(ref, oid);
}
Also used : ObjectId(org.bson.types.ObjectId) String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 14 with JsonParseException

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

the class ParameterBindingJsonReader method readIntFromExtendedJson.

private int readIntFromExtendedJson() {
    JsonToken nextToken = popToken();
    int value;
    if (nextToken.getType() == JsonTokenType.INT32) {
        value = nextToken.getValue(Integer.class);
    } else if (nextToken.getType() == JsonTokenType.INT64) {
        value = nextToken.getValue(Long.class).intValue();
    } else {
        throw new JsonParseException("JSON reader expected an integer but found '%s'.", nextToken.getValue());
    }
    return value;
}
Also used : JsonParseException(org.bson.json.JsonParseException)

Example 15 with JsonParseException

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

the class ParameterBindingJsonReader method visitNew.

private void visitNew() {
    JsonToken typeToken = popToken();
    if (typeToken.getType() != JsonTokenType.UNQUOTED_STRING) {
        throw new JsonParseException("JSON reader expected a type name but found '%s'.", typeToken.getValue());
    }
    String value = typeToken.getValue(String.class);
    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)) {
        currentValue = visitBinDataConstructor();
        setCurrentBsonType(BsonType.BINARY);
    } else if ("Date".equals(value)) {
        currentValue = visitDateTimeConstructor();
        setCurrentBsonType(BsonType.DATE_TIME);
    } else if ("HexData".equals(value)) {
        currentValue = visitHexDataConstructor();
        setCurrentBsonType(BsonType.BINARY);
    } else if ("ISODate".equals(value)) {
        currentValue = visitISODateTimeConstructor();
        setCurrentBsonType(BsonType.DATE_TIME);
    } else if ("NumberInt".equals(value)) {
        currentValue = visitNumberIntConstructor();
        setCurrentBsonType(BsonType.INT32);
    } else if ("NumberLong".equals(value)) {
        currentValue = visitNumberLongConstructor();
        setCurrentBsonType(BsonType.INT64);
    } else if ("NumberDecimal".equals(value)) {
        currentValue = visitNumberDecimalConstructor();
        setCurrentBsonType(BsonType.DECIMAL128);
    } else if ("ObjectId".equals(value)) {
        currentValue = visitObjectIdConstructor();
        setCurrentBsonType(BsonType.OBJECT_ID);
    } else if ("RegExp".equals(value)) {
        currentValue = visitRegularExpressionConstructor();
        setCurrentBsonType(BsonType.REGULAR_EXPRESSION);
    } else if ("DBPointer".equals(value)) {
        currentValue = visitDBPointerConstructor();
        setCurrentBsonType(BsonType.DB_POINTER);
    } 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)) {
        currentValue = visitUUIDConstructor(value);
        setCurrentBsonType(BsonType.BINARY);
    } else {
        throw new JsonParseException("JSON reader expected a type name but found '%s'.", value);
    }
}
Also used : MinKey(org.bson.types.MinKey) MaxKey(org.bson.types.MaxKey) 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