Search in sources :

Example 1 with CodeWScope

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

the class BasicBSONEncoder method _putObjectField.

/**
     * Encodes any Object type
     *
     * @param name         the field name
     * @param initialValue the value to write
     */
protected void _putObjectField(final String name, final Object initialValue) {
    if ("_transientFields".equals(name)) {
        return;
    }
    if (name.contains("\0")) {
        throw new IllegalArgumentException("Document field names can't have a NULL character. (Bad Key: '" + name + "')");
    }
    if ("$where".equals(name) && initialValue instanceof String) {
        putCode(name, new Code((String) initialValue));
    }
    Object value = BSON.applyEncodingHooks(initialValue);
    if (value == null) {
        putNull(name);
    } else if (value instanceof Date) {
        putDate(name, (Date) value);
    } else if (value instanceof Number) {
        putNumber(name, (Number) value);
    } else if (value instanceof Decimal128) {
        putDecimal128(name, (Decimal128) value);
    } else if (value instanceof Character) {
        putString(name, value.toString());
    } else if (value instanceof String) {
        putString(name, value.toString());
    } else if (value instanceof ObjectId) {
        putObjectId(name, (ObjectId) value);
    } else if (value instanceof Boolean) {
        putBoolean(name, (Boolean) value);
    } else if (value instanceof Pattern) {
        putPattern(name, (Pattern) value);
    } else if (value instanceof Iterable) {
        putIterable(name, (Iterable) value);
    } else if (value instanceof BSONObject) {
        putObject(name, (BSONObject) value);
    } else if (value instanceof Map) {
        putMap(name, (Map) value);
    } else if (value instanceof byte[]) {
        putBinary(name, (byte[]) value);
    } else if (value instanceof Binary) {
        putBinary(name, (Binary) value);
    } else if (value instanceof UUID) {
        putUUID(name, (UUID) value);
    } else if (value.getClass().isArray()) {
        putArray(name, value);
    } else if (value instanceof Symbol) {
        putSymbol(name, (Symbol) value);
    } else if (value instanceof BSONTimestamp) {
        putTimestamp(name, (BSONTimestamp) value);
    } else if (value instanceof CodeWScope) {
        putCodeWScope(name, (CodeWScope) value);
    } else if (value instanceof Code) {
        putCode(name, (Code) value);
    } else if (value instanceof DBRef) {
        BSONObject temp = new BasicBSONObject();
        DBRef dbRef = (DBRef) value;
        temp.put("$ref", dbRef.getCollectionName());
        temp.put("$id", dbRef.getId());
        if (dbRef.getDatabaseName() != null) {
            temp.put("$db", dbRef.getDatabaseName());
        }
        putObject(name, temp);
    } else if (value instanceof MinKey) {
        putMinKey(name);
    } else if (value instanceof MaxKey) {
        putMaxKey(name);
    } else if (putSpecial(name, value)) {
    // no-op
    } else {
        throw new IllegalArgumentException("Can't serialize " + value.getClass());
    }
}
Also used : Pattern(java.util.regex.Pattern) ObjectId(org.bson.types.ObjectId) Symbol(org.bson.types.Symbol) DBRef(com.mongodb.DBRef) MaxKey(org.bson.types.MaxKey) BSONTimestamp(org.bson.types.BSONTimestamp) Decimal128(org.bson.types.Decimal128) Code(org.bson.types.Code) Date(java.util.Date) CodeWScope(org.bson.types.CodeWScope) MinKey(org.bson.types.MinKey) Binary(org.bson.types.Binary) UUID(java.util.UUID) Map(java.util.Map)

Example 2 with CodeWScope

use of org.bson.types.CodeWScope 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 3 with CodeWScope

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

the class BSONTest method testCode.

@Test
public void testCode() throws IOException {
    BSONObject scope = new BasicBSONObject("x", 1);
    CodeWScope c = new CodeWScope("function() { x += 1; }", scope);
    BSONObject document = new BasicBSONObject("map", c);
    checkEncodingAndDecoding(document, 53, "52918d2367533165bfc617df50335cbb");
}
Also used : CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 4 with CodeWScope

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

the class TestQuery method testWhereWithInvalidStringQuery.

@Test
public void testWhereWithInvalidStringQuery() {
    getDs().save(new PhotoWithKeywords());
    final CodeWScope hasKeyword = new CodeWScope("keywords != null", new BasicDBObject());
    try {
        // must fail
        assertNotNull(getDs().find(PhotoWithKeywords.class).where(hasKeyword.getCode()).get());
        fail("Invalid javascript magically isn't invalid anymore?");
    } catch (MongoInternalException e) {
    // fine
    } catch (MongoException e) {
    // fine
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) CodeWScope(org.bson.types.CodeWScope) MongoInternalException(com.mongodb.MongoInternalException) Test(org.junit.Test)

Example 5 with CodeWScope

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

the class BsonGeneratorTest method javascriptWithScope.

/**
 * Test if {@link JavaScript} objects with a scope can be serialized
 * @throws Exception if something goes wrong
 */
@Test
public void javascriptWithScope() throws Exception {
    Map<String, Object> scope = new LinkedHashMap<String, Object>();
    scope.put("a", 99);
    scope.put("b", 80);
    JavaScript javaScript = new JavaScript("a < 100", scope);
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("javaScript", javaScript);
    BSONObject obj = generateAndParse(data);
    CodeWScope result = (CodeWScope) obj.get("javaScript");
    assertNotNull(result);
    assertEquals(javaScript.getCode(), result.getCode());
    Map<?, ?> returnedScope = result.getScope().toMap();
    assertEquals(returnedScope, scope);
}
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) LinkedHashMap(java.util.LinkedHashMap) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

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