Details
-
Type: Task
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: 0.6
-
Fix Version/s: 0.6
-
Component/s: Converters
-
Labels:None
Description
(From J. Matthew Prior)
I would like to be able to serialize java.net.URLs. Currently it null pointers on deserialization.
public void testHttpURL() throws Exception {
URL http_url = new URL("http://www.apple.com/");
String xml = xstream.toXML(http_url);
URL read_back = (URL) xstream.fromXML(xml);
assertEquals(read_back, http_url);
}
public void testFileURL() throws Exception {
URL file_url = new URL("file:/usr/local/bin/");
String xml = xstream.toXML(file_url);
URL read_back = (URL) xstream.fromXML(xml);
assertEquals(read_back, file_url);
}
The URLStreamHandler is marked as transient, but is only lazily initialized in constructor or readObject() (neither of which get touched by xstream).
Here is a converter that should fix the problem:
/**
- a converter for
{@link java.net.URL}
that works around
- the fact that the embedded
{@link java.net.URLStreamHandler}
is
- transient and can only be initialized via a non-default constructor
- @author jmp
- @version $Revision$
*/
public class URLConverter extends AbstractBasicConverter {
public boolean canConvert(Class type)
{ return type.equals(URL.class); } protected Object fromString(String str) {
try
catch (MalformedURLException e)
{ throw new ConversionException("Cannot parse url " + str, e); }}
}