[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [eclipselink-users] MOXY: Map empty element as Boolean
|
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:
...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