Problem deserializing XML into java objects.

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

Problem deserializing XML into java objects.

Venkatesh
I have run into a problem using XStream to serialize and deserialize XML streams. The following is my scenario:

public interface MyInterface {

}

public class MyObjectA implements MyInterface  {
     private int x; ..
}

public class MyObjectB implements MyInterface  {

     private String test; ...

}

public class ClassThatNeedsTransformation {
    private MyInterface x;
}

While XStream is able to serialize the ClassThatNeedsTransformation based on the type of object that is held by it, I am having problems getting XStream to deserialize the same based on the specific type held in "x". I read up some docs and it seems to indicate that I need to provide XStream with a "default" implementation class for the interface in which case, I lose the flexibility to store either of the concrete implementations of the interface. Wanted to know if this is indeed a limitation of XStream and if no, how I would overcome the same. Appreciate your help on the topic.

Venkatesh
Reply | Threaded
Open this post in threaded view
|

Re: Problem deserializing XML into java objects.

Jörg Schaible-3
Hi Venkatesh,

Venkatesh wrote:

> I have run into a problem using XStream to serialize and deserialize XML
> streams. The following is my scenario:
>
> public interface MyInterface {
>
> }
>
> public class MyObjectA implements MyInterface  {
>      private int x; ..
> }
>
> public class MyObjectB implements MyInterface  {
>
>      private String test; ...
>
> }
>
> public class ClassThatNeedsTransformation {
>     private MyInterface x;
> }
>
> While XStream is able to serialize the ClassThatNeedsTransformation based
> on the type of object that is held by it, I am having problems getting
> XStream to deserialize the same based on the specific type held in "x". I
> read up some docs and it seems to indicate that I need to provide XStream
> with a "default" implementation class for the interface in which case, I
> lose the flexibility to store either of the concrete implementations of
> the interface. Wanted to know if this is indeed a limitation of XStream
> and if no, how I would overcome the same. Appreciate your help on the
> topic.

I am not sure if I understand your problem correctly. Let's have a list with
two instances of ClassThatNeedsTransformation, each one with a different
implementation for MyInterface. The resulting XML will look similar to:

=========== %< ==============
 <list>
   <ClassThatNeedsTransformation>
     <x class="MyObjectA">
       <x>42</x>
     </x>
   </ClassThatNeedsTransformation>
   <ClassThatNeedsTransformation>
     <x class="MyObjectB">
       <test>Arthur</test>
     </x>
   </ClassThatNeedsTransformation>
 </list>
=========== %< ==============

You see, the XML contains any information required to recreate the persisted
object graph, the information which implementation has to be taken for the
MyInterface instance is kept in the "class" attribute.

So, what do you want to do?

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Problem deserializing XML into java objects.

Venkatesh
Jorg:

Thanks for replying back to me. The problem I have is that the client that is constructing the XML is generating the XML without the "class" hints you have outlined in your example (so information about what concrete class is embedded in the xml is not present).  Hope that clarifies the problem I am trying to see if there is a solution for?

Venkatesh


On Thu, Jun 6, 2013 at 1:15 AM, Jörg Schaible <[hidden email]> wrote:
Hi Venkatesh,

Venkatesh wrote:

> I have run into a problem using XStream to serialize and deserialize XML
> streams. The following is my scenario:
>
> public interface MyInterface {
>
> }
>
> public class MyObjectA implements MyInterface  {
>      private int x; ..
> }
>
> public class MyObjectB implements MyInterface  {
>
>      private String test; ...
>
> }
>
> public class ClassThatNeedsTransformation {
>     private MyInterface x;
> }
>
> While XStream is able to serialize the ClassThatNeedsTransformation based
> on the type of object that is held by it, I am having problems getting
> XStream to deserialize the same based on the specific type held in "x". I
> read up some docs and it seems to indicate that I need to provide XStream
> with a "default" implementation class for the interface in which case, I
> lose the flexibility to store either of the concrete implementations of
> the interface. Wanted to know if this is indeed a limitation of XStream
> and if no, how I would overcome the same. Appreciate your help on the
> topic.

I am not sure if I understand your problem correctly. Let's have a list with
two instances of ClassThatNeedsTransformation, each one with a different
implementation for MyInterface. The resulting XML will look similar to:

=========== %< ==============
 <list>
   <ClassThatNeedsTransformation>
     <x class="MyObjectA">
       <x>42</x>
     </x>
   </ClassThatNeedsTransformation>
   <ClassThatNeedsTransformation>
     <x class="MyObjectB">
       <test>Arthur</test>
     </x>
   </ClassThatNeedsTransformation>
 </list>
=========== %< ==============

You see, the XML contains any information required to recreate the persisted
object graph, the information which implementation has to be taken for the
MyInterface instance is kept in the "class" attribute.

So, what do you want to do?

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|

Re: Re: Problem deserializing XML into java objects.

Jörg Schaible-2
Hi Venkatesh,

Venkatesh wrote:

> Jorg:
>
> Thanks for replying back to me. The problem I have is that the client that
> is constructing the XML is generating the XML without the "class" hints
> you have outlined in your example (so information about what concrete
> class is
> embedded in the xml is not present).  Hope that clarifies the problem I am
> trying to see if there is a solution for?

Point is that XStream is "from Java to XML and back", i.e. it ensures that
all required information is present to deserialize an equivalent object
graph again.

In your XML this information is simply not available for XStream, so it is
up to *you* to take proper action. You can register a custom converter that
will handle all objects that implement MyInterface and you have to decide in
its deserialization method (e.g. based on the name of the first child
element) what instance has to be created.

Regards,
Jörg


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

    http://xircles.codehaus.org/manage_email