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