Search in sources :

Example 71 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method writeBinaryData.

/**
 * Test if  binary data can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void writeBinaryData() throws Exception {
    byte[] binary = new byte[] { (byte) 0x05, (byte) 0xff, (byte) 0xaf, (byte) 0x30, 'A', 'B', 'C', (byte) 0x13, (byte) 0x80, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("binary", binary);
    // binary data has to be converted to base64 with normal JSON
    ObjectMapper mapper = new ObjectMapper();
    String jsonString = mapper.writeValueAsString(data);
    assertEquals("{\"binary\":\"Bf+vMEFCQxOA/////w==\"}", jsonString);
    // with BSON we don't have to convert to base64
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BsonFactory bsonFactory = new BsonFactory();
    ObjectMapper om = new ObjectMapper(bsonFactory);
    om.writeValue(baos, data);
    // document header (4 bytes) + type (1 byte) + field_name ("binary", 6 bytes) +
    // end_of_string (1 byte) + binary_size (4 bytes) + subtype (1 byte) +
    // binary_data (13 bytes) + end_of_document (1 byte)
    int expectedLen = 4 + 1 + 6 + 1 + 4 + 1 + 13 + 1;
    assertEquals(expectedLen, baos.size());
    // BSON is smaller than JSON (at least in this case)
    assertTrue(baos.size() < jsonString.length());
    // test if binary data can be parsed
    BSONObject obj = generateAndParse(data);
    byte[] objbin = (byte[]) obj.get("binary");
    assertArrayEquals(binary, objbin);
}
Also used : BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 72 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method rawBytes.

/**
 * Test the {@link BsonGenerator#writeBinary(byte[], int, int)} method
 * @throws Exception if something goes wrong
 */
@Test
public void rawBytes() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BsonGenerator gen = new BsonGenerator(JsonGenerator.Feature.collectDefaults(), 0, baos);
    gen.writeStartObject();
    gen.writeFieldName("Test");
    gen.writeBinary(new byte[] { (byte) 1, (byte) 2 });
    gen.writeEndObject();
    gen.close();
    byte[] r = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(r);
    BSONDecoder decoder = new BasicBSONDecoder();
    BSONObject obj = decoder.readObject(bais);
    byte[] o = (byte[]) obj.get("Test");
    assertEquals(2, o.length);
    assertEquals((byte) 1, o[0]);
    assertEquals((byte) 2, o[1]);
}
Also used : BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) BSONObject(org.bson.BSONObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BasicBSONDecoder(org.bson.BasicBSONDecoder) Test(org.junit.Test)

Example 73 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonParserTest method parseBigString.

/**
 * Tests reading a very large string. Refers issue #18
 * @throws Exception if something went wrong
 * @author endasb
 */
@Test
public void parseBigString() throws Exception {
    BSONObject o = new BasicBSONObject();
    StringBuilder bigStr = new StringBuilder();
    for (int i = 0; i < 80000; i++) {
        bigStr.append("abc");
    }
    o.put("String", bigStr.toString());
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(240000, data.get("String").toString().length());
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Test(org.junit.Test)

Example 74 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonParserTest method parseUndefined.

/**
 * Test if undefined values are deserialized correct (i.e. if they
 * are skipped if the object is deserialized into a {@link Map})
 * @throws Exception if something goes wrong
 */
@Test
public void parseUndefined() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("Undefined", new Object());
    o.put("Int32", 5);
    BSONEncoder enc = new BasicBSONEncoder() {

        @Override
        protected boolean putSpecial(String name, Object o) {
            putUndefined(name);
            return true;
        }
    };
    byte[] b = enc.encode(o);
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    ObjectMapper mapper = new ObjectMapper(new BsonFactory());
    Map<?, ?> data = mapper.readValue(bais, Map.class);
    assertEquals(1, data.size());
    assertEquals(5, data.get("Int32"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONEncoder(org.bson.BasicBSONEncoder) BasicBSONEncoder(org.bson.BasicBSONEncoder) BSONEncoder(org.bson.BSONEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 75 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonParserTest method parsePrimitives.

/**
 * Test if primitives can be deserialized
 * @throws Exception if something goes wrong
 */
@Test
public void parsePrimitives() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("Double", 5.0);
    o.put("Float", 10.0f);
    o.put("String", "Hello World");
    o.put("Null", null);
    o.put("Bool1", true);
    o.put("Bool2", false);
    o.put("Int32", 1234);
    o.put("Int64", 1234L);
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(5.0, data.get("Double"));
    assertEquals(10.0, data.get("Float"));
    assertEquals("Hello World", data.get("String"));
    assertNull(data.get("Null"));
    assertEquals(true, data.get("Bool1"));
    assertEquals(false, data.get("Bool2"));
    assertEquals(1234, data.get("Int32"));
    assertEquals(1234L, data.get("Int64"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Test(org.junit.Test)

Aggregations

BSONObject (org.bson.BSONObject)101 BasicBSONObject (org.bson.BasicBSONObject)49 Test (org.junit.Test)34 BasicDBObject (com.mongodb.BasicDBObject)19 SerializableString (com.fasterxml.jackson.core.SerializableString)14 SerializedString (com.fasterxml.jackson.core.io.SerializedString)14 LinkedHashMap (java.util.LinkedHashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 DBObject (com.mongodb.DBObject)10 ArrayList (java.util.ArrayList)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Map (java.util.Map)6 BasicBSONDecoder (org.bson.BasicBSONDecoder)6 BSONDecoder (org.bson.BSONDecoder)5 BSONEncoder (org.bson.BSONEncoder)5 BasicBSONEncoder (org.bson.BasicBSONEncoder)5 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4