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