List<T>list serialize-deserialize problem

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

List<T>list serialize-deserialize problem

Швабауэр Павел

Good day team,
I'm having strange problem with marshaling/unmarshaling objects.

My small structure:
<data>
   <aclog>
     <index>index</index>
     <id>id</id>
     <date>date</date>
     <desc>desc</desc>
   </aclog>
   <aclog>
     <index>index</index>
     <id>id</id>
     <date>date</date>
     <desc>desc</desc>
   </aclog>
</data>

Here is small of code:

         XStream xstream = new XStream(new DomDriver());
         xstream.addImplicitCollection(CctldListActivities.class, "aclog");
         xstream.alias("data", CctldListActivities.class);
         xstream.alias("aclog", CctldAction.class);

         CctldListActivities lcat = new CctldListActivities();
         lcat.add(new CctldAction("date","index","desc","id"));
         lcat.add(new CctldAction("date","index","desc","id"));

         //marshaling is Ok!
LoggerFactory.getLogger(getClass()).debug(xstream.toXML(lcat));
         String xml = xstream.toXML(lcat);

         // and here is an exception:
         CctldListActivities lcat2 = (CctldListActivities)
xstream.fromXML(xml);

Exception text:
---- Debugging information ----
message             : index : index
cause-exception     :
com.thoughtworks.xstream.mapper.CannotResolveClassException
cause-message       : index : index
class               : uz.webname.web.cctld.CctldListActivities
required-type       : java.lang.Object
path                : /data/aclog/index
-------------------------------


Objects are very easy and small:

public class CctldListActivities {
     private List<CctldAction> aclog = new ArrayList<CctldAction>();
     private String rcode;
     public CctldListActivities () {
         aclog = new ArrayList<CctldAction>();
     }
     public void add(CctldAction cat) {
         aclog.add(cat);
     }
     // other getters/setters
}
public class CctldAction  {
     private String index, id, date, desc;
     public CctldAction(String date, String index, String desc, String id) {
         this.index = index;
         this.date = date;
         this.desc = desc;
         this.id = id;
     }
     // other getters/setters
}

Could anybody suggest me anything?



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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: List<T>list serialize-deserialize problem

Jörg Schaible-2
Hi,

Швабауэр Павел wrote:

>
> Good day team,
> I'm having strange problem with marshaling/unmarshaling objects.
>
> My small structure:
> <data>
>    <aclog>
>      <index>index</index>
>      <id>id</id>
>      <date>date</date>
>      <desc>desc</desc>
>    </aclog>
>    <aclog>
>      <index>index</index>
>      <id>id</id>
>      <date>date</date>
>      <desc>desc</desc>
>    </aclog>
> </data>
>
> Here is small of code:
>
>          XStream xstream = new XStream(new DomDriver());
>          xstream.addImplicitCollection(CctldListActivities.class,
>          "aclog"); xstream.alias("data", CctldListActivities.class);
>          xstream.alias("aclog", CctldAction.class);
>
>          CctldListActivities lcat = new CctldListActivities();
>          lcat.add(new CctldAction("date","index","desc","id"));
>          lcat.add(new CctldAction("date","index","desc","id"));
>
>          //marshaling is Ok!
> LoggerFactory.getLogger(getClass()).debug(xstream.toXML(lcat));
>          String xml = xstream.toXML(lcat);
>
>          // and here is an exception:
>          CctldListActivities lcat2 = (CctldListActivities)
> xstream.fromXML(xml);
>
> Exception text:
> ---- Debugging information ----
> message             : index : index
> cause-exception     :
> com.thoughtworks.xstream.mapper.CannotResolveClassException
> cause-message       : index : index
> class               : uz.webname.web.cctld.CctldListActivities
> required-type       : java.lang.Object
> path                : /data/aclog/index
> -------------------------------
>
>
> Objects are very easy and small:
>
> public class CctldListActivities {
>      private List<CctldAction> aclog = new ArrayList<CctldAction>();
>      private String rcode;
>      public CctldListActivities () {
>          aclog = new ArrayList<CctldAction>();
>      }
>      public void add(CctldAction cat) {
>          aclog.add(cat);
>      }
>      // other getters/setters
> }
> public class CctldAction  {
>      private String index, id, date, desc;
>      public CctldAction(String date, String index, String desc, String id)
>      {
>          this.index = index;
>          this.date = date;
>          this.desc = desc;
>          this.id = id;
>      }
>      // other getters/setters
> }
>
> Could anybody suggest me anything?

The problem is that you define the implicit list as arbitrary list of
objetcs and therefore any element will be matched against the members of the
class first. Since "aclog" matches a real member, the wrong converter is
selected at deserialization time.

In your case you will have to define the implicit list explicitly:

=================== %< =================
xstream.alias("data", CctldListActivities.class);
xstream.addImplicitCollection(CctldListActivities.class, "aclog", "aclog",
CctldAction.class);
=================== %< =================

That's all.

- Jörg


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

    http://xircles.codehaus.org/manage_email