我正在尝试使用 genson 1.3 将来自我的 jax rs 服务的 json 响应反序列化为如下所示的对象列表:
[{"@id":1,"id":1,"size":200,"websites":[],"group":{"@id":2,"id":1,"volumes":[{"@id":3,"id":3,"size":100,"websites":[],"group":{"@id":4,"id":1,"volumes":[3,{"@id":5,"id":2,"size":100,"websites":[],"group":4},{"@id":6,"id":4,"size":100,"websites":[],"group":4},{"@id":7,"id":1,"size":200,"websites":[],"group":4}],"name":"ubuntu-dockerhosting-vg"}},5,6,7],"name":"ubuntu-dockerhosting-vg"}},{"@id":8,"id":2,"size":100,"websites":[],"group":2},{"@id":9,"id":3,"size":100,"websites":[],"group":2},{"@id":10,"id":4,"size":100,"websites":[],"group":2}]
我用 jsonlint 检查了它,它是有效的。我正在尝试List<LogicalVolumeImpl>
使用以下代码将其反序列化
com.owlike.genson.GenericType<List<LogicalVolumeImpl>> type =
new com.owlike.genson.GenericType<List<LogicalVolumeImpl>>(){};
List<LogicalVolumeImpl> volumes = null;
Genson genson = new GensonBuilder()
.usePermissiveParsing(true)
.create();
try {
volumes = genson.deserialize(jsonString, type);
} catch (RuntimeException e){
e.printStackTrace();
System.out.println("Unable to parse: " + jsonString);
}
并得到这个例外:
com.owlike.genson.JsonBindingException: Could not deserialize to type interface java.util.List
at com.owlike.genson.Genson.deserialize(Genson.java:391)
at com.owlike.genson.Genson.deserialize(Genson.java:321)
at com.dockerhosting.client.DockerhostingClient$LogicalVolume.list(DockerhostingClient.java:372)
at com.dockerhosting.test.LogicalVolumeTest.hasLvs(LogicalVolumeTest.java:71)
at com.dockerhosting.test.LogicalVolumeTest.main(LogicalVolumeTest.java:33)
Caused by: com.owlike.genson.JsonBindingException: Could not deserialize to property 'group' of class class com.dockerhosting.client.impl.lvm.LogicalVolumeImpl
at com.owlike.genson.reflect.PropertyMutator.couldNotDeserialize(PropertyMutator.java:49)
at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:32)
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:109)
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:92)
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56)
at com.owlike.genson.convert.DefaultConverters$CollectionConverter.deserialize(DefaultConverters.java:175)
at com.owlike.genson.convert.DefaultConverters$SingleValueAsListFactory$1.deserialize(DefaultConverters.java:224)
at com.owlike.genson.convert.DefaultConverters$SingleValueAsListFactory$1.deserialize(DefaultConverters.java:210)
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56)
at com.owlike.genson.Genson.deserialize(Genson.java:389)
... 4 more
Caused by: com.owlike.genson.stream.JsonStreamException: Illegal character at row 0 and column 1 expected { but read '}' !
at com.owlike.genson.stream.JsonReader.newWrongTokenException(JsonReader.java:951)
at com.owlike.genson.stream.JsonReader.begin(JsonReader.java:427)
at com.owlike.genson.stream.JsonReader.beginObject(JsonReader.java:159)
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:103)
at com.owlike.genson.reflect.BeanDescriptor.deserialize(BeanDescriptor.java:92)
at com.owlike.genson.convert.NullConverter$NullConverterWrapper.deserialize(NullConverter.java:56)
at com.owlike.genson.reflect.PropertyMutator.deserialize(PropertyMutator.java:30)
... 12 more
据我了解,这很可能是由于将“group”对象属性减少为对象的 id(“group”:2 而不是 genson 预期的完全展开的对象)。
我能做些什么?我尝试在 GensonBuilder 中使用 exclude(String field) 排除组属性,但它生效了。
更新
LogicalVolumeImpl 和 VolumeGroupImpl 的来源
public class VolumeGroupImpl implements IVolumeGroup, Serializable {
Long id;
String name;
@Override
public Long getId() {
return id;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class LogicalVolumeImpl implements ILogicalVolume, Serializable{
Long id;
@JsonProperty("group")
VolumeGroupImpl group;
Long size;
@Override
public IVolumeGroup getGroup() {
return group;
}
@Override
public Long getSize() {
return size;
}
@Override
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public void setGroup(VolumeGroupImpl group) {
this.group = group;
}
public void setSize(Long size) {
this.size = size;
}
}