xStream 1.4.17 error with apache.common.collections.BidiMap.DualHashBidiMap

61 views
Skip to first unread message

Vipul Gupta

unread,
Jun 25, 2021, 2:02:57 AM6/25/21
to XStream User
Is there any way I can properly serialize and deserialize unserializable-parent object in xStream-1.4.17.

For e.g - While using DualHashBidiMap of apache commons.collections in the following
code -
```
> DualHashBidiMap db = new DualHashBidiMap();
> db.put(1,2);
> XStream xStream=new XStream(new DomDriver());
> String xml = xStream.toXML(db);
> DualHashBidiMap db2 = (DualHashBidiMap)xStream.fromXML(xml);
```

I am getting following xml -
```
<org.apache.commons.collections.bidimap.DualHashBidiMap serialization="custom">
<unserializable-parents/>
<org.apache.commons.collections.bidimap.DualHashBidiMap>
<default/>
<map>
<entry>
<int>1</int>
<int>2</int>
</entry>
</map>
</org.apache.commons.collections.bidimap.DualHashBidiMap>
```

But while deserializing the same XML string, I am getting `java.lang.NullPointerException`.

One workAround that I found is to register a converter for DualHashBidiMap class as follows -
```
>xStream.register(new MapConverter(xStream.getMapper(),DualHashBidiMap.class);
```

So my question is, Is there any way I can make this work for any such class without registering a converter for it.
I apologize in advance for any naiveness. As I am not too familiar with xStream.

Best,
Vipul

Jörg Schaible

unread,
Jun 25, 2021, 1:34:15 PM6/25/21
to XStream User
Hi,

On Friday, 25. June 2021, 08:02:57 CEST Vipul Gupta wrote:
> Is there any way I can properly serialize and deserialize
> unserializable-parent object in xStream-1.4.17.
>
> For e.g - While using DualHashBidiMap of apache commons.collections in the
> following
> code -
> ```
> *> DualHashBidiMap db = new DualHashBidiMap();*
> *> db.put(1,2);*
> *> XStream xStream=new XStream(new DomDriver());*
> *> String xml = xStream.toXML(db);*
>
> *> DualHashBidiMap db2 = (DualHashBidiMap)xStream.fromXML(xml);*```
>
> I am getting following xml -
> ```
> *<org.apache.commons.collections.bidimap.DualHashBidiMap
> serialization="custom">*
> *<unserializable-parents/>*
> *<org.apache.commons.collections.bidimap.DualHashBidiMap>*
> *<default/>*
> *<map>*
> *<entry>*
> *<int>1</int>*
> *<int>2</int>*
> *</entry>*
> *</map>*
> *</org.apache.commons.collections.bidimap.DualHashBidiMap>*
> ```
>
> But while deserializing the same XML string, I am getting
> `java.lang.NullPointerException`.

Normally it works OOTB. Did you also try Java serialization - does that work?
Please provide a stack trace.

> One workAround that I found is to register a converter for DualHashBidiMap
> class as follows -
> ```
>
> >xStream.register(new
>
> MapConverter(xStream.getMapper(),DualHashBidiMap.class);
> ```

This means, that it is handled as an arbitrary Map implementation without any
support for special implementation details.

> So my question is, Is there any way I can make this work for any such class
> without registering a converter for it.

As said, it should work OOTB, but somewhere is an error.

> I apologize in advance for any naiveness. As I am not too familiar with
> xStream.

Your welcome,
Jörg


Vipul Gupta

unread,
Jun 28, 2021, 2:33:50 AM6/28/21
to xstrea...@googlegroups.com
Java serialization works just fine. 

Please provide a stack trace.
Here is the complete stack trace - 
---

Security framework of XStream not explicitly initialized, using predefined black list on your own risk.
com.thoughtworks.xstream.converters.ConversionException: Failed calling method
---- Debugging information ----
message : Failed calling method
cause-exception : java.lang.NullPointerException
cause-message : null
method : org.apache.commons.collections.bidimap.DualHashBidiMap.readObject()
class : org.apache.commons.collections.bidimap.DualHashBidiMap
required-type : org.apache.commons.collections.bidimap.DualHashBidiMap
converter-type : com.thoughtworks.xstream.converters.reflection.SerializableConverter
path : /org.apache.commons.collections.bidimap.DualHashBidiMap/org.apache.commons.collections.bidimap.DualHashBidiMap
version : 1.4.17
at com.thoughtworks.xstream.core.util.SerializationMembers.callReadObject(SerializationMembers.java:136)
at com.thoughtworks.xstream.converters.reflection.SerializableConverter.doUnmarshal(SerializableConverter.java:443)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:277)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:72)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1431)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1411)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1296)
at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1287)
at com.cisco.main.SerializerMain.normalserialization(SerializerMain.java:131)
at com.cisco.main.SerializerMain.main(SerializerMain.java:65)
Caused by: java.lang.NullPointerException
at org.apache.commons.collections.bidimap.DualHashBidiMap.readObject(DualHashBidiMap.java:101)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.thoughtworks.xstream.core.util.SerializationMembers.callReadObject(SerializationMembers.java:132)
... 14 more 

----

> One workAround that I found is to register a converter for DualHashBidiMap
> class as follows -
> ```
>
> >xStream.register(new
>
> MapConverter(xStream.getMapper(),DualHashBidiMap.class);
> ```

This means, that it is handled as an arbitrary Map implementation without any
support for special implementation details.

> So my question is, Is there any way I can make this work for any such class
> without registering a converter for it.

As said, it should work OOTB, but somewhere is an error.

> I apologize in advance for any naiveness. As I am not too familiar with
> xStream.

Your welcome,
Jörg


--
You received this message because you are subscribed to the Google Groups "XStream User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xstream-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xstream-user/5959286.eFutsTfUbz%40floh.

Jörg Schaible

unread,
Jun 28, 2021, 4:46:12 PM6/28/21
to xstrea...@googlegroups.com

Hi,


actually, I verified that this is a special case where XStream fails. You will have to open an issue at Github:

https://github.com/x-stream/xstream/issues


Hwoever, with the current version you won't get this object processed without further setup.


Regards,

Jörg


Reply all
Reply to author
Forward
0 new messages