use of org.bson.BSONEncoder in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseBsonObject.
private <T> T parseBsonObject(BSONObject o, Class<T> cls, Module... modules) throws IOException {
BSONEncoder enc = new BasicBSONEncoder();
byte[] b = enc.encode(o);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
BsonFactory fac = new BsonFactory();
ObjectMapper mapper = new ObjectMapper(fac);
if (modules != null) {
for (Module mod : modules) {
mapper.registerModule(mod);
}
}
fac.setCodec(mapper);
return mapper.readValue(bais, cls);
}
use of org.bson.BSONEncoder in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseBig.
/**
* Test if {@link BigDecimal} objects can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseBig() throws Exception {
BSONObject o = new BasicBSONObject();
o.put("Double", 5.0);
o.put("Int32", 1234);
BSONEncoder enc = new BasicBSONEncoder();
byte[] b = enc.encode(o);
ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectMapper mapper = new ObjectMapper(new BsonFactory());
mapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
mapper.configure(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS, true);
Map<?, ?> data = mapper.readValue(bais, Map.class);
assertEquals(BigDecimal.class, data.get("Double").getClass());
assertEquals(BigInteger.class, data.get("Int32").getClass());
}
use of org.bson.BSONEncoder 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.BSONEncoder 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.BSONEncoder in project mongo-hadoop by mongodb.
the class BSONWritable method write.
/**
* {@inheritDoc}
*
* @see Writable#write(DataOutput)
*/
public void write(final DataOutput out) throws IOException {
BSONEncoder enc = new BasicBSONEncoder();
BasicOutputBuffer buf = new BasicOutputBuffer();
enc.set(buf);
enc.putObject(doc);
enc.done();
buf.pipe(new DataOutputOutputStreamAdapter(out));
}
Aggregations