I'm trying to deserialize a json response from my jax rs service with genson 1.3 to a list of objects which looks like this:
[{"@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}]
I checked it with jsonlint and it is valid. I'm trying to deserialize it to List<LogicalVolumeImpl>
with the following code
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);
}
and getting this exception:
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
As I understand, this is most probably caused by reduction of "group" object property to just id of the object ( "group" : 2 instead of full unwrapped object as expected by genson).
What can I do? I tried to exclude group property using exclude(String field) in GensonBuilder, but it made effect.
Update
Source of LogicalVolumeImpl and 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;
}
}
Actually your mapping is invalid. For example we can see stuff like group: 4, where 4 is a number and in your class group property is an object of type VolumeGroupImpl.
I assume you generated this json with some library that handles object references during ser/de, but Genson doesn't. At least not for the moment.