Search in sources :

Example 81 with BSONObject

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"));
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) JavaScript(de.undercouch.bson4jackson.types.JavaScript) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) Code(org.bson.types.Code) CodeWScope(org.bson.types.CodeWScope) Test(org.junit.Test)

Example 82 with BSONObject

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");
}
Also used : BSONObject(org.bson.BSONObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LinkedHashMap(java.util.LinkedHashMap) BsonModule(de.undercouch.bson4jackson.BsonModule) BasicBSONDecoder(org.bson.BasicBSONDecoder) BSONDecoder(org.bson.BSONDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) BSONObject(org.bson.BSONObject) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BasicBSONDecoder(org.bson.BasicBSONDecoder) BsonFactory(de.undercouch.bson4jackson.BsonFactory)

Example 83 with BSONObject

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;
}
Also used : NullWritable(org.apache.hadoop.io.NullWritable) Writable(org.apache.hadoop.io.Writable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.io.ByteWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) IntWritable(org.apache.hadoop.io.IntWritable) AbstractMapWritable(org.apache.hadoop.io.AbstractMapWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) Text(org.apache.hadoop.io.Text) BytesWritable(org.apache.hadoop.io.BytesWritable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) NullWritable(org.apache.hadoop.io.NullWritable) BasicBSONObject(org.bson.BasicBSONObject) FloatWritable(org.apache.hadoop.io.FloatWritable) ArrayWritable(org.apache.hadoop.io.ArrayWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) AbstractMapWritable(org.apache.hadoop.io.AbstractMapWritable) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) LongWritable(org.apache.hadoop.io.LongWritable) Map(java.util.Map) ByteWritable(org.apache.hadoop.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable)

Example 84 with BSONObject

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"));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONCallback(org.bson.BasicBSONCallback) MongoClientURI(com.mongodb.MongoClientURI) BSONObject(org.bson.BSONObject) BSONCallback(org.bson.BSONCallback) BasicBSONCallback(org.bson.BasicBSONCallback)

Example 85 with BSONObject

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);
}
Also used : BSONWritable(com.mongodb.hadoop.io.BSONWritable) BasicDBObject(com.mongodb.BasicDBObject) MongoUpdateWritable(com.mongodb.hadoop.io.MongoUpdateWritable) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Aggregations

BSONObject (org.bson.BSONObject)101 BasicBSONObject (org.bson.BasicBSONObject)49 Test (org.junit.Test)34 BasicDBObject (com.mongodb.BasicDBObject)19 SerializableString (com.fasterxml.jackson.core.SerializableString)14 SerializedString (com.fasterxml.jackson.core.io.SerializedString)14 LinkedHashMap (java.util.LinkedHashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 IOException (java.io.IOException)11 DBObject (com.mongodb.DBObject)10 ArrayList (java.util.ArrayList)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Map (java.util.Map)6 BasicBSONDecoder (org.bson.BasicBSONDecoder)6 BSONDecoder (org.bson.BSONDecoder)5 BSONEncoder (org.bson.BSONEncoder)5 BasicBSONEncoder (org.bson.BasicBSONEncoder)5 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4