Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] MOXY: Map empty element as Boolean

Hi Magnus,

Glad you got it working. In the quick test case I tried, I was seeing the converter get called with null when the <something/> node was absent. Not sure what's different between your case and mine, but as long as it's working now, that's good.

I don't think there's currently any way to get empty string to write out as <something/>. There's a setting on NullPolicy to get nulls to write out as empty nodes, which would write it out in the desired format. However, since you need to marshal both absent nodes and empty nodes, and can't represent both as null, I don't think that can be leveraged here.  It would be a good enhancement to write out empty strings that way as well for direct mappings.

-Matt

On 11/02/2011 4:44 AM, Magnus Heino wrote:
I added this:

nullPolicy = new NullPolicy();
        nullPolicy.setSetPerformedForAbsentNode(false);
        nullPolicy.setNullRepresentedByEmptyNode(false);
        mapping.setNullPolicy(nullPolicy);

and now it's working. It would be really nice to have the element self-closed, but maybe thats not possible?

/Magnus

On Thu, Feb 10, 2011 at 11:44 PM, Magnus Heino <magnus@xxxxxxxxx> wrote:

I was wrong, this doesn't quite do the right thing.

When true, the xml is written like <something></something> instead of <something/>. I guess thats because of the empty string? Since it means the same thing thats really not much of a problem though... a minor problem.

But whats not so good is that when parsing xml without <something/> the converter isn't called and Foo.something is accessed directly and set to null. I guess I could add a explicit setter/getter and check for null there, but I really don't want Foo to have any setters.

?

/Magnus


On Thu, Feb 10, 2011 at 7:19 PM, Matt MacIvor <matt.macivor@xxxxxxxxxx> wrote:
Hi Magnus,

You could do this by using a combination of a Converter and a NullPolicy on a DirectMapping, which is slightly more work to map, but makes your object model better.

You would need to do something like:
XMLDirectMapping mapping = new XMLDirectMapping();
mapping.setAttributeName("something");
mapping.setXPath("something/text()");
mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
mapping.setConverter(new MyBooleanConverter());

The way this mapping is currently set up, an empty node in the XML will come back as "" and an absent node in the XML will come back as null. The converter just needs to handle the conversion from those values to a boolean:

            @Override
            public Object convertObjectValueToDataValue(Object objectValue, Session session, XMLMarshaller marshaller) {
                if(objectValue == Boolean.FALSE) {
                    return null;
                }
                return "";
            }

            @Override
            public Object convertDataValueToObjectValue(Object dataValue, Session session, XMLUnmarshaller unmarshaller) {
                if(dataValue == null) {
                    return Boolean.FALSE;
                } else {
                    return Boolean.TRUE;
                }
            }
      
Hope this helps,

-Matt

On 10/02/2011 7:25 AM, Magnus Heino wrote:

I have this class;

class Foo {
  Boolean something;
}


This xml:

<foo>
  <something/>
</foo>

...should create a mapped instance that has something = Boolean.TRUE;

This xml:

<foo>
</foo>

...should create a mapped instance that has something = Boolean.FALSE;

How can I do this? I cannot find a way to map this to a Boolean. 

Right now I create a descriptor for a dummy class, and map that instead of the boolean. Then i get null or dummy object instead of false and true

        XMLChoiceObjectMapping something = new XMLChoiceObjectMapping();
        something.setAttributeName("something");
        allvarligtFelIR.addChoiceElement("something", Dummy.class);

But this feels wrong, and I really want a Boolean....

Thanks!

--

  /Magnus Heino

_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

_______________________________________________
eclipselink-users mailing list
eclipselink-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/eclipselink-users




--

  /Magnus Heino



--

  /Magnus Heino

_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Back to the top