Search in sources :

Example 6 with Binary

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());
    }
}
Also used : Pattern(java.util.regex.Pattern) ObjectId(org.bson.types.ObjectId) Symbol(org.bson.types.Symbol) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Decimal128(org.bson.types.Decimal128) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary) UUID(java.util.UUID) Map(java.util.Map)

Example 7 with Binary

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);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) Binary(org.bson.types.Binary) Test(org.junit.Test)

Example 8 with Binary

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);
}
Also used : MinKey(org.bson.types.MinKey) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) CodeWithScope(org.bson.types.CodeWithScope) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Binary(org.bson.types.Binary) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) BsonRegularExpression(org.bson.BsonRegularExpression) Code(org.bson.types.Code) Date(java.util.Date) BsonTimestamp(org.bson.BsonTimestamp) Test(org.junit.Test)

Example 9 with Binary

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"));
}
Also used : Binary(org.bson.types.Binary) Test(org.junit.Test)

Example 10 with 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());
}
Also used : Binary(org.bson.types.Binary) Test(org.junit.Test)

Aggregations

Binary (org.bson.types.Binary)20 Date (java.util.Date)11 ObjectId (org.bson.types.ObjectId)10 Test (org.junit.Test)10 MaxKey (org.bson.types.MaxKey)8 MinKey (org.bson.types.MinKey)8 BSONTimestamp (org.bson.types.BSONTimestamp)7 Code (org.bson.types.Code)7 BasicDBObject (com.mongodb.BasicDBObject)5 DBRef (com.mongodb.DBRef)5 CodeWScope (org.bson.types.CodeWScope)5 Document (org.bson.Document)4 Symbol (org.bson.types.Symbol)3 MongoGridFSException (com.mongodb.MongoGridFSException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Arrays.asList (java.util.Arrays.asList)2 GregorianCalendar (java.util.GregorianCalendar)2 List (java.util.List)2 Map (java.util.Map)2