Hi Jörg,

Let explain my question a little better.  

The 'Address' class looks something like -

class Address {
  
   private String street1;
  
   private String street2;

   private String city;

   private String state;

   private String zip;

   private ADDR_TYPE addressType; // this value is in the 'omitFields' list
as we make an attribute of it later

}

where ADDR_TYPE is an emum defined somewhere else as -

enum ADDR_TYPE {
   HOME, 
   OFFICE 
}

The AddressConverter looks like -

public class AddressConverter extends JavaBeanConverter {

public boolean canConvert(Class clazz) {
                return clazz.equals(Address.class);
        }

public void marshal(Object value, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
      writer.addAttribute("type", ((Address)value).getAddressType());
      super.marshal(value, writer, context); // Calls 'JavaBeanConverter's
marshal
}

public Object unmarshal(HierarchicalStreamReader reader,  
UnmarshallingContext context) {
      super.unmarshal(reader, context);
}

}

Using this converter and alias for both the fields as 'address', I could get
the xml which I said below.

Now since you rightly said -

> Because PlaceInfo has no field named "address". Therefore the converter
> for 
> the PlaceInfo instance does not know, what "address" means at all.

during unmarshal, even though I have a qNameMap mapping 'address' to the
'Address.class', it makes a new instance but cannot relate the new instance
to any member in the 'PlaceInfo' class.

One solution to this would be a right a converter for the 'PlaceInfo' class
and not just the 'Address' class.

I was wondering if there is a way of specifying or changing the 'nodeName'
that the HierarchicalStreamReader is seeing at the moment in the read
process.

For eg. when reader.getNodeName() equals 'address', can I refer to the
mapper of the xstream configuration and give it a new name based on some
condition.  This way, in this case, I could say that look at the 'type'
attribute of the opening tag and depending on that, rename the field to
either 'homeAddress' or 'officeAddress'.

I will go ahead with making a converter for the 'PlaceInfo' class as that
seems like the simplest solution.


Jörg Schaible-2 wrote:
> 
> Hi Sriram,
> 
> sriram v wrote:
> 
>> 
>> Hi,
>> 
>> I have a class like -
>> 
>> class PlaceInfo {
>> 
>> Address homeAddress;
>> 
>> Address officeAddress;
>> 
>> }
>> 
>> I serialize the xml as -
>> 
>> <address type="home"></address>
>> <address type="office"></address>
> 
> This is what you get when you serialize a PlaceInfo instance? Sorry, but I 
> don't buy this ...
> 
>> Basically I made a 'AddressConverter' class extending JavaBeanConverter
>> with adding the 'type' attribute in the 'marshal' function.
> 
> What type is your converter actually handling and what does it in the 
> marshal method?
>  
>> When I try to 'unmarshal' this xml, the 'AbstractReflectionConverter'
>> kicks in and raises 'ConversionException' saying that it can't find
>> 'address'
>> field in the 'PlaceInfo' class.  I can see why it is trying to do this.
> 
> Because PlaceInfo has no field named "address". Therefore the converter
> for 
> the PlaceInfo instance does not know, what "address" means at all.
> 
>> How do I tell the 'unmarshal' function to look for 'homeAddress' or
>> 'officeAddress' using the xstream configuration?
> 
> You cannot. If you really want to have the PlaceInfo element in your XML
> to 
> contain two elements named "address", you'll need a custom converter for 
> PlaceInfo.
> 
>> I can make an 'Address'
>> object in the 'unmarshal' function and return it but it still gives the
>> same error.
> 
> You will have to create an Address instance, but I simply guess your 
> unmarshal method is not even called.
> 
> So, did you read and understand the converter tutorial?
> 
> Regards,
> Jörg
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
> 
>     http://xircles.codehaus.org/manage_email
> 
> 
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Deserializing-multiple-fields-of-same-type-tp33150949p33152082.html
Sent from the xstream - user mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to