Search in sources :

Example 6 with CodeWScope

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

the class BsonSerializersTest method javascriptWithScope.

/**
 * Tests {@link BsonJavaScriptSerializer}
 * @throws Exception if something goes wrong
 */
@Test
public void javascriptWithScope() throws Exception {
    Map<String, Object> scope = new HashMap<>();
    scope.put("j", 5);
    JavaScript js = new JavaScript("code", scope);
    CodeWScope code = (CodeWScope) generateAndParse(js);
    assertEquals(js.getCode(), code.getCode());
    assertEquals(js.getScope(), code.getScope());
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JavaScript(de.undercouch.bson4jackson.types.JavaScript) BSONObject(org.bson.BSONObject) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 7 with CodeWScope

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

the class DBObjectCodec method writeValue.

@SuppressWarnings("unchecked")
private void writeValue(final BsonWriter bsonWriter, final EncoderContext encoderContext, @Nullable final Object value) {
    if (value == null) {
        bsonWriter.writeNull();
    } else if (value instanceof DBRef) {
        encodeDBRef(bsonWriter, (DBRef) value, encoderContext);
    } else if (value instanceof Map) {
        encodeMap(bsonWriter, (Map<String, Object>) value, encoderContext);
    } else if (value instanceof Iterable) {
        encodeIterable(bsonWriter, (Iterable) value, encoderContext);
    } else if (value instanceof BSONObject) {
        encodeBsonObject(bsonWriter, (BSONObject) value, encoderContext);
    } else if (value instanceof CodeWScope) {
        encodeCodeWScope(bsonWriter, (CodeWScope) value, encoderContext);
    } else if (value instanceof byte[]) {
        encodeByteArray(bsonWriter, (byte[]) value);
    } else if (value.getClass().isArray()) {
        encodeArray(bsonWriter, value, encoderContext);
    } else if (value instanceof Symbol) {
        bsonWriter.writeSymbol(((Symbol) value).getSymbol());
    } else {
        Codec codec = codecRegistry.get(value.getClass());
        encoderContext.encodeWithChildContext(codec, bsonWriter, value);
    }
}
Also used : OverridableUuidRepresentationCodec(org.bson.codecs.OverridableUuidRepresentationCodec) Codec(org.bson.codecs.Codec) CollectibleCodec(org.bson.codecs.CollectibleCodec) Symbol(org.bson.types.Symbol) BSONObject(org.bson.BSONObject) BSONObject(org.bson.BSONObject) HashMap(java.util.HashMap) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) Map(java.util.Map) BsonTypeCodecMap(org.bson.codecs.BsonTypeCodecMap) CodeWScope(org.bson.types.CodeWScope)

Example 8 with CodeWScope

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

the class DBCollectionTest method shouldAcceptDocumentsWithAllValidValueTypes.

@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
    BasicDBObject doc = new BasicDBObject();
    doc.append("_id", new ObjectId());
    doc.append("bool", true);
    doc.append("int", 3);
    doc.append("short", (short) 4);
    doc.append("long", 5L);
    doc.append("str", "Hello MongoDB");
    doc.append("float", 6.0f);
    doc.append("double", 1.1);
    doc.append("date", new Date());
    doc.append("ts", new BSONTimestamp(5, 1));
    doc.append("pattern", Pattern.compile(".*"));
    doc.append("minKey", new MinKey());
    doc.append("maxKey", new MaxKey());
    doc.append("js", new Code("code"));
    doc.append("jsWithScope", new CodeWScope("code", new BasicDBObject()));
    doc.append("null", null);
    doc.append("uuid", UUID.randomUUID());
    doc.append("db ref", new com.mongodb.DBRef("test", new ObjectId()));
    doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
    doc.append("byte array", new byte[] { 1, 2, 3 });
    doc.append("int array", new int[] { 4, 5, 6 });
    doc.append("list", asList(7, 8, 9));
    doc.append("doc list", asList(new Document("x", 1), new Document("x", 2)));
    collection.insert(doc);
    DBObject found = collection.findOne();
    assertNotNull(found);
    assertEquals(ObjectId.class, found.get("_id").getClass());
    assertEquals(Boolean.class, found.get("bool").getClass());
    assertEquals(Integer.class, found.get("int").getClass());
    assertEquals(Integer.class, found.get("short").getClass());
    assertEquals(Long.class, found.get("long").getClass());
    assertEquals(String.class, found.get("str").getClass());
    assertEquals(Double.class, found.get("float").getClass());
    assertEquals(Double.class, found.get("double").getClass());
    assertEquals(Date.class, found.get("date").getClass());
    assertEquals(BSONTimestamp.class, found.get("ts").getClass());
    assertEquals(Pattern.class, found.get("pattern").getClass());
    assertEquals(MinKey.class, found.get("minKey").getClass());
    assertEquals(MaxKey.class, found.get("maxKey").getClass());
    assertEquals(Code.class, found.get("js").getClass());
    assertEquals(CodeWScope.class, found.get("jsWithScope").getClass());
    assertNull(found.get("null"));
    assertEquals(UUID.class, found.get("uuid").getClass());
    assertEquals(DBRef.class, found.get("db ref").getClass());
    assertEquals(Binary.class, found.get("binary").getClass());
    assertEquals(byte[].class, found.get("byte array").getClass());
    assertTrue(found.get("int array") instanceof List);
    assertTrue(found.get("list") instanceof List);
    assertTrue(found.get("doc list") instanceof List);
}
Also used : BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Binary(org.bson.types.Binary) Test(org.junit.Test)

