commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Holger Haag <Holger.H...@SoftConEx.de>
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
>value. 
>
>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:

Item.betwixt
============

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


ItemList.betwixt
================

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


Item.java
=========

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) {
        this.name = name;
    }

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



ItemList.java
=============

import java.util.ArrayList;

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


ItemListTest.java
=================

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.io.BeanWriter;

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();
            item0.setItemId("item0");
            item0.setName("name0");

            final Item item1 = new Item();
            item1.setItemId("item1");
            item1.setName("name1");

            final ItemList itemList = new ItemList();
            itemList.addItem(item0);
            itemList.addItem(item1);

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

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

            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) {
            ex.printStackTrace();
            fail(ex.getMessage());
        }
    }
}


Rgds
Holger


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