commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From robert burrell donkin <robertburrelldon...@blueyonder.co.uk>
Subject Re: [betwixt] Map content serialization
Date Sun, 13 Mar 2005 22:46:06 GMT
hi

i've taken a look at this and it seems to work fine on my machine. i
suspect that this has already been fixed in HEAD.

could you please try again with a fresh build from HEAD or a recent
nightly?

- robert

On Wed, 2005-03-09 at 17:14 -0500, Cliche Sylvain wrote:
> Whenever I try to convert the DTO_Test object that contains a Map into a XML
> String the display looks like this (see test program below):
> 
> String:   
> <DTO_Test>
>     <addresses/>
> </DTO_Test>
> 
> However, if the method setWriteEmptyElements(true) is called over the
> BeanWriter object instance with a value of true for its parameter:
> BeanWriter writer = new BeanWriter(outputWriter);
> writer.setWriteEmptyElements(true);
> 
> then the display looks like this (the content of the Map is actually
> serialized):
> String:   
> <DTO_Test>
>     <addresses>
>       <entry>
>         <key>key_B</key>
>         <value>valeur_B</value>
>       </entry>
>       <entry>
>         <key>key_A</key>
>         <value>valeur_A</value>
>       </entry>
>     </addresses>
> </DTO_Test>
> 
> So I'm a little surprised to have to set to true this method in order to
> serialize the content of a Map. Here is a fragment of the JavaDoc from the
> method AbstractBeanWriter .setWriteEmptyElements(boolean writeEmptyElements)
> and nothing is mentionned regarding the serialization of the content of a
> Map:
> 	Sets whether empty elements should be written into the output.
> 	An empty element is one that has no attributes, no child elements
> and no body text. For example, <element/> is an empty element but <element
> attr='value'/> is not. 
> Also, the downside with this approach is that all empty fields from the
> objects get serialized as well, which adds useless data (I don't need all
> the empty elements I just need the content of a Map). Is there a way to
> serialize the content of a Map without having to set to true the method
> setWriteEmptyElements ? I am using Betwixt 0.6 with WSAD 5.1 in a J2EE 1.3
> environment. The test program and most objects are written below.
> 
> Thank's,
> 
> Sylvain
> 
> /**
>  * Object which hold data to be exported to xml
>  */
> public class DTO_Test {
> 	public DTO_Test() { super(); }	
> 	private Map addresses = new HashMap();
> 	public Map getAddresses() { return addresses; }	
> 	public void setAddresses(Map newAddresses) { this.addresses =
> newAddresses; }	
> 	public void addAddress(String name, Object bla) {
> addresses.put(name, bla); }
> }
> 
> 
> /**
>  * Actual test program
>  */
> public class ObjectSerialization {
> 	public static final void main(String [] bla) {
> 		DTO_Test dto = new DTO_Test();
> 		dto.addAddress("key_A", "value_A");
> 		dto.addAddress("key_B", "value_B");
> 		
> 		XmlUtilTest xmlUtil = new XmlUtilTest();
> 
> 		try {
> 			String strObj = xmlUtil.toXml(dto);
> 			System.out.println("String: \n" + strObj);
> 		}
> 		catch(Exception ex) {
> 			System.out.println(">>>> Exception: " +
> ex.getMessage());
> 		}
> 	}
> }
> 
> /**
>  * Contains methods to call in order to perform the serialization
>  */
> public class XmlUtilTest {
> 	private static BindingConfiguration configuration =
> 		new BindingConfiguration(new CustomObjectStringConverter(),
> true);
> 	/**
> 	 * Transform object content to xml
> 	 */
> 	public String toXml(Object object) throws Exception {
> 		if (object == null) { return ""; }
> 
> 		StringWriter outputWriter = new StringWriter();
> 		BeanWriter writer = new BeanWriter(outputWriter);
> 		writer.setBindingConfiguration(configuration);
> 		writer.getBindingConfiguration().setMapIDs(false);
> 		/*
> 		* Must set to true to have the content of a Map serialized
> 		* However by doing so all empty fields get serialized as
> empty elements (<example/>)
> 		*/
> 		writer.setWriteEmptyElements(false);
> 	
> writer.getXMLIntrospector().getConfiguration().setAttributesForPrimitives(fa
> lse);
> 		writer.enablePrettyPrint();
> 	
> writer.getXMLIntrospector().getConfiguration().setAttributeNameMapper( new
> DecapitalizeNameMapper());
> 	
> writer.getXMLIntrospector().getConfiguration().setElementNameMapper( new
> DecapitalizeNameMapper());
> 
>   		String name = object.getClass().getName();
>   		name = name.substring(name.lastIndexOf(".") + 1);
> 		writer.write(name , object);
> 		return outputWriter.toString();
> 	}
> }
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 

-- 
robert burrell donkin <robertburrelldonkin@blueyonder.co.uk>


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message