use of org.bson.types.MaxKey in project mongo-java-driver by mongodb.
the class DocumentCodecTest method testPrimitiveBSONTypeCodecs.
@Test
public void testPrimitiveBSONTypeCodecs() throws IOException {
DocumentCodec documentCodec = new DocumentCodec();
Document doc = new Document();
doc.put("oid", new ObjectId());
doc.put("integer", 1);
doc.put("long", 2L);
doc.put("string", "hello");
doc.put("double", 3.2);
doc.put("decimal", Decimal128.parse("0.100"));
doc.put("binary", new Binary(BsonBinarySubType.USER_DEFINED, new byte[] { 0, 1, 2, 3 }));
doc.put("date", new Date(1000));
doc.put("boolean", true);
doc.put("code", new Code("var i = 0"));
doc.put("minkey", new MinKey());
doc.put("maxkey", new MaxKey());
// doc.put("pattern", Pattern.compile("^hello")); // TODO: Pattern doesn't override equals method!
doc.put("null", null);
documentCodec.encode(writer, doc, EncoderContext.builder().build());
BsonInput bsonInput = createInputBuffer();
Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
assertEquals(doc, decodedDocument);
}
use of org.bson.types.MaxKey in project mongo-java-driver by mongodb.
the class DBCollectionTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
BasicDBObject doc = new BasicDBObject();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("short", (short) 4);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("float", 6.0f);
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BSONTimestamp(5, 1));
doc.append("pattern", Pattern.compile(".*"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWScope("code", new BasicDBObject()));
doc.append("null", null);
doc.append("uuid", UUID.randomUUID());
doc.append("db ref", new com.mongodb.DBRef("test", new ObjectId()));
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("byte array", new byte[] { 1, 2, 3 });
doc.append("int array", new int[] { 4, 5, 6 });
doc.append("list", asList(7, 8, 9));
doc.append("doc list", asList(new Document("x", 1), new Document("x", 2)));
collection.insert(doc);
DBObject found = collection.findOne();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Integer.class, found.get("short").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("float").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BSONTimestamp.class, found.get("ts").getClass());
assertEquals(Pattern.class, found.get("pattern").getClass());
assertEquals(MinKey.class, found.get("minKey").getClass());
assertEquals(MaxKey.class, found.get("maxKey").getClass());
assertEquals(Code.class, found.get("js").getClass());
assertEquals(CodeWScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(UUID.class, found.get("uuid").getClass());
assertEquals(DBRef.class, found.get("db ref").getClass());
assertEquals(Binary.class, found.get("binary").getClass());
assertEquals(byte[].class, found.get("byte array").getClass());
assertTrue(found.get("int array") instanceof List);
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
use of org.bson.types.MaxKey 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.types.MaxKey in project mongo-java-driver by mongodb.
the class LazyBSONObject method readValue.
Object readValue(final BsonBinaryReader reader) {
switch(reader.getCurrentBsonType()) {
case DOCUMENT:
return readDocument(reader);
case ARRAY:
return readArray(reader);
case DOUBLE:
return reader.readDouble();
case STRING:
return reader.readString();
case BINARY:
byte binarySubType = reader.peekBinarySubType();
if (BsonBinarySubType.isUuid(binarySubType) && reader.peekBinarySize() == 16) {
return new UuidCodec().decode(reader, DecoderContext.builder().build());
}
BsonBinary binary = reader.readBinaryData();
if (binarySubType == BINARY.getValue() || binarySubType == OLD_BINARY.getValue()) {
return binary.getData();
} else {
return new Binary(binary.getType(), binary.getData());
}
case NULL:
reader.readNull();
return null;
case UNDEFINED:
reader.readUndefined();
return null;
case OBJECT_ID:
return reader.readObjectId();
case BOOLEAN:
return reader.readBoolean();
case DATE_TIME:
return new Date(reader.readDateTime());
case REGULAR_EXPRESSION:
BsonRegularExpression regularExpression = reader.readRegularExpression();
return Pattern.compile(regularExpression.getPattern(), BSON.regexFlags(regularExpression.getOptions()));
case DB_POINTER:
BsonDbPointer dbPointer = reader.readDBPointer();
return callback.createDBRef(dbPointer.getNamespace(), dbPointer.getId());
case JAVASCRIPT:
return new Code(reader.readJavaScript());
case SYMBOL:
return new Symbol(reader.readSymbol());
case JAVASCRIPT_WITH_SCOPE:
return new CodeWScope(reader.readJavaScriptWithScope(), (BSONObject) readJavaScriptWithScopeDocument(reader));
case INT32:
return reader.readInt32();
case TIMESTAMP:
BsonTimestamp timestamp = reader.readTimestamp();
return new BSONTimestamp(timestamp.getTime(), timestamp.getInc());
case INT64:
return reader.readInt64();
case DECIMAL128:
return reader.readDecimal128();
case MIN_KEY:
reader.readMinKey();
return new MinKey();
case MAX_KEY:
reader.readMaxKey();
return new MaxKey();
default:
throw new IllegalArgumentException("unhandled BSON type: " + reader.getCurrentBsonType());
}
}
use of org.bson.types.MaxKey in project mongo-java-driver by mongodb.
the class JSONSerializersTest method testLegacySerialization.
@Test
public void testLegacySerialization() {
ObjectSerializer serializer = JSONSerializers.getLegacy();
BasicDBObject testObj = new BasicDBObject();
// test ARRAY
BasicDBObject[] a = { new BasicDBObject("object1", "value1"), new BasicDBObject("object2", "value2") };
testObj.put("array", a);
StringBuilder buf = new StringBuilder();
serializer.serialize(a, buf);
assertEquals("[ { \"object1\" : \"value1\"} , { \"object2\" : \"value2\"}]", buf.toString());
// test BINARY
byte[] b = { 1, 2, 3, 4 };
testObj = new BasicDBObject("binary", new org.bson.types.Binary(b));
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"binary\" : <Binary Data>}", buf.toString());
// test BOOLEAN
testObj = new BasicDBObject("boolean", new Boolean(true));
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals(buf.toString(), "{ \"boolean\" : true}");
// test BSON_TIMESTAMP,
testObj = new BasicDBObject("timestamp", new BSONTimestamp());
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"timestamp\" : { \"$ts\" : 0 , \"$inc\" : 0}}", buf.toString());
// test BYTE_ARRAY
testObj = new BasicDBObject("byte_array", b);
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"byte_array\" : <Binary Data>}", buf.toString());
// test CODE
testObj = new BasicDBObject("code", new Code("test code"));
buf = new StringBuilder();
serializer.serialize(testObj, buf);
assertEquals("{ \"code\" : { \"$code\" : \"test code\"}}", buf.toString());
// test CODE_W_SCOPE
testObj = new BasicDBObject("scope", "scope of code");
CodeWScope codewscope = new CodeWScope("test code", testObj);
buf = new StringBuilder();
serializer.serialize(codewscope, buf);
assertEquals("{ \"$code\" : \"test code\" , \"$scope\" : { \"scope\" : \"scope of code\"}}", buf.toString());
// test DATE
Date d = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
buf = new StringBuilder();
serializer.serialize(d, buf);
assertEquals("{ \"$date\" : \"" + format.format(d) + "\"}", buf.toString());
// test DB_OBJECT implicit in preceding tests
// test DB_REF_BASE
DBRef dbref = new com.mongodb.DBRef("test.test", "4d83ab59a39562db9c1ae2af");
buf = new StringBuilder();
serializer.serialize(dbref, buf);
assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\"}", buf.toString());
DBRef dbrefWithDatabaseName = new com.mongodb.DBRef("mydb", "test.test", "4d83ab59a39562db9c1ae2af");
buf = new StringBuilder();
serializer.serialize(dbrefWithDatabaseName, buf);
assertEquals("{ \"$ref\" : \"test.test\" , \"$id\" : \"4d83ab59a39562db9c1ae2af\" , \"$db\" : \"mydb\"}", buf.toString());
// test ITERABLE
BasicBSONList testList = new BasicBSONList();
testList.add(new BasicDBObject("key1", "val1"));
testList.add(new BasicDBObject("key2", "val2"));
buf = new StringBuilder();
serializer.serialize(testList, buf);
assertEquals("[ { \"key1\" : \"val1\"} , { \"key2\" : \"val2\"}]", buf.toString());
// test MAP
Map<String, String> testMap = new TreeMap<String, String>();
testMap.put("key1", "val1");
testMap.put("key2", "val2");
buf = new StringBuilder();
serializer.serialize(testMap, buf);
assertEquals("{ \"key1\" : \"val1\" , \"key2\" : \"val2\"}", buf.toString());
// test MAXKEY
buf = new StringBuilder();
serializer.serialize(new MaxKey(), buf);
assertEquals("{ \"$maxKey\" : 1}", buf.toString());
// test MINKEY
buf = new StringBuilder();
serializer.serialize(new MinKey(), buf);
assertEquals("{ \"$minKey\" : 1}", buf.toString());
// test NULL
buf = new StringBuilder();
serializer.serialize(null, buf);
assertEquals(" null ", buf.toString());
// test NUMBER
Random rand = new Random();
long val = rand.nextLong();
Long longVal = new Long(val);
buf = new StringBuilder();
serializer.serialize(longVal, buf);
assertEquals(String.valueOf(val), buf.toString());
// test OBJECT_ID
buf = new StringBuilder();
serializer.serialize(new ObjectId("4d83ab3ea39562db9c1ae2ae"), buf);
assertEquals("{ \"$oid\" : \"4d83ab3ea39562db9c1ae2ae\"}", buf.toString());
// test PATTERN
buf = new StringBuilder();
serializer.serialize(Pattern.compile("test"), buf);
assertEquals("{ \"$regex\" : \"test\"}", buf.toString());
// test STRING
buf = new StringBuilder();
serializer.serialize("test string", buf);
assertEquals("\"test string\"", buf.toString());
// test UUID;
UUID uuid = UUID.randomUUID();
buf = new StringBuilder();
serializer.serialize(uuid, buf);
assertEquals("{ \"$uuid\" : \"" + uuid.toString() + "\"}", buf.toString());
// test Decimal128
Decimal128 decimal128 = Decimal128.parse("3.140");
buf = new StringBuilder();
serializer.serialize(decimal128, buf);
assertEquals("{ \"$numberDecimal\" : \"3.140\"}", buf.toString());
}
Aggregations