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