Example 9 with CodeWScope

use of org.bson.types.CodeWScope in project morphia by mongodb.

the class TestQuery method testWhereCodeWScopeQuery.

@Test
public void testWhereCodeWScopeQuery() {
    getDs().save(new PhotoWithKeywords(new Keyword("california"), new Keyword("nevada"), new Keyword("arizona")));
    //        CodeWScope hasKeyword = new CodeWScope("for (kw in this.keywords) { if(kw.keyword == kwd) return true; } return false;
    // ", new BasicDBObject("kwd","california"));
    final CodeWScope hasKeyword = new CodeWScope("this.keywords != null", new BasicDBObject());
    assertNotNull(getDs().find(PhotoWithKeywords.class).where(hasKeyword).get());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 10 with CodeWScope

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

the class JSONCallback method objectDone.

@Override
public Object objectDone() {
    String name = curName();
    Object o = super.objectDone();
    if (_lastArray) {
        return o;
    }
    BSONObject b = (BSONObject) o;
    // override the object if it's a special type
    if (b.containsField("$oid")) {
        o = new ObjectId((String) b.get("$oid"));
    } else if (b.containsField("$date")) {
        if (b.get("$date") instanceof Number) {
            o = new Date(((Number) b.get("$date")).longValue());
        } else {
            SimpleDateFormat format = new SimpleDateFormat(_msDateFormat);
            format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
            o = format.parse(b.get("$date").toString(), new ParsePosition(0));
            if (o == null) {
                // try older format with no ms
                format = new SimpleDateFormat(_secDateFormat);
                format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
                o = format.parse(b.get("$date").toString(), new ParsePosition(0));
            }
        }
    } else if (b.containsField("$regex")) {
        o = Pattern.compile((String) b.get("$regex"), BSON.regexFlags((String) b.get("$options")));
    } else if (b.containsField("$ts")) {
        //Legacy timestamp format
        Integer ts = ((Number) b.get("$ts")).intValue();
        Integer inc = ((Number) b.get("$inc")).intValue();
        o = new BSONTimestamp(ts, inc);
    } else if (b.containsField("$timestamp")) {
        BSONObject tsObject = (BSONObject) b.get("$timestamp");
        Integer ts = ((Number) tsObject.get("t")).intValue();
        Integer inc = ((Number) tsObject.get("i")).intValue();
        o = new BSONTimestamp(ts, inc);
    } else if (b.containsField("$code")) {
        if (b.containsField("$scope")) {
            o = new CodeWScope((String) b.get("$code"), (DBObject) b.get("$scope"));
        } else {
            o = new Code((String) b.get("$code"));
        }
    } else if (b.containsField("$ref")) {
        o = new DBRef((String) b.get("$ref"), b.get("$id"));
    } else if (b.containsField("$minKey")) {
        o = new MinKey();
    } else if (b.containsField("$maxKey")) {
        o = new MaxKey();
    } else if (b.containsField("$uuid")) {
        o = UUID.fromString((String) b.get("$uuid"));
    } else if (b.containsField("$binary")) {
        int type = (b.get("$type") instanceof String) ? Integer.valueOf((String) b.get("$type"), 16) : (Integer) b.get("$type");
        byte[] bytes = DatatypeConverter.parseBase64Binary((String) b.get("$binary"));
        o = new Binary((byte) type, bytes);
    } else if (b.containsField("$undefined") && b.get("$undefined").equals(true)) {
        o = new BsonUndefined();
    } else if (b.containsField("$numberLong")) {
        o = Long.valueOf((String) b.get("$numberLong"));
    } else if (b.containsField("$numberDecimal")) {
        o = Decimal128.parse((String) b.get("$numberDecimal"));
    }
    if (!isStackEmpty()) {
        _put(name, o);
    } else {
        o = !BSON.hasDecodeHooks() ? o : BSON.applyDecodingHooks(o);
        setRoot(o);
    }
    return o;
}
Also used : ObjectId(org.bson.types.ObjectId) BSONObject(org.bson.BSONObject) GregorianCalendar(java.util.GregorianCalendar) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) SimpleTimeZone(java.util.SimpleTimeZone) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject) Binary(org.bson.types.Binary) SimpleDateFormat(java.text.SimpleDateFormat) BsonUndefined(org.bson.BsonUndefined) ParsePosition(java.text.ParsePosition)

Aggregations

CodeWScope (org.bson.types.CodeWScope)15 Test (org.junit.Test)11 Code (org.bson.types.Code)8 BasicDBObject (com.mongodb.BasicDBObject)6 BSONTimestamp (org.bson.types.BSONTimestamp)6 Date (java.util.Date)5 BSONObject (org.bson.BSONObject)5 Binary (org.bson.types.Binary)5 MaxKey (org.bson.types.MaxKey)5 MinKey (org.bson.types.MinKey)5 ObjectId (org.bson.types.ObjectId)5 DBRef (com.mongodb.DBRef)3 JavaScript (de.undercouch.bson4jackson.types.JavaScript)3 SimpleDateFormat (java.text.SimpleDateFormat)3 GregorianCalendar (java.util.GregorianCalendar)3 SimpleTimeZone (java.util.SimpleTimeZone)3 BasicBSONObject (org.bson.BasicBSONObject)3 Symbol (org.bson.types.Symbol)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2