Search in sources :

Example 1 with BSONObject

use of org.bson.BSONObject in project camel by apache.

the class MongoDbConversionsTest method testInsertBsonInputStream.

@Test
public void testInsertBsonInputStream() {
    assertEquals(0, testCollection.count());
    DefaultDBEncoder encoder = new DefaultDBEncoder();
    BSONObject bsonObject = new BasicDBObject();
    bsonObject.put("_id", "testInsertBsonString");
    Object result = template.requestBody("direct:insertJsonString", new ByteArrayInputStream(encoder.encode(bsonObject)));
    DBObject b = testCollection.find(new BasicDBObject("_id", "testInsertBsonString")).first();
    assertNotNull("No record with 'testInsertBsonString' _id", b);
}
Also used : DefaultDBEncoder(com.mongodb.DefaultDBEncoder) BasicDBObject(com.mongodb.BasicDBObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 2 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method writeBigDecimalsAsDecimal128s.

/**
 * Test if {@link BigDecimal} objects can be serialized as {@link Decimal128}s
 * @throws Exception if something goes wrong
 */
@Test
public void writeBigDecimalsAsDecimal128s() throws Exception {
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("big", new BigDecimal("0.3"));
    BSONObject obj = generateAndParse(data);
    Double result = (Double) obj.get("big");
    // BigDecimal("0.3") does not equal 0.3!
    assertEquals(0.3, result, 0.000001);
    assertFalse(Double.valueOf(0.3).equals(result));
    data = new LinkedHashMap<String, Object>();
    data.put("big", new BigDecimal("0.3"));
    obj = generateAndParse(data, Feature.WRITE_BIGDECIMALS_AS_DECIMAL128);
    org.bson.types.Decimal128 strResult = (org.bson.types.Decimal128) obj.get("big");
    assertEquals(new BigDecimal("0.3"), strResult.bigDecimalValue());
}
Also used : BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) Decimal128(de.undercouch.bson4jackson.types.Decimal128) BigDecimal(java.math.BigDecimal) LinkedHashMap(java.util.LinkedHashMap) BSONObject(org.bson.BSONObject) Test(org.junit.Test)

Example 3 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method timestamps.

/**
 * Test if {@link Timestamp} objects can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void timestamps() throws Exception {
    Timestamp timestamp = new Timestamp(100, 200);
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("timestamp", timestamp);
    BSONObject obj = generateAndParse(data);
    BSONTimestamp result = (BSONTimestamp) obj.get("timestamp");
    assertNotNull(result);
    assertEquals(timestamp.getInc(), result.getInc());
    assertEquals(timestamp.getTime(), result.getTime());
}
Also used : BSONObject(org.bson.BSONObject) BSONTimestamp(org.bson.types.BSONTimestamp) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) Timestamp(de.undercouch.bson4jackson.types.Timestamp) BSONTimestamp(org.bson.types.BSONTimestamp) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 4 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method javascript.

/**
 * Test if {@link JavaScript} objects can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void javascript() throws Exception {
    JavaScript javaScript = new JavaScript("a < 100");
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("javaScript", javaScript);
    BSONObject obj = generateAndParse(data);
    Code result = (Code) obj.get("javaScript");
    assertNotNull(result);
    assertEquals(javaScript.getCode(), result.getCode());
}
Also used : JavaScript(de.undercouch.bson4jackson.types.JavaScript) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) Code(org.bson.types.Code) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 5 with BSONObject

use of org.bson.BSONObject in project bson4jackson by michel-kraemer.

the class BsonGeneratorTest method arrays.

/**
 * Test if arrays can be serialized correctly
 * @throws Exception if something goes wrong
 */
@Test
@SuppressWarnings("unchecked")
public void arrays() throws Exception {
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("Int32", 5);
    data.put("Arr", Arrays.asList("a", "b", "c"));
    data.put("Int64", 10L);
    List<String> a3 = Arrays.asList("d", "e", "f");
    List<String> a4 = Arrays.asList("g", "h", "j");
    List<List<String>> a5 = new ArrayList<List<String>>();
    a5.add(a3);
    a5.add(a4);
    data.put("Arr2", a5);
    Map<String, Object> data2 = new LinkedHashMap<String, Object>();
    data2.put("Str", "Hello");
    List<Map<String, Object>> a6 = new ArrayList<Map<String, Object>>();
    a6.add(data2);
    data.put("Arr3", a6);
    BSONObject obj = generateAndParse(data);
    assertEquals(5, obj.get("Int32"));
    List<String> o = (List<String>) obj.get("Arr");
    assertEquals(3, o.size());
    assertEquals("a", o.get(0));
    assertEquals("b", o.get(1));
    assertEquals("c", o.get(2));
    assertEquals(10L, obj.get("Int64"));
    List<List<String>> o5 = (List<List<String>>) obj.get("Arr2");
    assertEquals(a5, o5);
    List<BSONObject> o6 = (List<BSONObject>) obj.get("Arr3");
    assertEquals(1, o6.size());
    BSONObject b6 = o6.get(0);
    assertEquals("Hello", b6.get("Str"));
}
Also used : ArrayList(java.util.ArrayList) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) ArrayList(java.util.ArrayList) List(java.util.List) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

BSONObject (org.bson.BSONObject)101 BasicBSONObject (org.bson.BasicBSONObject)49 Test (org.junit.Test)34 BasicDBObject (com.mongodb.BasicDBObject)19 SerializableString (com.fasterxml.jackson.core.SerializableString)14 SerializedString (com.fasterxml.jackson.core.io.SerializedString)14 LinkedHashMap (java.util.LinkedHashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 DBObject (com.mongodb.DBObject)10 ArrayList (java.util.ArrayList)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Map (java.util.Map)6 BasicBSONDecoder (org.bson.BasicBSONDecoder)6 BSONDecoder (org.bson.BSONDecoder)5 BSONEncoder (org.bson.BSONEncoder)5 BasicBSONEncoder (org.bson.BasicBSONEncoder)5 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4