7

我们需要处理来自遗留服务器的一些损坏的 JSON,这些 JSON 错误地将null值编码为输出中的文字"null"字符串。

我已经发现我可能想将https://github.com/FasterXML/jackson-core/blob/master/src/main/java/com/fasterxml/jackson/core/base/ParserMinimalBase.java#L368覆盖为“修复”这个,但这似乎在杰克逊内心深处,我宁愿以不同的方式做。是否有替代方案,例如通过使用ObjectMapper添加自定义反序列化器String.class或者我迷路了?

4

1 回答 1

2

好的,它通过覆盖标准的字符串反序列化器来工作。不幸的是,我不得不复制完整的实现,因为org/codehaus/jackson/map/deser/std/StringDeserializer.java它是最终的,不能扩展。

public class FixesModule extends SimpleModule {

    public FixesModule() {
        super();
        addDeserializer(String.class, new CustomStringDeserializer());
    }
}

public class CustomStringDeserializer extends StdScalarDeserializer<String> {

    private static final String NULL_STRING = "null";

    public CustomStringDeserializer() {
        super(String.class);
    }

    @Override
    public String deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        JsonToken curr = jp.getCurrentToken();
        // Usually should just get string value:
        if (curr == JsonToken.VALUE_STRING) {
            // BEGIN NULL_STRING fix
            if (NULL_STRING.equals(jp.getText())) {
                return null;
            }
            // END NULL_STRING fix
            return jp.getText();
        }
        // [JACKSON-330]: need to gracefully handle byte[] data, as base64
        if (curr == JsonToken.VALUE_EMBEDDED_OBJECT) {
            Object ob = jp.getEmbeddedObject();
            if (ob == null) {
                return null;
            }
            if (ob instanceof byte[]) {
                return Base64Variants.getDefaultVariant().encode((byte[]) ob, false);
            }
            // otherwise, try conversion using toString()...
            return ob.toString();
        }
        // Can deserialize any scalar value, but not markers
        if (curr.isScalarValue()) {
            return jp.getText();
        }
        throw ctxt.mappingException(_valueClass, curr);
    }

    // 1.6: since we can never have type info ("natural type"; String, Boolean,
    // Integer, Double):
    // (is it an error to even call this version?)
    @Override
    public String deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer typeDeserializer)
            throws IOException, JsonProcessingException {
        return deserialize(jp, ctxt);
    }
}
于 2013-06-11T10:07:09.387 回答