杰克逊:将对象视为原始对象 [英] Jackson: treat object as primitive

查看:160
本文介绍了杰克逊:将对象视为原始对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它或多或少是一个double的包装类。当我通过jackson序列化我的课程时,我将收到类似:{value:123.0}的内容。我基本上想要发生的是,杰克逊给了我123.0。如果我可以扩展Number,我的问题就会解决,但由于我已经扩展了另一个类,所以这不是一个选项。

I have a class which is more or less a wrapping class around a double. When I serialize my class via jackson I will receive something like: { "value" : 123.0 }. What I basically would like to happen is, that jackson gives me just 123.0. My problem would be solved if I could extend Number, but since I am already extending another class this is not an option.

Class:

@JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("unused")
public class TestValue {
    @JsonProperty
    private final Double d;

    public TestValue(Double d) {
        this.d = d;
    }
}

结果:

{
  "d" : 123.0
}

预期会有什么效果:

public class TestValue extends Number {
    private final Double d;

    public TestValue(Double d) {
        this.d = d;
    }

    public double doubleValue() {
        return d;
    }

    public float floatValue() {
        return d.floatValue();
    }

    public int intValue() {
        return d.intValue();
    }

    public long longValue() {
        return d.longValue();
    }

    public String toString() {
        return d.toString();
    }
}

..导致:123.0

.. which results in: 123.0

但是 - 你知道 - 我已经扩展了另一个抽象类。我怎样才能得到明确的结果?

But - you know - I am already extending an other abstract class. How can I get the expteced result?

推荐答案

既然我确定somene可以重用这个,我会回答我自己的问题(与感谢Gavin向我展示的方式):

Since I am sure somene can reuse this, I will answer my own question (with thanks to Gavin showing me the way):

public class TestValue {
    private final Double d;

    public TestValue(Double d) {
        this.d = d;
    }

    @JsonValue
    public Double getValue() {
        return d;
    }
}

这篇关于杰克逊:将对象视为原始对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