use of org.bson.BsonUndefined in project mongo-java-driver by mongodb.
the class JSONCallback method objectDone.
@Override
public Object objectDone() {
String name = curName();
Object o = super.objectDone();
if (_lastArray) {
return o;
}
BSONObject b = (BSONObject) o;
// override the object if it's a special type
if (b.containsField("$oid")) {
o = new ObjectId((String) b.get("$oid"));
} else if (b.containsField("$date")) {
if (b.get("$date") instanceof Number) {
o = new Date(((Number) b.get("$date")).longValue());
} else {
SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
if (o == null) {
// try older format with no ms
format = new SimpleDateFormat(_secDateFormat);
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
o = format.parse(b.get("$date").toString(), new ParsePosition(0));
}
}
} else if (b.containsField("$regex")) {
o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
} else if (b.containsField("$ts")) {
//Legacy timestamp format
Integer ts = ((Number) b.get("$ts")).intValue();
Integer inc = ((Number) b.get("$inc")).intValue();
o = new BSONTimestamp(ts, inc);
} else if (b.containsField("$timestamp")) {
BSONObject tsObject = (BSONObject) b.get("$timestamp");
Integer ts = ((Number) tsObject.get("t")).intValue();
Integer inc = ((Number) tsObject.get("i")).intValue();
o = new BSONTimestamp(ts, inc);
} else if (b.containsField("$code")) {
if (b.containsField("$scope")) {
o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
} else {
o = new Code((String) b.get("$code"));
}
} else if (b.containsField("$ref")) {
o = new DBRef((String) b.get("$ref"), b.get("$id"));
} else if (b.containsField("$minKey")) {
o = new MinKey();
} else if (b.containsField("$maxKey")) {
o = new MaxKey();
} else if (b.containsField("$uuid")) {
o = UUID.fromString((String) b.get("$uuid"));
} else if (b.containsField("$binary")) {
int type = (b.get("$type") instanceof String) ? Integer.valueOf((String) b.get("$type"), 16) : (Integer) b.get("$type");
byte[] bytes = DatatypeConverter.parseBase64Binary((String) b.get("$binary"));
o = new Binary((byte) type, bytes);
} else if (b.containsField("$undefined") && b.get("$undefined").equals(true)) {
o = new BsonUndefined();
} else if (b.containsField("$numberLong")) {
o = Long.valueOf((String) b.get("$numberLong"));
} else if (b.containsField("$numberDecimal")) {
o = Decimal128.parse((String) b.get("$numberDecimal"));
}
if (!isStackEmpty()) {
_put(name, o);
} else {
o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o);
setRoot(o);
}
return o;
}
use of org.bson.BsonUndefined in project mongo-java-driver by mongodb.
the class JSONSerializersTest method testUndefinedCodecs.
@Test
public void testUndefinedCodecs() {
ObjectSerializer serializer = JSONSerializers.getStrict();
StringBuilder buf = new StringBuilder();
serializer.serialize(new BsonUndefined(), buf);
assertEquals(buf.toString(), "{ \"$undefined\" : true}");
}
use of org.bson.BsonUndefined in project mongo-java-driver by mongodb.
the class JSONCallbackTest method undefinedParsing.
@Test
public void undefinedParsing() {
Object undefined = JSON.parse("{ \"$undefined\" : true }");
assertEquals(new BsonUndefined(), undefined);
}
Aggregations