I have in my mongo some documents which have doubles as one of their fields:
{
"_id" : "1b1a39a7-5a04-4a0c-9a9e-658b5c95b58d",
"content" :
{
"NumTaux" : 0.00196713814946994,
"SourceYear" : NumberInt(2017),
"Year" : NumberInt(2020),
"Age" : NumberInt(-1),
"Sex" : "F"
}
}
I use the mongo java driver to write to and read from our Mongo. When I am reading these documents, the doubles are sometimes converted to scientific notation:
I tried solving this by writing a custom codec for Doubles:
public class DoubleCodec implements Codec<Double> {
@Override
public Double decode(BsonReader reader, DecoderContext decoderContext) {
return reader.readDouble();
}
@Override
public void encode(BsonWriter writer, Double value, EncoderContext encoderContext) {
writer.writeDouble(value);
}
@Override
public Class<Double> getEncoderClass() {
return Double.class;
}
}
and adding it to my collection's registry:
DoubleCodec doubleCodec = new DoubleCodec();
CodecRegistry registry = CodecRegistries.fromRegistries(MongoClient.getDefaultCodecRegistry(),
CodecRegistries.fromCodecs(doubleCodec));
matrixDataCollection = MongoFactory.obtainSyncClient(config)
.getDatabase(config.getString(MONGO_DB))
.getCollection(MATRIX_DATA_COLLECTION)
.withCodecRegistry(registry);
But this doesn't seem to work, the decode method in the codec is never called as far as I can see (breakpoint/system out) My suspicion is that the already existing codec for Double is being called instead.
Is there any way to make it so that my own DoubleCodec gets called instead of the default one, or is there another way to configure how the default Double codec behaves?