Search in sources :

Example 1 with BSONEncoder

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);
}
Also used : BasicBSONEncoder(org.bson.BasicBSONEncoder) BasicBSONEncoder(org.bson.BasicBSONEncoder) BSONEncoder(org.bson.BSONEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with BSONEncoder

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());
}
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) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with BSONEncoder

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());
}
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) Test(org.junit.Test)

Example 4 with BSONEncoder

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;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BsonModule(de.undercouch.bson4jackson.BsonModule) BasicBSONEncoder(org.bson.BasicBSONEncoder) BasicBSONEncoder(org.bson.BasicBSONEncoder) BSONEncoder(org.bson.BSONEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BsonFactory(de.undercouch.bson4jackson.BsonFactory)

Example 5 with BSONEncoder

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));
}
Also used : BasicBSONEncoder(org.bson.BasicBSONEncoder) BasicBSONEncoder(org.bson.BasicBSONEncoder) BSONEncoder(org.bson.BSONEncoder) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Aggregations

BSONEncoder (org.bson.BSONEncoder)7 BasicBSONEncoder (org.bson.BasicBSONEncoder)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 BSONObject (org.bson.BSONObject)5 BasicBSONObject (org.bson.BasicBSONObject)5 Test (org.junit.Test)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Module (com.fasterxml.jackson.databind.Module)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 BsonFactory (de.undercouch.bson4jackson.BsonFactory)1 BsonModule (de.undercouch.bson4jackson.BsonModule)1 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)1