users@jaxb.java.net

Re: mapping using annotations without compiling a schema

From: Sekhar Vajjhala <Sekhar.Vajjhala_at_Sun.COM>
Date: Mon, 20 Jun 2005 22:00:21 -0400

Dmitri Colebatch wrote:

>On 6/21/05, Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com> wrote:
>
>
>>I agree that a good tutorial of how to annotate your own class is
>>necessary. I'm sure we'll have something by the first release, but until
>>then hang in there...
>>
>>
>
>Thanks - I have no problems hanging in there, I was more concerned of
>asking a question that warranted a response of RTFM (o: I'm waiting
>on my request for cvs access to come through, so hopefully that'll
>make it a bit easier to keep up with what's going on.
>
>
>
>>By default, there's @XmlAccessorType(PUBLIC) assumed on each package. So
>>that's why you are seeing a lot of unwanted elements in your XML. Try to
>>set it to @XmlAccessorType(NONE) to have no default, or use @XmlTransient.
>>
>>When I did a similar thing, @XmlAccessorType(NONE) suited me better,
>>because in that way only annotated fields get mapped to XML.
>>
>>
>
>Hmm, I'm in splashing around in the deep end with annotations, but as
>I understand it the default is really
>@XmlAccessorType(AccessType.PROPERTY) and what you're suggesting is
>@XmlAccessorType(AccessType.NONE) - however I don't see
>AccessType.NONE (it only have property and field).
>
>Using XmlTransient as suggested by you and Lexi I managed to make the
>following do what I want:
>
>@XmlRootElement(name = "DealMsg", namespace = "urn:deal")
>public class Deal
>{
> @XmlEnum(value=String.class)
> enum DealStatus
> {
> @XmlEnumValue(value="In progress")
> inProgress,
> @XmlEnumValue(value = "Finished")
> finished
> };
>
> @XmlElement(name = "DealStatus", namespace = "urn:deal")
> private DealStatus status = DealStatus.inProgress;
>
> @XmlTransient
> public DealStatus getStatus()
> {
> return status;
> }
>
> public void setStatus(DealStatus status)
> {
> this.status = status;
> }
>}
>
>It spits out
>
><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
><DealMsg xmlns="urn:deal">
> <DealStatus>In progress</DealStatus>
></DealMsg>
>
>But it feels dirty. The field is not transient, I'm simply hacking
>around my inability to make your suggestion of NONE work. That
>suggestion feels much better to me. Is this something that perhaps
>has slipped through the cracks, or is it a misunderstanding of mine
>with regard to annotations?
>
Neither. The reason you don't see AccessType.NONE and AccessType.PUBLIC
is because they have been
introduced since Early Draft 2 but the later version of the
spec/javadocs are not available (but
will be available soon). By the way it is going to be
AccessType.PUBLIC_MEMBER not
AccessType.PUBLIC.


>
>Thanks both for your help. Look forward to hearing more about the
>@XmlAccessorType(AccessType.NONE) option.
>
There are other alternatives to using @XmlTransient - even with Early
Draft 2 version.

a. set the default to @XmlAccessorType(AccessType.PROPERTY)
    move the annotation to the getter/setter from the field to the property.

b. set the default to @XmlAccessorType(AccessType.FIELD)
    the property is not serialized by default.

c. Both a and b are in Early Draft 2. This one will be in post Early
Draft 2.
    The default will be @XmlAccessorType(AccessType.PUBLIC_MEMBER) which
    means only public fields and properties are serialized by default.
Thus if you
    put the annotation on the property there is no need to put
@XmlTransient on the
    field itself since the field is private and won't be serialized by
default anyway.

d. This one will be available in the post Early Draft 2 version.
    set default serialization to @XmlAccessorType(AccessType.NONE) which
means
    neither fields nor properties are serialized by default; the
field/property has to be
    explicitly annotated for it be serialized.

Sekhar
   

>
>cheers
>dim
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>