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());
}
}
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));
}
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");
}
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
}
}
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);
}
Aggregations