Search in sources :

Example 66 with BSONObject

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

the class BsonGeneratorTest method objectIds.

/**
 * Test if {@link ObjectId}s can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void objectIds() throws Exception {
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    ObjectId objectId = new ObjectId((int) (System.currentTimeMillis() / 1000), new Random().nextInt(), 100);
    data.put("_id", objectId);
    BSONObject obj = generateAndParse(data);
    org.bson.types.ObjectId result = (org.bson.types.ObjectId) obj.get("_id");
    assertNotNull(result);
    assertEquals(org.bson.types.ObjectId.createFromLegacyFormat(objectId.getTime(), objectId.getMachine(), objectId.getInc()), result);
}
Also used : Random(java.util.Random) ObjectId(de.undercouch.bson4jackson.types.ObjectId) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 67 with BSONObject

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

the class BsonGeneratorTest method patterns.

/**
 * Test if {@link Pattern}s can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void patterns() throws Exception {
    Pattern pattern = Pattern.compile("a.*a", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("pattern", pattern);
    BSONObject obj = generateAndParse(data);
    Pattern result = (Pattern) obj.get("pattern");
    assertNotNull(result);
    assertEquals(pattern.pattern(), result.pattern());
    assertEquals(pattern.flags(), result.flags());
}
Also used : Pattern(java.util.regex.Pattern) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 68 with BSONObject

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

the class BsonGeneratorTest method dates.

/**
 * Test if {@link Date} objects can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void dates() throws Exception {
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    Date date = new Date();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    data.put("date", date);
    data.put("calendar", calendar);
    BSONObject obj = generateAndParse(data);
    assertEquals(date, obj.get("date"));
    assertEquals(date, obj.get("calendar"));
}
Also used : Calendar(java.util.Calendar) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 69 with BSONObject

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

the class BsonGeneratorTest method writeBigDecimalsAsStrings.

/**
 * Test if {@link BigDecimal} objects can be serialized as {@link String}s
 * @throws Exception if something goes wrong
 */
@Test
public void writeBigDecimalsAsStrings() 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(Double.valueOf(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, BsonGenerator.Feature.WRITE_BIGDECIMALS_AS_STRINGS);
    String strResult = (String) obj.get("big");
    assertEquals("0.3", strResult);
}
Also used : BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) SerializableString(com.fasterxml.jackson.core.SerializableString) SerializedString(com.fasterxml.jackson.core.io.SerializedString) BigDecimal(java.math.BigDecimal) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 70 with BSONObject

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

the class BsonGeneratorTest method assertRaw.

private void assertRaw(byte[] r) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(r);
    BSONDecoder decoder = new BasicBSONDecoder();
    BSONObject obj = decoder.readObject(bais);
    byte[] o = (byte[]) obj.get("Test");
    CharBuffer buf = ByteBuffer.wrap(o).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
    assertEquals(2, buf.remaining());
    char a = buf.get();
    char b = buf.get();
    assertEquals('a', a);
    assertEquals('b', b);
}
Also used : BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) BSONObject(org.bson.BSONObject) CharBuffer(java.nio.CharBuffer) BasicBSONDecoder(org.bson.BasicBSONDecoder)

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