Search in sources :

Example 6 with BasicBSONEncoder

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

Example 7 with BasicBSONEncoder

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

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