|
View:
New views
2 Messages
—
Rating Filter:
Alert me
|
|
|
Omit java.util.Map field when empty?Hello there,
when i got a class like this: public class Foo { private String name = "bla"; private Map<String, String> events = new HashMap<String, String>(); ... } And the events-Map has contents, im ok with output like this: <Foo> <name>bla</name> <events> <entry> <string>foo</string> <string>bar</string> </entry> </events> </Foo> But whats the best way to get output like this, if the events Map is empty?? <Foo> <name>bla</bla> </Foo> currently im getting: <Foo> <name>bla</bla> <events /> </Foo> I realize that an empty map should be different from null, but since im using a lot of empty Maps i'd like to avoid these tags in the output. Using a custom Map Converter doesnt solve this, since the <events> tag comes from the fieldname and not from the Map deserialization. Another Problem: the Map is a field in a common baseclass. Do i have to write a custom MarshallingStrategy that uses the original one, but omits fieldnames for empty Maps? If so, what is the easiest way? Would it work to initialize this field with null by default, and only create the map, when adding an item? Thanks in advance. --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
Re: Omit java.util.Map field when empty?Hi Chris,
Chris wrote: > Hello there, > > when i got a class like this: > > public class Foo { > private String name = "bla"; > private Map<String, String> events = new HashMap<String, String>(); > ... > } > > And the events-Map has contents, im ok with output like this: > <Foo> > <name>bla</name> > <events> > <entry> > <string>foo</string> > <string>bar</string> > </entry> > </events> > </Foo> > > But whats the best way to get output like this, if the events Map is > empty?? > > <Foo> > <name>bla</bla> > </Foo> > > currently im getting: > > <Foo> > <name>bla</bla> > <events /> > </Foo> > > I realize that an empty map should be different from null, That's the reason. > but since > im using a lot of empty Maps i'd like to avoid these tags in the > output. > > Using a custom Map Converter doesnt solve this, since the <events> tag > comes from the fieldname and not from the Map deserialization. Yep. > Another Problem: the Map is a field in a common baseclass. > > Do i have to write a custom MarshallingStrategy that uses the original > one, but omits fieldnames for empty Maps? No, the tags are written by the converter used for Foo (presumably the ReflectionConverter). > If so, what is the easiest way? > > Would it work to initialize this field with null by default, and only > create the map, when adding an item? Yes. Also keep in mind, that the deserialization of XML without the empty events tag will also set this field to null - you have to handle it then anyway. As alternative you may write writeReplace/readResolve methods if your objects are cloneable: private Object writeReplace() { Foo written; if (!events.isEmpty()) { written = this; } else { written = clone(); clone.events = null; } return written; } private Object readResolve() { if (events == null) { events = new HashMap<String, String>(); } return this; } - Jörg --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
| Free embeddable forum powered by Nabble | Forum Help |