java 序列化 枚举,从JSON反序列化Java枚举

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}}

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;

}

}

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)

...

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 ?

解决方案

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

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?

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值