use of org.bson.BasicBSONEncoder 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.BasicBSONEncoder in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseEmbeddedDocumentAsTree.
/**
* Tests reading an embedded document through
* {@link BsonParser#readValueAsTree()}. Refers issue #9
* @throws Exception if something went wrong
* @author audistard
*/
@Test
public void parseEmbeddedDocumentAsTree() throws Exception {
BSONObject o2 = new BasicBSONObject();
o2.put("Int64", 10L);
BSONObject o3 = new BasicBSONObject();
o3.put("Int64", 11L);
BSONObject o1 = new BasicBSONObject();
o1.put("Obj2", o2);
o1.put("Obj3", o3);
BSONEncoder enc = new BasicBSONEncoder();
byte[] b = enc.encode(o1);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
BsonFactory fac = new BsonFactory();
ObjectMapper mapper = new ObjectMapper(fac);
fac.setCodec(mapper);
BsonParser dec = fac.createParser(bais);
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Obj2", dec.getCurrentName());
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
JsonNode obj2 = dec.readValueAsTree();
assertEquals(1, obj2.size());
assertNotNull(obj2.get("Int64"));
assertEquals(10L, obj2.get("Int64").longValue());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Obj3", dec.getCurrentName());
assertEquals(JsonToken.START_OBJECT, dec.nextToken());
assertEquals(JsonToken.FIELD_NAME, dec.nextToken());
assertEquals("Int64", dec.getCurrentName());
assertEquals(JsonToken.VALUE_NUMBER_INT, dec.nextToken());
assertEquals(11L, dec.getLongValue());
assertEquals(JsonToken.END_OBJECT, dec.nextToken());
assertEquals(JsonToken.END_OBJECT, dec.nextToken());
}
Aggregations