use of org.bson.BasicBSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseBinaryObject.
/**
* Test if binary data can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseBinaryObject() throws Exception {
byte[] b = new byte[] { 1, 2, 3, 4, 5 };
BSONObject o = new BasicBSONObject();
o.put("barr", b);
BinaryClass data = parseBsonObject(o, BinaryClass.class);
assertArrayEquals(b, data.barr);
}
use of org.bson.BasicBSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseAsText.
/**
* Checks if the parser returns a textual representation of arbitrary
* tokens. See issue #23.
* @throws Exception if something went wrong
*/
@Test
public void parseAsText() throws Exception {
BSONObject o = new BasicBSONObject();
o.put("Float", 5.0f);
o.put("Int32", 1234);
BSONEncoder enc = new BasicBSONEncoder();
byte[] b = enc.encode(o);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
BsonFactory fac = new BsonFactory();
BsonParser dec = fac.createParser(bais);
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Float", dec.getCurrentName());
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, dec.nextToken());
assertEquals(5.0f, dec.getFloatValue(), 0.00001);
assertEquals("5.0", dec.getText());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Int32", dec.getCurrentName());
assertEquals(JsonToken.VALUE_NUMBER_INT, dec.nextToken());
assertEquals(1234, dec.getIntValue());
assertEquals("1234", dec.getText());
assertEquals(JsonToken.END_OBJECT, dec.nextToken());
}
use of org.bson.BasicBSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseEmbeddedDocument.
/**
* Test if embedded objects can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseEmbeddedDocument() throws Exception {
BSONObject o1 = new BasicBSONObject();
o1.put("Int32", 5);
BSONObject o2 = new BasicBSONObject();
o2.put("Int64", 10L);
o1.put("Obj", o2);
o1.put("String", "Hello");
Map<?, ?> data = parseBsonObject(o1);
assertEquals(3, data.size());
assertEquals(5, data.get("Int32"));
Map<?, ?> data2 = (Map<?, ?>) data.get("Obj");
assertEquals(1, data2.size());
assertEquals(10L, data2.get("Int64"));
assertEquals("Hello", data.get("String"));
}
use of org.bson.BasicBSONObject in project bson4jackson by michel-kraemer.
the class BsonDeserializersTest method generateAndParse.
private static <T> T generateAndParse(Object o, Class<T> cls) throws Exception {
BSONObject bo = new BasicBSONObject();
// that's why all properties of classes in TC must be named 'obj'
bo.put("obj", o);
BSONEncoder encoder = new BasicBSONEncoder();
byte[] barr = encoder.encode(bo);
ByteArrayInputStream bais = new ByteArrayInputStream(barr);
ObjectMapper om = new ObjectMapper(new BsonFactory());
om.registerModule(new BsonModule());
T r = om.readValue(bais, cls);
return r;
}
use of org.bson.BasicBSONObject in project mongo-hadoop by mongodb.
the class BSONSerDe method deserializeMap.
/**
* Also deserialize a Map with the same mapElemTypeInfo
* @param value the value for which to get the Hive representation
* @param valueTypeInfo a description of the value's type
* @param ext the field name
* @return the Hive representation of the value
*/
private Object deserializeMap(final Object value, final MapTypeInfo valueTypeInfo, final String ext) {
BasicBSONObject b = (BasicBSONObject) value;
TypeInfo mapValueTypeInfo = valueTypeInfo.getMapValueTypeInfo();
for (Entry<String, Object> entry : b.entrySet()) {
b.put(entry.getKey(), deserializeField(entry.getValue(), mapValueTypeInfo, ext));
}
return b.toMap();
}
Aggregations