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);
}
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]);
}
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());
}
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"));
}
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"));
}
Aggregations