Java 在jackson中反序列化bson长基元json

Java 在jackson中反序列化bson长基元json,java,mongodb,jackson,jackson2,Java,Mongodb,Jackson,Jackson2,我使用MongoDB作为数据存储,但我们希望使用Jackson进行序列化/反序列化(例如,MongoPojo类处理的场景不如Jackson-builders那么多) 我们使用自定义编解码器提供程序实现了这一点-以下是编解码器本身: class JacksonCodec<T> implements Codec<T> { private final ObjectMapper objectMapper; private final Codec<RawBs

我使用MongoDB作为数据存储,但我们希望使用Jackson进行序列化/反序列化(例如,MongoPojo类处理的场景不如Jackson-builders那么多)

我们使用自定义编解码器提供程序实现了这一点-以下是编解码器本身:

class JacksonCodec<T> implements Codec<T> {


   private final ObjectMapper objectMapper;
    private final Codec<RawBsonDocument> rawBsonDocumentCodec;
    private final Class<T> type;

    public JacksonCodec(ObjectMapper objectMapper,
                        CodecRegistry codecRegistry,
                        Class<T> type) {
        this.objectMapper = objectMapper;
        this.rawBsonDocumentCodec = codecRegistry.get(RawBsonDocument.class);
        this.type = type;
    }

    @Override
    public T decode(BsonReader reader, DecoderContext decoderContext) {
        try {

            RawBsonDocument document = rawBsonDocumentCodec.decode(reader, decoderContext);
            String json = document.toJson();
            return objectMapper.readValue(json, type);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public void encode(BsonWriter writer, Object value, EncoderContext encoderContext) {
        try {

            String json = objectMapper.writeValueAsString(value);

            rawBsonDocumentCodec.encode(writer, RawBsonDocument.parse(json), encoderContext);

        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public Class<T> getEncoderClass() {
        return this.type;
    }
}
但是BsonLongDeserializer从来没有被Jackson调用过(原语的处理方式是否不同,是否会使注册的反序列化程序短路?)

杰克逊版本2.9.3。MongoDB驱动程序版本3.6

如果有人对攻击角度有任何建议,我将不胜感激


引用了一些似乎没有帮助的文章:

我通过创建一个JsonWriterSettings对象来抑制奇怪的json反序列化,从而修复了Mongo方面的问题。这来自这里:

编解码器现在如下所示:

class JacksonCodec<T> implements Codec<T> {
    private final ObjectMapper objectMapper;
    private final Codec<BsonDocument> rawBsonDocumentCodec;
    private final Class<T> type;

    public JacksonCodec(ObjectMapper objectMapper,
                        CodecRegistry codecRegistry,
                        Class<T> type) {
        this.objectMapper = objectMapper;
        this.rawBsonDocumentCodec = codecRegistry.get(BsonDocument.class);
        this.type = type;
    }

    @Override
    public T decode(BsonReader reader, DecoderContext decoderContext) {
        try {
            //https://stackoverflow.com/questions/35209839/converting-document-objects-in-mongodb-3-to-pojos
            JsonWriterSettings settings = JsonWriterSettings.builder().int64Converter((value, writer) -> writer.writeNumber(value.toString())).build();

            BsonDocument document = rawBsonDocumentCodec.decode(reader, decoderContext);
            String json = document.toJson(settings);
            return objectMapper.readValue(json, type);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public void encode(BsonWriter writer, Object value, EncoderContext encoderContext) {
        try {

            String json = objectMapper.writeValueAsString(value);

            rawBsonDocumentCodec.encode(writer, RawBsonDocument.parse(json), encoderContext);

        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public Class<T> getEncoderClass() {
        return this.type;
    }
}
类JacksonCodec实现编解码器{ 私有最终ObjectMapper ObjectMapper; 专用最终编解码器RawBSonDocument编解码器; 私有最终类类型; 公共JacksonCodec(对象映射器对象映射器, CodecRegistry CodecRegistry, 班级类型){ this.objectMapper=objectMapper; this.rawBsonDocumentCodec=codecRegistry.get(BsonDocument.class); this.type=type; } @凌驾 公共T解码(BsonReader阅读器,DecoderContext DecoderContext){ 试一试{ //https://stackoverflow.com/questions/35209839/converting-document-objects-in-mongodb-3-to-pojos JsonWriterSettings=JsonWriterSettings.builder().int64Converter((值,writer)->writer.WriteEnumber(值.toString()).build(); BsonDocument document=rawBsonDocumentCodec.decode(读卡器,decoderContext); 字符串json=document.toJson(设置); 返回objectMapper.readValue(json,类型); }捕获(IOE异常){ 抛出新的未选中异常(e); } } @凌驾 public void encode(BsonWriter编写器、对象值、EncoderContext EncoderContext){ 试一试{ 字符串json=objectMapper.writeValueAsString(值); rawBsonDocumentCodec.encode(writer、RawBsonDocument.parse(json)、encoderContext); }捕获(IOE异常){ 抛出新的未选中异常(e); } } @凌驾 公共类getEncoderClass(){ 返回此.type; } }
我通过创建一个JsonWriterSettings对象来抑制奇怪的json反序列化,从而修复了Mongo方面的问题,从而使其正常工作。这来自这里:

编解码器现在如下所示:

class JacksonCodec<T> implements Codec<T> {
    private final ObjectMapper objectMapper;
    private final Codec<BsonDocument> rawBsonDocumentCodec;
    private final Class<T> type;

    public JacksonCodec(ObjectMapper objectMapper,
                        CodecRegistry codecRegistry,
                        Class<T> type) {
        this.objectMapper = objectMapper;
        this.rawBsonDocumentCodec = codecRegistry.get(BsonDocument.class);
        this.type = type;
    }

    @Override
    public T decode(BsonReader reader, DecoderContext decoderContext) {
        try {
            //https://stackoverflow.com/questions/35209839/converting-document-objects-in-mongodb-3-to-pojos
            JsonWriterSettings settings = JsonWriterSettings.builder().int64Converter((value, writer) -> writer.writeNumber(value.toString())).build();

            BsonDocument document = rawBsonDocumentCodec.decode(reader, decoderContext);
            String json = document.toJson(settings);
            return objectMapper.readValue(json, type);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public void encode(BsonWriter writer, Object value, EncoderContext encoderContext) {
        try {

            String json = objectMapper.writeValueAsString(value);

            rawBsonDocumentCodec.encode(writer, RawBsonDocument.parse(json), encoderContext);

        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public Class<T> getEncoderClass() {
        return this.type;
    }
}
类JacksonCodec实现编解码器{ 私有最终ObjectMapper ObjectMapper; 专用最终编解码器RawBSonDocument编解码器; 私有最终类类型; 公共JacksonCodec(对象映射器对象映射器, CodecRegistry CodecRegistry, 班级类型){ this.objectMapper=objectMapper; this.rawBsonDocumentCodec=codecRegistry.get(BsonDocument.class); this.type=type; } @凌驾 公共T解码(BsonReader阅读器,DecoderContext DecoderContext){ 试一试{ //https://stackoverflow.com/questions/35209839/converting-document-objects-in-mongodb-3-to-pojos JsonWriterSettings=JsonWriterSettings.builder().int64Converter((值,writer)->writer.WriteEnumber(值.toString()).build(); BsonDocument document=rawBsonDocumentCodec.decode(读卡器,decoderContext); 字符串json=document.toJson(设置); 返回objectMapper.readValue(json,类型); }捕获(IOE异常){ 抛出新的未选中异常(e); } } @凌驾 public void encode(BsonWriter编写器、对象值、EncoderContext EncoderContext){ 试一试{ 字符串json=objectMapper.writeValueAsString(值); rawBsonDocumentCodec.encode(writer、RawBsonDocument.parse(json)、encoderContext); }捕获(IOE异常){ 抛出新的未选中异常(e); } } @凌驾 公共类getEncoderClass(){ 返回此.type; } }
只是好奇,你似乎在使用Jackson的ObjectMapper,ObjectMapper ObjectMapper=new ObjectMapper(new BsonFactory())不是已经成功了吗?已经好几年了,所以我的记忆很模糊。当时的问题(现在可能很好地解决了)是BsonFactory在mongo编解码器的概念上并没有发挥好作用。在这一点上,我真的不记得为什么了,但我知道我花了大量的时间试图让这个工作。最后,我不得不使用的方法真的没有效率(很多字符串转换)。我仍然不明白为什么Mongo觉得有必要推出自己的json工厂……Kevin Day——对不起。我在发表评论后查看了日期。我知道那种为一些看似微不足道的事情投入疯狂时间的感觉。我一直在尝试将bson转换为json字符串。我在读了这篇文章后就开始工作了。只是好奇,你似乎在使用Jackson的ObjectMapper,ObjectMapper ObjectMapper=new ObjectMapper(new BsonFactory())不是已经成功了吗?已经好几年了,所以我在这里的记忆相当模糊。当时的问题(现在可能很好地解决了)是BsonFactory在mongo编解码器的概念上并没有发挥好作用。在这一点上,我真的不记得为什么了,但我知道我花了大量的时间试图让这个工作。最后,我不得不使用的方法真的没有效率(很多字符串转换)。我仍然不明白为什么Mongo觉得有必要推出自己的json工厂……Kevin Day——对不起。我在发表评论后查看了日期。我知道那种为一些看似微不足道的事情投入疯狂时间的感觉。我
class JacksonCodec<T> implements Codec<T> {
    private final ObjectMapper objectMapper;
    private final Codec<BsonDocument> rawBsonDocumentCodec;
    private final Class<T> type;

    public JacksonCodec(ObjectMapper objectMapper,
                        CodecRegistry codecRegistry,
                        Class<T> type) {
        this.objectMapper = objectMapper;
        this.rawBsonDocumentCodec = codecRegistry.get(BsonDocument.class);
        this.type = type;
    }

    @Override
    public T decode(BsonReader reader, DecoderContext decoderContext) {
        try {
            //https://stackoverflow.com/questions/35209839/converting-document-objects-in-mongodb-3-to-pojos
            JsonWriterSettings settings = JsonWriterSettings.builder().int64Converter((value, writer) -> writer.writeNumber(value.toString())).build();

            BsonDocument document = rawBsonDocumentCodec.decode(reader, decoderContext);
            String json = document.toJson(settings);
            return objectMapper.readValue(json, type);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public void encode(BsonWriter writer, Object value, EncoderContext encoderContext) {
        try {

            String json = objectMapper.writeValueAsString(value);

            rawBsonDocumentCodec.encode(writer, RawBsonDocument.parse(json), encoderContext);

        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    @Override
    public Class<T> getEncoderClass() {
        return this.type;
    }
}