从JSON反序列化Java枚举 [英] Deserialize java enum from JSON

查看:599
本文介绍了从JSON反序列化Java枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Jackson 1.9.1对与Java对象之间的JSON请求响应字符串进行序列化和反序列化。原始Java类型,集合类型和自定义对象都可以(反)序列化而不会出现问题。但是,尝试将JSON字符串反序列化为Java枚举时遇到问题。
JSON字符串的序列化方式如下:

We use Jackson 1.9.1 to serialize and deserialize JSON request response strings to/from Java objects. Primitive Java types, collection types, and custom objects are (de)serialized without issues. However, I have a problem trying to deserialize JSON string into java enum. JSON string is serialized like so:

"wt":{"wt":100.5,"unit":{"LBS":3}}

wt的Java类型如下:

Java type for wt is like so:

public class Weight {

    protected double weight;
    protected Unit unit;
}

我指的是,以及因此,提出了重量单位的枚举,如下所示:

I referred to this, this, and this on SO and came up with enum for weight units like so:

public enum Unit {

    KG("kg"),
    GM("gm"),
    LBS("lbs"),
    OZ("oz");

    private String value;  
    private WeightMeasurementUnit(String value) { this.value = value; }

    @JsonValue
    public String getValue() { return this.value; }

    @JsonCreator
    public static Unit create(String val) {
        Unit[] units = Unit.values();
        for (Unit unit : units) {
            if (unit.getValue().equals(val)) {
                return unit;
            }
        }
        return LBS;
    }
}

问题是,当我尝试反序列化时提到JSON,我得到这个错误,说:无法识别的字段 LBS(类abcdWeight),未标记为可忽略异常stacktrace如下:

The problem is, when ever I try to deserialize above mentioned JSON I get this error saying: "Unrecognized field "LBS" (Class a.b.c.d.Weight), not marked as ignorable" Exception stacktrace is like so:

Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "LBS" (Class a.b.c.d.Weight), not marked as ignorable
 at [Source: java.io.ByteArrayInputStream@20172017; line: 1, column: 464] (through reference chain: a.b.c.d.MyRequest["blah"]->a.b.c.d.AnotherType["wt"]->a.b.c.d.Weight["LBS"])
    at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
    at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
    at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
    at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)

。 ..

我的问题是:
枚举的序列化JSON字符串是否正确?
为使枚举正确反序列化,我还应该包括(或注释)什么?

My questions are: Is the serialized JSON string for enum seem correct ? What else should I include (or annotate) for the enum to be properly deserialized ?

推荐答案

我假设在枚举单位代码块中,您的意思是 Unit 而不是 WeightMeasurementUnit

I am assuming that in the public enum Unit code block, you mean Unit instead of WeightMeasurementUnit.

体重类的体重只有 unit ,因此,如果您传递 { wt:100.5, unit: lbs} ,它应该起作用,因为单位只是一个没有价值的单位。因此,反序列化程序无法将 { LBS:3} 解析为 Unit 3 的用途是什么?

The Weight class has only a weight and a unit, so if you pass {"wt":100.5,"unit":"lbs"}, it should work, because a unit is just a unit without value. So there is no way for the deserializer to parse {"LBS":3} as a Unit. What is the 3 for?

另一个问题是您的值是 lbs,而您传递的是 LBS 。因此,要么需要标准化,要么需要使用 unit.getValue()。equalsIgnoreCase(val)

Another problem is that your value is "lbs" whereas you are passing "LBS". So either you need to standardise or you need to use unit.getValue().equalsIgnoreCase(val)

这篇关于从JSON反序列化Java枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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