users@jersey.java.net

[Jersey] Re: _at_XmlEnumValue is not recognized by Jackson

From: Pengfei Di <pengfei.di_at_match2blue.com>
Date: Fri, 8 Apr 2011 16:08:28 +0200

Hello Jakub,

thanks a lot for your nice advice. I think I will follow it.

But I still have a question. When I turn POJO mapping on, the Jackson
provider can use some JAXB annotations, like @XmlElement, @XmlTransient,
however, it cannot recognize some other JAXB annotations, like
@XmlEnumValue, @XmlRootElement. (as stated in
http://wiki.fasterxml.com/JacksonJAXBAnnotations, these two should also
be supported.)
(Of course, I can use Jackson annotation instead, but just wondering why
some work, some not.)

Pengfei




On 04/08/2011 03:47 PM, Jakub Podlesak wrote:
> Hello Pengfei,
>
> This is just a general advice, that Jackson should provide
> better results in terms of the output JSON format.
> You do not need to deal with the single element array thing
> and non-strings etc, as the mapping goes directly
> from the Java object model to JSON and vice versa.
> The configuration should be much easier in the most cases.
>
> You are also not constrained to JAXB beans only, as Jacskon
> is capable of (de)serializing any Java object for you.
>
> Anway, there might be (quite rare) cases, where the JAXB->JSON mapping
> could work better for you (e.g. you want to rely on certain JAXB features,
> let say XmlAdapter...).
>
> The general advice from me would be: try the Jackson providers first,
> if it works then fine, if it does not: ask Tatu/this mailing list on how
> to customize
> the processing, and only if there is a real blocker, try to figure out
> if the JAXB->JSON mapping would work better.
>
> HTH,
>
> ~Jakub
>
> On 04/08/2011 02:55 PM, Pengfei Di wrote:
>> Hello Jakub,
>>
>> you are totally right, I had tried to switch the POJO mapping back,
>> and add a custom Jackson serialization implementation for the enum
>> class. It does work and gives the result as expected.
>> However, If I turn the POJO mapping off, the @XmlEnumValue can be
>> recognized, and I can save the implementation of two classes (a
>> Serializer and a Deserializer). Isn't it a better way?
>> Just wondering, what is the disadvantage to turn the POJO mapping off.
>>
>> Pengfei
>>
>>
>>
>>
>> On 04/08/2011 01:55 PM, Jakub Podlesak wrote:
>>> Hi Pengfei,
>>>
>>> I am a bit confused. You stated you turned the POJO mapping feature off.
>>> Then Jackson JAX-RS provider won't be used and you can not expect
>>> anything
>>> from it as the internal JAXB->JSON mapping is not leveraging the Jackson
>>> features
>>> described by Tatu. I think, and Tatu could confirm, if you switched the
>>> POJO mapping
>>> back on, and provided your own custom (Jackson) serialization
>>> implementation
>>> for the class, that should work for you as requested.
>>>
>>> ~Jakub
>>>
>>> On 04/08/2011 01:05 PM, Pengfei Di wrote:
>>>> Hello Tatu,
>>>>
>>>> thank you for your hint. I just found the reason.
>>>> I have to turn of the POJOMapping feature, otherwise the
>>>> JacksonJsonProvider instead of the AbstractRootElementProvider will be
>>>> used for JSON representation.
>>>>
>>>> Now what I get ist: {"myEnum": "2"}
>>>> As you said, @XmlEnum is unfortunately not support.
>>>> What is confusing is that there is no statement from the jackson web
>>>> site, whether this annotation will be supported in the future or will
>>>> not.
>>>>
>>>> Pengfei
>>>>
>>>> On 04/06/2011 06:38 PM, Tatu Saloranta wrote:
>>>>> On Wed, Apr 6, 2011 at 7:00 AM, Pengfei
>>>>> Di<pengfei.di_at_match2blue.com> wrote:
>>>>>> Hello,
>>>>>>
>>>>>> I am trying to write a JAXB class, that constains a field of enum
>>>>>> type, into
>>>>>> JSON String. I tried with @XmlEnumValue annotation, but it doesn't
>>>>>> take
>>>>>> effect.
>>>>>> The JAXB class looks like:
>>>>>>
>>>>>> @XmlRootElement
>>>>>> public class MyClass {
>>>>>>
>>>>>> @XmlEnum(Integer.class)
>>>>>> public enum MyEnum {
>>>>>> @XmlEnumValue("1") INACTIVE,
>>>>>> @XmlEnumValue("2") ACTIVE
>>>>>> }
>>>>>>
>>>>>> private MyEnum myEnum;
>>>>>>
>>>>>> public getMyEnum(){
>>>>>> return myEnum;
>>>>>> }
>>>>>> // setter
>>>>>> ...
>>>>>> }
>>>>>>
>>>>>> The expected JSON string is : {"myEnum":2}
>>>>>> But what I get is : {"myEnum":"ACTIVE"}
>>>>>>
>>>>>> Does Jackson support this @XmlEnum annotation? or did I do something
>>>>>> wrong?
>>>>> Jackson does not support @XmlEnum -- enums will always be serialized
>>>>> as Strings, unless custom serializer is used -- but
>>>>> JaxbAnnotationIntrospector should support @XmlEnumValue for
>>>>> customizing actual values used. So this looks like a bug, as long as
>>>>> you have JAXB annotation support enabled (if not, you do need to
>>>>> enable support).
>>>>>
>>>>> -+ Tatu +-
>>


-- 
Pengfei Di
Technology
match2blue software development GmbH
Leutragraben 1
07743 Jena
Tel: +49 3641 816 8092
Mobil: +49 1520 166 8691
Fax: +49 3641 573 3479
Email: pengfei.di_at_match2blue.com
Web  : www.match2blue.com
Blog : http://blog.match2blue.com
Registergericht: Amtsgericht Jena
Registernummer: HRB 503726
Geschäftsführerin: Stephanie Renda