Details

    • Type: Task Task
    • Status: Closed Closed
    • Priority: Major 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

      { return new URL(str); }

      catch (MalformedURLException e)

      { throw new ConversionException("Cannot parse url " + str, e); }

      }

      }

        People

        • Assignee:
          Unassigned
          Reporter:
          Joe Walnes
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: