简体   繁体   English

杰克逊自定义数字解析

[英]Jackson Custom Number Parsing

I want to have Jackson always parse numbers as Long or Double. 我希望杰克逊总是将数字解析为长或双。

I have a class like the following with the corresponding getters and setters: 我有一个类如下的类与相应的getter和setter:

public class Foo {
    private HashMap<String, ArrayList<HashMap<String, Object>>> tables;

    ...

}

And some Json that looks like so: 还有一些Json看起来像这样:

{ "tables" : 
    { "table1" : 
        [
            { "t1Field1" : 0,
              "t1Field2" : "val2" 
            },
            { "t1Field1" : 1,
              "t1Field2" : "val4" 
            }
        ]
    }
}

Jackson will parse the values for t1Field1 as Integers/Longs and Floats/Doubles based on the size of the number. Jackson会根据数字的大小将t1Field1的值解析为Integers / Longs和Floats / Doubles。 But I want to always get Longs and Doubles. 但是我想要永远获得Longs和Doubles。

I'm almost certain I have to write a custom deserializer or parser to do this and I have looked through examples but haven't found anything that works how I would imagine. 我几乎可以肯定我必须编写一个自定义反序列化程序或解析器才能执行此操作,我已经查看了示例,但没有找到任何可行的方法。 I just want to extend existing Jackson functionality and override what happens for numbers. 我只是想扩展现有的Jackson功能并覆盖数字的变化。 I don't want to write a whole deserializer for Objects. 我不想为Objects写一个完整的反序列化器。 I just want to do something like: 我只是想做一些事情:

public class CustomerNumberDeserializer extends SomethingFromCoreJackson {
    public Object deserialize() {
        Object num;
        num = super.deserialize();
        if (num instanceof Integer)
            return Long.valueOf(((Integer)num).intValue());
        return num;
    }
}

But all the Jackson classes that I thought to extend were either final or abstract and seemed to require a bunch of extra work. 但是我认为延伸的所有杰克逊课程都是最终的或抽象的,似乎需要一些额外的工作。 Is what I want possible? 我想要的是什么?

After revisiting this I found the class that I wanted to extend. 重新审视之后,我找到了我想扩展的课程。 Hope this helps someone. 希望这有助于某人。

I created a custom deserializer as follows: 我创建了一个自定义反序列化器,如下所示:

/**
 * Custom deserializer for untyped objects that ensures integers are returned as longs
 */
public class ObjectDeserializer extends UntypedObjectDeserializer {

    private static final long serialVersionUID = 7764405880012867708L;

    @Override
    public Object deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException {

        Object out = super.deserialize(jp, ctxt);
        if (out instanceof Integer) {
            return Long.valueOf((Integer)out).longValue();
        }
        return out;
    }

    @Override
    public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt,
            TypeDeserializer typeDeserializer) throws IOException {

        Object out = super.deserializeWithType(jp, ctxt, typeDeserializer);
        if (out instanceof Integer) {
            return Long.valueOf((Integer)out).longValue();
        }
        return out;
    }
}

And configured my object mapper to use it: 并配置我的对象映射器来使用它:

ObjectMapper om = new ObjectMapper();
SimpleModule mod = new SimpleModule().addDeserializer(Object.class, new ObjectDeserializer());
om.registerModule(mod);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM