commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Holger Haag <>
Subject Re: [betwixt] Cannot make collections work
Date Fri, 16 Sep 2005 09:07:08 GMT
robert burrell donkin wrote:

>On Wed, 2005-09-14 at 23:23 +0200, Holger Haag wrote:
>>Hi all,
>>I am trying to serialize and deserialize a HashMap using betwixt 
>>(version 0.6). Serialization (Converting the bean to XML) works fine but 
>>deserialization does not. The map is always empty. To isolate the 
>>problems I have created a unit test which should succeed but ... fails.
>betwixt is a bean-based mapper. to map constructs such as maps, it
>extends the concept of a simple property with getter and setting to a
>complex property which has a map getter and a putter taking the key and
>ItemMap doesn't match this pattern (item contains it's own key and so
>the matching property is an adder not a putter) and so the read fails.
>i suppose that the right way to handle this situation would for the
>mapper to write the values (only) without keys and then use the adder
>for reading. it's not easy to persuade betwixt to do this. in terms of a
>quick fix, the best approach might be to expose an iterator for the
>values and map than to the adder. 
>if this isn't satisfactory, then post again and i'll see whether i can
>describe a mapping that would do that. 
>- robert

Hello Robert,

thank you very much for the information and your time. I actually have 
the same problems with maps and lists, so maybe the map example was not 
the best choice.

The problem remains pretty much the same:

I am trying to serialize and deserialize a List (generic ArrayList<>)  
using betwixt (version 0.6). Serialization (Converting the bean to XML) 
works fine but  deserialization does not. The map is always empty. To 
isolate the problems I have created a unit test which should succeed but 
... fails.

The files for ItemListTest are as follows:


<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes="element">
  <element name='item'>
    <element name="itemId" property="itemId"/>
    <element name="name" property="name"/>


<?xml version='1.0' encoding='UTF-8' ?>
<info primitiveTypes="element">
  <element name='itemList'>
    <element name="item" property="items"/>

public class Item {
    private String itemId;

    public String getItemId() {
        return itemId;

    public void setItemId(String key) {
        this.itemId = key;

    private String name;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String toString() {
        return getItemId() + "=" + getName();

import java.util.ArrayList;

public class ItemList extends ArrayList<Item>{
    public void addItem(final Item item) {
    public ItemList getItems() {
        return this;



import junit.framework.TestCase;

public class ItemListTest extends TestCase {
    public void test() {
        try {
            final File file = new File("/temp/itemList.xml");
            System.out.println("file: " + file);

            final Item item0 = new Item();

            final Item item1 = new Item();

            final ItemList itemList = new ItemList();

            System.out.println("Written: " + itemList);

            final FileWriter fWriter = new FileWriter(file);
            final BeanWriter bWriter = new BeanWriter(fWriter);
            bWriter.write("itemList", itemList);

            BeanReader reader = new BeanReader();
            reader.registerBeanClass("itemList", ItemList.class);

            final Object parsed1 = reader.parse(file);
            System.out.println("parsed1: " + parsed1);
            System.out.println("parsed1.class: " + parsed1.getClass());

            final ItemList parsed2 = (ItemList)parsed1;
            System.out.println("parsed2: " + parsed2);

            assertEquals(itemList.size(), parsed2.size());
        } catch(final Exception ex) {


  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message