users@jersey.java.net

[Jersey] Re: Deserializing nested objects

From: António Mota <amsmota_at_gmail.com>
Date: Fri, 25 Nov 2011 14:01:40 +0000

I succeed to deserialize a single instance of Image, but using such a
complicated scheme that I think something simpler should be
possible. Basically I extended JacksonJaxbJsonProvider overriden readFrom
to use a CustomObjectMapper thet has a custom implementation
of AbstractTypeResolver that returns
a TypeFactory.fromClass(BoxCoordinates.class)...


But even then, this does work for a Image, but for a List<Image> the
original problem still happens, so I'm back to square zero...

Any help will be most welcome...

Cheers.



**
* Melhores cumprimentos / Beir beannacht / Best regards
**______________________________________________________*
*António Manuel dos Santos Mota
http://gplus.to/amsmota
**http://www.cardcloud.com/amsmota**
**______________________________________________________*
**


2011/11/25 António Mota <amsmota_at_gmail.com>

> Hi, thanks for your response.
>
> I tried to test with another method returning a single instance of my
> Image class instead of a List<Image> and the problem still happens, meaning
> it's not due to type erasure (if I understood you correctly). I think it's
> because the Client side doesn't "know" the BoxCoordinates class when trying
> to deserialize the object, because if I do a simple HTTP Get I got the
> correct JSON:
>
>
> {"id":1006,"imageId":14960,"reference":"1","comments":"1","page":0,"coordinates":{"p0":[548.0,796.0],"p1":[46.0,542.0]}}
>
> So the question is, how do I let Jersey Client be aware of
> the BoxCoordinates class? Is that as simple as somehow "register" that
> class with the Jersey Client or as complicated as writing a
> MessageBodyReader or a ObjectMapper?
>
> My classes are:
>
> @XmlRootElement(name="imageAnnotation")
> @XmlAccessorType(XmlAccessType.NONE)
> public class ImageAnnotation {
> @XmlElement
> private long id;
> @XmlElement
> private int imageId;
> @XmlElement
> private String reference;
> @XmlElement
> private String comments = null;
> @XmlElement
> private int page;
> @XmlJavaTypeAdapter(AnyTypeAdapter.class)
> private BoxCoordinates coordinates;
> (...getters and setters...)
> }
>
> @XmlRootElement(name="boxCoordinates")
> @XmlAccessorType(XmlAccessType.NONE)
> public class BoxCoordinates{
> @XmlElement
> private double[] p0 = {0, 0};
> @XmlElement
> private double[] p1 = {0, 0};
> (...getters and setters...)
> }
>
>
> Thanks in advance.
>
> **
> * Melhores cumprimentos / Beir beannacht / Best regards
> **______________________________________________________*
> *António Manuel dos Santos Mota
> http://gplus.to/amsmota
> **http://www.cardcloud.com/amsmota**
> **______________________________________________________*
> **
>
>
> On 24 November 2011 17:07, Tatu Saloranta <tsaloranta_at_gmail.com> wrote:
>
>> 2011/11/24 António Mota <amsmota_at_gmail.com>:
>> ...
>> > when trying to deserialize I got the following:
>> >
>> > Problem deserializing property 'coordinates' (expected type: [simple
>> type,
>> > class BoxCoordinates]; actual type: java.util.LinkedHashMap), problem:
>> Can
>> > not set BoxCoordinates field Image.coordinates to
>> java.util.LinkedHashMap
>> > (through reference chain: Image["coordinates"])
>>
>> This is usually a clear indication that type information is being lost
>> somewhere due to Java's type erasure.
>>
>> So, instead of trying to bind to, say, BoxCoordinates, declared type
>> is passed as Object; and JSON Object structure is mapped to
>> LinkedHashMap in those cases (it is an Object, and usually most
>> logical match).
>>
>> To know why this happens it would be necessary to see definition of
>> Image class, to know what exactly is happening.
>>
>> -+ Tatu +-
>>
>
>