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

I added this:

nullPolicy = new NullPolicy();

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


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.



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.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:

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

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


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

I have this class;

class Foo {
  Boolean something;

This xml:


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

This xml:


...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();
        allvarligtFelIR.addChoiceElement("something", Dummy.class);

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



  /Magnus Heino

_______________________________________________ eclipselink-users mailing list eclipselink-users@xxxxxxxxxxx

eclipselink-users mailing list


  /Magnus Heino


  /Magnus Heino

Back to the top