use of org.bson.types.Code 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.Code in project mongo-java-driver by mongodb.
the class CollectionAcceptanceTest method shouldAcceptDocumentsWithAllValidValueTypes.
@Test
public void shouldAcceptDocumentsWithAllValidValueTypes() {
Document doc = new Document();
doc.append("_id", new ObjectId());
doc.append("bool", true);
doc.append("int", 3);
doc.append("long", 5L);
doc.append("str", "Hello MongoDB");
doc.append("double", 1.1);
doc.append("date", new Date());
doc.append("ts", new BsonTimestamp(5, 1));
doc.append("pattern", new BsonRegularExpression("abc"));
doc.append("minKey", new MinKey());
doc.append("maxKey", new MaxKey());
doc.append("js", new Code("code"));
doc.append("jsWithScope", new CodeWithScope("code", new Document()));
doc.append("null", null);
doc.append("binary", new Binary((byte) 42, new byte[] { 10, 11, 12 }));
doc.append("list", Arrays.asList(7, 8, 9));
doc.append("doc list", Arrays.asList(new Document("x", 1), new Document("x", 2)));
collection.insertOne(doc);
Document found = collection.find().first();
assertNotNull(found);
assertEquals(ObjectId.class, found.get("_id").getClass());
assertEquals(Boolean.class, found.get("bool").getClass());
assertEquals(Integer.class, found.get("int").getClass());
assertEquals(Long.class, found.get("long").getClass());
assertEquals(String.class, found.get("str").getClass());
assertEquals(Double.class, found.get("double").getClass());
assertEquals(Date.class, found.get("date").getClass());
assertEquals(BsonTimestamp.class, found.get("ts").getClass());
assertEquals(BsonRegularExpression.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(CodeWithScope.class, found.get("jsWithScope").getClass());
assertNull(found.get("null"));
assertEquals(Binary.class, found.get("binary").getClass());
assertTrue(found.get("list") instanceof List);
assertTrue(found.get("doc list") instanceof List);
}
use of org.bson.types.Code 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.Code in project bson4jackson by michel-kraemer.
the class BsonGeneratorTest method javascript.
/**
* Test if {@link JavaScript} objects can be serialized
* @throws Exception if something goes wrong
*/
@Test
public void javascript() throws Exception {
JavaScript javaScript = new JavaScript("a < 100");
Map<String, Object> data = new LinkedHashMap<String, Object>();
data.put("javaScript", javaScript);
BSONObject obj = generateAndParse(data);
Code result = (Code) obj.get("javaScript");
assertNotNull(result);
assertEquals(javaScript.getCode(), result.getCode());
}
use of org.bson.types.Code in project MonjaDB by Kanatoko.
the class MDocumentEditor method setItemInfo.
// --------------------------------------------------------------------------------
private void setItemInfo(TreeItem treeItem, String key, Object value) {
if (key.equals("_id") && value.getClass() == Double.class) {
BigDecimal bd = new BigDecimal(((Double) value).doubleValue());
value = bd.toString();
}
treeItem.setText(key + " : " + value);
if (value instanceof Integer) {
treeItem.setImage(intImage);
} else if (value instanceof Double) {
treeItem.setImage(doubleImage);
} else if (value instanceof Long) {
treeItem.setImage(longImage);
} else if (value instanceof Date) {
treeItem.setImage(dateImage);
} else if (value instanceof String) {
treeItem.setImage(stringImage);
} else if (value instanceof ObjectId) {
treeItem.setImage(oidImage);
} else if (value instanceof Boolean) {
treeItem.setImage(boolImage);
} else if (value instanceof Code) {
treeItem.setImage(jsImage);
}
}
Aggregations