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;
}
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;
}
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;
}
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());
}
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;
}
Aggregations