Search in sources :

Example 1 with BsonGenerator

use of de.undercouch.bson4jackson.BsonGenerator in project immutables by immutables.

the class BsonEncoding method unwrapJsonable.

public static DBObject unwrapJsonable(String json) {
    try {
        JsonParser parser = JSON_FACTORY.createParser(json);
        parser.nextToken();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BsonGenerator generator = BSON_FACTORY.createGenerator(outputStream);
        generator.copyCurrentStructure(parser);
        generator.close();
        parser.close();
        byte[] data = outputStream.toByteArray();
        return (DBObject) new LazyDBCallback(null).createObject(data, 0);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}
Also used : BsonGenerator(de.undercouch.bson4jackson.BsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) LazyDBCallback(com.mongodb.LazyDBCallback) DBObject(com.mongodb.DBObject) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 2 with BsonGenerator

use of de.undercouch.bson4jackson.BsonGenerator in project bson4jackson by michel-kraemer.

the class BsonUuidSerializer method serialize.

@Override
public void serialize(UUID value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (gen instanceof BsonGenerator) {
        BsonGenerator bgen = (BsonGenerator) gen;
        bgen.writeBinary(null, BsonConstants.SUBTYPE_UUID, uuidToLittleEndianBytes(value), 0, 16);
    } else {
        new UUIDSerializer().serialize(value, gen, provider);
    }
}
Also used : UUIDSerializer(com.fasterxml.jackson.databind.ser.std.UUIDSerializer) BsonGenerator(de.undercouch.bson4jackson.BsonGenerator)

Example 3 with BsonGenerator

use of de.undercouch.bson4jackson.BsonGenerator in project immutables by immutables.

the class BsonEncoding method unwrapBsonable.

/**
   * Although it may seem that re-parsing is bizarre, but it is one [of not so many] ways to do
   * proper marshaling. This kind of inefficiency will only hit query constraints that have many
   * object with custom marshaling, which considered to be a rare case.
   * @param adapted adapted value that know how to write itself to {@link JsonWriter}
   * @return object converted to MongoDB driver's {@link BSONObject}.
   */
public static Object unwrapBsonable(Support.Adapted<?> adapted) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        BsonGenerator generator = BSON_FACTORY.createGenerator(outputStream);
        BsonWriter writer = new BsonWriter(generator);
        writer.beginObject().name(PREENCODED_VALUE_WRAPPER_FIELD_NAME);
        adapted.write(writer);
        writer.endObject();
        writer.close();
        BSONObject object = new BasicBSONDecoder().readObject(outputStream.toByteArray());
        return object.get(PREENCODED_VALUE_WRAPPER_FIELD_NAME);
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}
Also used : BSONObject(org.bson.BSONObject) BsonGenerator(de.undercouch.bson4jackson.BsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BasicBSONDecoder(org.bson.BasicBSONDecoder)

Aggregations

BsonGenerator (de.undercouch.bson4jackson.BsonGenerator)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 UUIDSerializer (com.fasterxml.jackson.databind.ser.std.UUIDSerializer)1 DBObject (com.mongodb.DBObject)1 LazyDBCallback (com.mongodb.LazyDBCallback)1 BSONObject (org.bson.BSONObject)1 BasicBSONDecoder (org.bson.BasicBSONDecoder)1