Search in sources :

Example 6 with JsonParseException

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

the class ParameterBindingJsonReader method visitDateTimeExtendedJson.

private long visitDateTimeExtendedJson() {
    long value;
    verifyToken(JsonTokenType.COLON);
    JsonToken valueToken = popToken();
    if (valueToken.getType() == JsonTokenType.BEGIN_OBJECT) {
        JsonToken nameToken = popToken();
        String name = nameToken.getValue(String.class);
        if (!name.equals("$numberLong")) {
            throw new JsonParseException(String.format("JSON reader expected $numberLong within $date, but found %s", name));
        }
        value = visitNumberLongExtendedJson();
        verifyToken(JsonTokenType.END_OBJECT);
    } else {
        if (valueToken.getType() == JsonTokenType.INT32 || valueToken.getType() == JsonTokenType.INT64) {
            value = valueToken.getValue(Long.class);
        } else if (valueToken.getType() == JsonTokenType.STRING || valueToken.getType() == JsonTokenType.UNQUOTED_STRING) {
            // Spring Data Customization START
            Object dt = bindableValueFor(valueToken).getValue();
            if (dt instanceof Date) {
                value = ((Date) dt).getTime();
            } else if (dt instanceof Number) {
                value = NumberUtils.convertNumberToTargetClass((Number) dt, Long.class);
            } else {
                try {
                    value = DateTimeFormatter.parse(dt.toString());
                } catch (IllegalArgumentException e) {
                    throw new JsonParseException(String.format("Failed to parse string '%s' as a date", dt), e);
                }
            }
        // Spring Data Customization END
        } else {
            throw new JsonParseException("JSON reader expected an integer or string but found '%s'.", valueToken.getValue());
        }
        verifyToken(JsonTokenType.END_OBJECT);
    }
    return value;
}
Also used : String(java.lang.String) JsonParseException(org.bson.json.JsonParseException) Date(java.util.Date)

Example 7 with JsonParseException

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

the class ParameterBindingJsonReader method visitNumberLongConstructor.

private long visitNumberLongConstructor() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken valueToken = popToken();
    long value;
    if (valueToken.getType() == JsonTokenType.INT32 || valueToken.getType() == JsonTokenType.INT64) {
        value = valueToken.getValue(Long.class);
    } else if (valueToken.getType() == JsonTokenType.STRING) {
        value = Long.parseLong(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 8 with JsonParseException

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

the class ParameterBindingJsonReader method visitNumberIntExtendedJson.

private Integer visitNumberIntExtendedJson() {
    verifyToken(JsonTokenType.COLON);
    Integer value;
    String intAsString = readStringFromExtendedJson();
    try {
        value = Integer.valueOf(intAsString);
    } catch (NumberFormatException e) {
        throw new JsonParseException(format("Exception converting value '%s' to type %s", intAsString, Integer.class.getName()), e);
    }
    verifyToken(JsonTokenType.END_OBJECT);
    return value;
}
Also used : String(java.lang.String) JsonParseException(org.bson.json.JsonParseException)

Example 9 with JsonParseException

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

the class ParameterBindingJsonReader method visitDateTimeConstructorWithOutNew.

private String visitDateTimeConstructorWithOutNew() {
    verifyToken(JsonTokenType.LEFT_PAREN);
    JsonToken token = popToken();
    if (token.getType() != JsonTokenType.RIGHT_PAREN) {
        while (token.getType() != JsonTokenType.END_OF_FILE) {
            token = popToken();
            if (token.getType() == JsonTokenType.RIGHT_PAREN) {
                break;
            }
        }
        if (token.getType() != JsonTokenType.RIGHT_PAREN) {
            throw new JsonParseException("JSON reader expected a ')' but found '%s'.", token.getValue());
        }
    }
    DateFormat df = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.ENGLISH);
    return df.format(new Date());
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) JsonParseException(org.bson.json.JsonParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 10 with JsonParseException

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

the class ParameterBindingJsonReader method visitNumberLongExtendedJson.

private Long visitNumberLongExtendedJson() {
    verifyToken(JsonTokenType.COLON);
    Long value;
    String longAsString = readStringFromExtendedJson();
    try {
        value = Long.valueOf(longAsString);
    } catch (NumberFormatException e) {
        throw new JsonParseException(format("Exception converting value '%s' to type %s", longAsString, Long.class.getName()), e);
    }
    verifyToken(JsonTokenType.END_OBJECT);
    return value;
}
Also used : 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