Search in sources :

Example 6 with BasicBSONObject

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);
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Test(org.junit.Test)

Example 7 with BasicBSONObject

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());
}
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 8 with BasicBSONObject

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"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Map(java.util.Map) Test(org.junit.Test)

Example 9 with BasicBSONObject

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;
}
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 10 with BasicBSONObject

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();
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Aggregations

BasicBSONObject (org.bson.BasicBSONObject)88 Test (org.junit.Test)39 BSONObject (org.bson.BSONObject)37 ArrayList (java.util.ArrayList)15 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)14 BSONWritable (com.mongodb.hadoop.io.BSONWritable)13 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)13 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)13 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)13 BasicDBObject (com.mongodb.BasicDBObject)11 ObjectId (org.bson.types.ObjectId)11 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DataBag (org.apache.pig.data.DataBag)6 Map (java.util.Map)5 Tuple (org.apache.pig.data.Tuple)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Mongo (com.mongodb.Mongo)4 Date (java.util.Date)4 DoubleWritable (org.apache.hadoop.io.DoubleWritable)4