Search in sources :

Example 1 with BasicBSONObject

use of org.bson.BasicBSONObject in project mongo-java-driver by mongodb.

the class JSONTest method testJSONEncoding.

@Test
public void testJSONEncoding() throws ParseException {
    String json = "{ 'str' : 'asdfasd' , 'long' : 123123123123 , 'int' : 5 , 'float' : 0.4 , 'bool' : false , " + "'date' : { '$date' : '2011-05-18T18:56:00Z'} , 'pat' : { '$regex' : '.*' , '$options' : ''} , " + "'oid' : { '$oid' : '4d83ab3ea39562db9c1ae2ae'} , 'ref' : { '$ref' : 'test.test' , " + "'$id' : { '$oid' : '4d83ab59a39562db9c1ae2af'}} , 'code' : { '$code' : 'asdfdsa'} , " + "'codews' : { '$code' : 'ggggg' , " + "'$scope' : { }} , 'ts' : { '$ts' : 1300474885 , '$inc' : 10} , 'null' :  null, " + "'uuid' : { '$uuid' : '60f65152-6d4a-4f11-9c9b-590b575da7b5' }}";
    BasicDBObject a = (BasicDBObject) JSON.parse(json);
    assertEquals("asdfasd", a.get("str"));
    assertEquals(5, a.get("int"));
    assertEquals(123123123123L, a.get("long"));
    assertEquals(0.4d, a.get("float"));
    assertEquals(false, a.get("bool"));
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
    assertEquals(format.parse("2011-05-18T18:56:00Z"), a.get("date"));
    Pattern pat = (Pattern) a.get("pat");
    Pattern pat2 = Pattern.compile(".*", BSON.regexFlags(""));
    assertEquals(pat.pattern(), pat2.pattern());
    assertEquals(pat.flags(), pat2.flags());
    ObjectId oid = (ObjectId) a.get("oid");
    assertEquals(new ObjectId("4d83ab3ea39562db9c1ae2ae"), oid);
    //        DBRef ref = (DBRef) a.get("ref");
    //        assertEquals(new DBRef(null, "test.test", new ObjectId("4d83ab59a39562db9c1ae2af")), ref);
    assertEquals(new Code("asdfdsa"), a.get("code"));
    assertEquals(new CodeWScope("ggggg", new BasicBSONObject()), a.get("codews"));
    assertEquals(new BSONTimestamp(1300474885, 10), a.get("ts"));
    assertEquals(UUID.fromString("60f65152-6d4a-4f11-9c9b-590b575da7b5"), a.get("uuid"));
    String json2 = JSON.serialize(a);
    BasicDBObject b = (BasicDBObject) JSON.parse(json2);
    assertEquals(JSON.serialize(b), JSON.serialize(b));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) Pattern(java.util.regex.Pattern) BasicBSONObject(org.bson.BasicBSONObject) SimpleTimeZone(java.util.SimpleTimeZone) ObjectId(org.bson.types.ObjectId) GregorianCalendar(java.util.GregorianCalendar) BSONTimestamp(org.bson.types.BSONTimestamp) SimpleDateFormat(java.text.SimpleDateFormat) Code(org.bson.types.Code) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 2 with BasicBSONObject

use of org.bson.BasicBSONObject in project jackrabbit-oak by apache.

the class ReplicaSetInfo method updateReplicaStatus.

void updateReplicaStatus() {
    BasicDBObject result;
    try {
        result = getReplicaStatus();
    } catch (MongoException e) {
        LOG.error("Can't get replica status", e);
        rootRevisions = null;
        secondariesSafeTimestamp = 0;
        return;
    }
    @SuppressWarnings("unchecked") Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
    if (members == null) {
        members = Collections.emptyList();
    }
    updateRevisions(members);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) MongoException(com.mongodb.MongoException)

Example 3 with BasicBSONObject

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

the class BsonParserTest method parseMinKey.

/**
 * Check if a MinKey can be parsed. Refers issue #51
 * @throws Exception if something goes wrong
 */
@Test
public void parseMinKey() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("A", new MinKey());
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(1, data.size());
    assertEquals("MinKey", data.get("A"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) MinKey(org.bson.types.MinKey) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Test(org.junit.Test)

Example 4 with BasicBSONObject

use of org.bson.BasicBSONObject 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 5 with BasicBSONObject

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

the class BsonParserTest method parseBinary.

/**
 * Test if binary objects can be deserialized
 * @throws Exception if something goes wrong
 */
@Test
public void parseBinary() throws Exception {
    byte[] b = new byte[] { 1, 2, 3, 4, 5 };
    BSONObject o = new BasicBSONObject();
    o.put("b1", b);
    o.put("b2", new Binary(BsonConstants.SUBTYPE_BINARY, b));
    o.put("uuid", new UUID(1L, 2L));
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(3, data.size());
    assertArrayEquals(b, (byte[]) data.get("b1"));
    assertArrayEquals(b, (byte[]) data.get("b2"));
    assertEquals(new UUID(1L, 2L), data.get("uuid"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Binary(org.bson.types.Binary) UUID(java.util.UUID) Test(org.junit.Test)

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