Search in sources :

Example 76 with BSONObject

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

the class BsonParserTest method parseEmbeddedArray.

/**
 * Test if embedded arrays can be deserialized
 * @throws Exception if something goes wrong
 */
@Test
public void parseEmbeddedArray() throws Exception {
    List<Integer> i = new ArrayList<Integer>();
    i.add(5);
    i.add(6);
    BSONObject o = new BasicBSONObject();
    o.put("Int32", 5);
    o.put("Arr", i);
    o.put("String", "Hello");
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(3, data.size());
    assertEquals(5, data.get("Int32"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) BasicBSONObject(org.bson.BasicBSONObject) ArrayList(java.util.ArrayList) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Test(org.junit.Test)

Example 77 with BSONObject

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

the class BsonParserTest method parseBigStringInThreads.

/**
 * Tests reading a very large string using multiple threads. Refers
 * issue #19. Does not fail reproducibly, but with very high probability.
 * You may have to run unit tests several times though to really rule out
 * multi-threading issues.
 * @throws Exception if something went wrong
 * @author endasb
 */
@Test
public void parseBigStringInThreads() throws Exception {
    final BSONObject o = new BasicBSONObject();
    final AtomicInteger fails = new AtomicInteger(0);
    StringBuilder bigStr = new StringBuilder();
    for (int i = 0; i < 80000; i++) {
        bigStr.append("abc");
    }
    o.put("String", bigStr.toString());
    ArrayList<Thread> threads = new ArrayList<Thread>();
    for (int i = 0; i < 50; i++) {
        threads.add(new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    Map<?, ?> data = parseBsonObject(o);
                    data = parseBsonObject(o);
                    assertNotNull(data);
                } catch (Exception e) {
                    fail("Threading issue " + fails.incrementAndGet());
                }
            }
        }));
    }
    for (Thread thread : threads) {
        thread.start();
    }
    for (Thread thread : threads) {
        thread.join();
    }
    assertEquals(0, fails.get());
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) ArrayList(java.util.ArrayList) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) IOException(java.io.IOException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) Test(org.junit.Test)

Example 78 with BSONObject

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

the class BsonParserTest method parseObjectId.

/**
 * Check if org.bson.types.ObjectId can be serialized and deserialized as
 * a byte array. See issue #38
 * @throws Exception if something goes wrong
 */
@Test
public void parseObjectId() throws Exception {
    class ObjectIdDeserializer extends StdDeserializer<org.bson.types.ObjectId> {

        private static final long serialVersionUID = 6934309887169924897L;

        protected ObjectIdDeserializer() {
            super(org.bson.types.ObjectId.class);
        }

        @Override
        public org.bson.types.ObjectId deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonGenerationException {
            return new org.bson.types.ObjectId(jp.getBinaryValue());
        }
    }
    org.bson.types.ObjectId oid = new org.bson.types.ObjectId();
    BSONObject o = new BasicBSONObject();
    o.put("oid", oid.toByteArray());
    SimpleModule mod = new SimpleModule();
    mod.addDeserializer(org.bson.types.ObjectId.class, new ObjectIdDeserializer());
    ObjectIdClass res = parseBsonObject(o, ObjectIdClass.class, mod);
    assertEquals(oid, res.oid);
}
Also used : StdDeserializer(com.fasterxml.jackson.databind.deser.std.StdDeserializer) ObjectId(de.undercouch.bson4jackson.types.ObjectId) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) DeserializationContext(com.fasterxml.jackson.databind.DeserializationContext) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JsonParser(com.fasterxml.jackson.core.JsonParser) Test(org.junit.Test)

Example 79 with BSONObject

use of org.bson.BSONObject 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)

Example 80 with BSONObject

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

the class BsonParserTest method parseComplex.

/**
 * Test if a complex BSON object containing various values can be
 * deserialized
 * @throws Exception if something goes wrong
 */
@Test
public void parseComplex() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("Timestamp", new BSONTimestamp(0xAABB, 0xCCDD));
    o.put("Symbol", new Symbol("Test"));
    o.put("ObjectId", org.bson.types.ObjectId.createFromLegacyFormat(Integer.MAX_VALUE, -2, Integer.MIN_VALUE));
    Pattern p = Pattern.compile(".*", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE);
    o.put("Regex", p);
    Map<?, ?> data = parseBsonObject(o);
    assertEquals(new Timestamp(0xAABB, 0xCCDD), data.get("Timestamp"));
    assertEquals(new de.undercouch.bson4jackson.types.Symbol("Test"), data.get("Symbol"));
    ObjectId oid = (ObjectId) data.get("ObjectId");
    assertEquals(Integer.MAX_VALUE, oid.getTime());
    assertEquals(-2, oid.getMachine());
    assertEquals(Integer.MIN_VALUE, oid.getInc());
    Pattern p2 = (Pattern) data.get("Regex");
    assertEquals(p.flags(), p2.flags());
    assertEquals(p.pattern(), p2.pattern());
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) Pattern(java.util.regex.Pattern) ObjectId(de.undercouch.bson4jackson.types.ObjectId) Symbol(org.bson.types.Symbol) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BSONTimestamp(org.bson.types.BSONTimestamp) Timestamp(de.undercouch.bson4jackson.types.Timestamp) BSONTimestamp(org.bson.types.BSONTimestamp) 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