use of org.bson.types.Binary in project mongo-java-driver by mongodb.
the class BasicBSONEncoder method _putObjectField.
/**
* Encodes any Object type
*
* @param name the field name
* @param initialValue the value to write
*/
protected void _putObjectField(final String name, final Object initialValue) {
if ("_transientFields".equals(name)) {
return;
}
if (name.contains("\0")) {
throw new IllegalArgumentException("Document field names can't have a NULL character. (Bad Key: '" + name + "')");
}
if ("$where".equals(name) && initialValue instanceof String) {
putCode(name, new Code((String) initialValue));
}
Object value = BSON.applyEncodingHooks(initialValue);
if (value == null) {
putNull(name);
} else if (value instanceof Date) {
putDate(name, (Date) value);
} else if (value instanceof Number) {
putNumber(name, (Number) value);
} else if (value instanceof Decimal128) {
putDecimal128(name, (Decimal128) value);
} else if (value instanceof Character) {
putString(name, value.toString());
} else if (value instanceof String) {
putString(name, value.toString());
} else if (value instanceof ObjectId) {
putObjectId(name, (ObjectId) value);
} else if (value instanceof Boolean) {
putBoolean(name, (Boolean) value);
} else if (value instanceof Pattern) {
putPattern(name, (Pattern) value);
} else if (value instanceof Iterable) {
putIterable(name, (Iterable) value);
} else if (value instanceof BSONObject) {
putObject(name, (BSONObject) value);
} else if (value instanceof Map) {
putMap(name, (Map) value);
} else if (value instanceof byte[]) {
putBinary(name, (byte[]) value);
} else if (value instanceof Binary) {
putBinary(name, (Binary) value);
} else if (value instanceof UUID) {
putUUID(name, (UUID) value);
} else if (value.getClass().isArray()) {
putArray(name, value);
} else if (value instanceof Symbol) {
putSymbol(name, (Symbol) value);
} else if (value instanceof BSONTimestamp) {
putTimestamp(name, (BSONTimestamp) value);
} else if (value instanceof CodeWScope) {
putCodeWScope(name, (CodeWScope) value);
} else if (value instanceof Code) {
putCode(name, (Code) value);
} else if (value instanceof DBRef) {
BSONObject temp = new BasicBSONObject();
DBRef dbRef = (DBRef) value;
temp.put("$ref", dbRef.getCollectionName());
temp.put("$id", dbRef.getId());
if (dbRef.getDatabaseName() != null) {
temp.put("$db", dbRef.getDatabaseName());
}
putObject(name, temp);
} else if (value instanceof MinKey) {
putMinKey(name);
} else if (value instanceof MaxKey) {
putMaxKey(name);
} else if (putSpecial(name, value)) {
// no-op
} else {
throw new IllegalArgumentException("Can't serialize " + value.getClass());
}
}
use of org.bson.types.Binary 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.Binary in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
Document doc = new Document();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BsonTimestamp(5, 1));
doc.append("pattern", new BsonRegularExpression("abc"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWithScope("code", new Document()));
doc.append("null", null);
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("list", Arrays.asList(7, 8, 9));
doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
collection.insertOne(doc);
Document found = collection.find().first();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BsonTimestamp.class, found.get("ts").getClass());
assertEquals(BsonRegularExpression.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(CodeWithScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(Binary.class, found.get("binary").getClass());
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
use of org.bson.types.Binary in project mongo-java-driver by mongodb.
the class DBCollectionTest method testOtherBinary.
@Test
public void testOtherBinary() {
byte[] data = { 1, 2, 3 };
Binary binaryValue = new Binary(BsonBinarySubType.USER_DEFINED, data);
collection.insert(new BasicDBObject("binary", binaryValue));
assertEquals(binaryValue, collection.findOne().get("binary"));
}
use of org.bson.types.Binary in project mongo-java-driver by mongodb.
the class JSONCallbackTest method binaryParsing.
@Test
public void binaryParsing() {
Binary parsedBinary = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : 0 }"));
assertEquals(0, parsedBinary.getType());
assertArrayEquals(new byte[] { 97, 98, 99, 100 }, parsedBinary.getData());
Binary parsedBinaryWithHexType = (Binary) JSON.parse(("{ \"$binary\" : \"YWJjZA==\", \"$type\" : \"80\" }"));
assertEquals((byte) 128, parsedBinaryWithHexType.getType());
assertArrayEquals(new byte[] { 97, 98, 99, 100 }, parsedBinaryWithHexType.getData());
}
Aggregations