use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonParserTest method parseCode.
/**
* Test if {@link JavaScript} objects can be deserialized
* @throws Exception if something goes wrong
*/
@Test
public void parseCode() throws Exception {
BSONObject scope = new BasicBSONObject();
scope.put("Int32", 5);
BSONObject o = new BasicBSONObject();
o.put("Code1", new CodeWScope("alert('test');", scope));
o.put("Code2", new Code("alert('Hello');"));
Map<?, ?> data = parseBsonObject(o);
assertEquals(2, data.size());
JavaScript c1 = (JavaScript) data.get("Code1");
JavaScript c2 = (JavaScript) data.get("Code2");
assertEquals("alert('test');", c1.getCode());
assertEquals("alert('Hello');", c2.getCode());
Map<String, Object> c1scope = c1.getScope();
assertEquals(5, c1scope.get("Int32"));
}
use of org.bson.BSONObject in project bson4jackson by michel-kraemer.
the class BsonSerializersTest method generateAndParse.
private static Object generateAndParse(Object data) throws Exception {
Map<String, Object> m = new LinkedHashMap<String, Object>();
m.put("data", data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectMapper om = new ObjectMapper(new BsonFactory());
om.registerModule(new BsonModule());
om.writeValue(baos, m);
byte[] r = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(r);
BSONDecoder decoder = new BasicBSONDecoder();
BSONObject bo = decoder.readObject(bais);
return bo.get("data");
}
use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class BSONWritable method toBSON.
/**
* Unwrap a (usually Writable) Object, getting back a value suitable for
* putting into a BSONObject. If the given object is not Writable, then
* simply return the Object back.
*
* @param x the Object to turn into BSON.
* @return the BSON representation of the Object.
*/
@SuppressWarnings("unchecked")
public static Object toBSON(final Object x) {
if (x == null) {
return null;
}
if (x instanceof Text) {
return x.toString();
}
if (x instanceof BSONWritable) {
return ((BSONWritable) x).getDoc();
}
if (x instanceof Writable) {
if (x instanceof AbstractMapWritable) {
if (!(x instanceof Map)) {
throw new IllegalArgumentException(String.format("Cannot turn %s into BSON, since it does " + "not implement java.util.Map.", x.getClass().getName()));
}
Map<Writable, Writable> map = (Map<Writable, Writable>) x;
BasicBSONObject bson = new BasicBSONObject();
for (Map.Entry<Writable, Writable> entry : map.entrySet()) {
bson.put(entry.getKey().toString(), toBSON(entry.getValue()));
}
return bson;
}
if (x instanceof ArrayWritable) {
Writable[] o = ((ArrayWritable) x).get();
Object[] a = new Object[o.length];
for (int i = 0; i < o.length; i++) {
a[i] = toBSON(o[i]);
}
return a;
}
if (x instanceof NullWritable) {
return null;
}
if (x instanceof BooleanWritable) {
return ((BooleanWritable) x).get();
}
if (x instanceof BytesWritable) {
return ((BytesWritable) x).getBytes();
}
if (x instanceof ByteWritable) {
return ((ByteWritable) x).get();
}
if (x instanceof DoubleWritable) {
return ((DoubleWritable) x).get();
}
if (x instanceof FloatWritable) {
return ((FloatWritable) x).get();
}
if (x instanceof LongWritable) {
return ((LongWritable) x).get();
}
if (x instanceof IntWritable) {
return ((IntWritable) x).get();
}
// TODO - Support counters
}
return x;
}
use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class MongoInputSplit method readFields.
@Override
public void readFields(final DataInput in) throws IOException {
BSONCallback cb = new BasicBSONCallback();
BSONObject spec;
byte[] l = new byte[4];
in.readFully(l);
int dataLen = org.bson.io.Bits.readInt(l);
byte[] data = new byte[dataLen + 4];
System.arraycopy(l, 0, data, 0, 4);
in.readFully(data, 4, dataLen - 4);
_bsonDecoder.decode(data, cb);
spec = (BSONObject) cb.get();
setInputURI(new MongoClientURI((String) spec.get("inputURI")));
if (spec.get("authURI") != null) {
setAuthURI(new MongoClientURI((String) spec.get("authURI")));
} else {
setAuthURI((MongoClientURI) null);
}
setKeyField((String) spec.get("keyField"));
BSONObject temp = (BSONObject) spec.get("fields");
setFields(temp != null ? new BasicDBObject(temp.toMap()) : null);
temp = (BSONObject) spec.get("query");
setQuery(temp != null ? new BasicDBObject(temp.toMap()) : null);
temp = (BSONObject) spec.get("sort");
setSort(temp != null ? new BasicDBObject(temp.toMap()) : null);
temp = (BSONObject) spec.get("min");
setMin(temp != null ? new BasicDBObject(temp.toMap()) : null);
temp = (BSONObject) spec.get("max");
setMax(temp != null ? new BasicDBObject(temp.toMap()) : null);
setLimit((Integer) spec.get("limit"));
setSkip((Integer) spec.get("skip"));
setNoTimeout((Boolean) spec.get("notimeout"));
}
use of org.bson.BSONObject in project mongo-hadoop by mongodb.
the class BSONFileRecordWriter method write.
public void write(final K key, final V value) throws IOException {
final FSDataOutputStream destination = this.outFile;
if (value instanceof MongoUpdateWritable) {
throw new IllegalArgumentException("MongoUpdateWriteable can only be used to output to a mongo collection, " + "not a static BSON file.");
}
Object keyBSON = null;
BSONObject toEncode = null;
byte[] outputByteBuf;
if (key != null) {
keyBSON = BSONWritable.toBSON(key);
if (keyBSON != null) {
toEncode = new BasicDBObject();
}
}
if (value instanceof BSONWritable) {
if (toEncode != null) {
toEncode.putAll(((BSONWritable) value).getDoc());
} else {
toEncode = ((BSONWritable) value).getDoc();
}
} else if (value instanceof BSONObject) {
if (toEncode != null) {
toEncode.putAll((BSONObject) value);
} else {
toEncode = (BSONObject) value;
}
} else {
if (toEncode != null) {
toEncode.put("value", BSONWritable.toBSON(value));
} else {
final DBObject o = new BasicDBObject();
o.put("value", BSONWritable.toBSON(value));
toEncode = o;
}
}
if (keyBSON != null) {
toEncode.put("_id", keyBSON);
}
outputByteBuf = bsonEnc.encode(toEncode);
destination.write(outputByteBuf, 0, outputByteBuf.length);
bytesWritten += outputByteBuf.length;
writeSplitData(outputByteBuf.length, false);
}
Aggregations