XStream
  1. XStream
  2. XSTR-480

XStream does not properly alias arrays of objects.

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.2.2
    • Fix Version/s: 1.3.1
    • Component/s: Core
    • Labels:
      None

      Description

      The following email exchange explains everything:

      Hi Tom,
      ________________________________

      From: Thomas P. Fuller
      Sent: Wednesday, February 27, 2008 12:50 PM
      Subject: Re: [xstream-user] Re: Question about changing the default root name when xstream.toXML / fromXML is invoked

      Hi Jorg,

      I've actually tried this – and I've done it again, just to make sure I've not missed something.

      Here's the simple test, and output:

      Code:

      public void test1 () {

      XStream xstream = new XStream (new JettisonMappedXmlDriver ());

      xstream.setMode(XStream.NO_REFERENCES);

      xstream.alias("CriterionValueTypeArgument", CriterionValueTypeArgument.class);
      xstream.alias("CriterionValueTypeArguments", CriterionValueTypeArgument[].class);

      CriterionValueTypeArgument cvta = new CriterionValueTypeArgument ();

      cvta.setValue("value");
      cvta.setDataValue("dataValue");
      cvta.setDataType("dataType");
      cvta.setDisplayValue("displayValue");

      CriterionValueTypeArgument[] list = new CriterionValueTypeArgument[]

      { cvta }

      ;

      String result = xstream.toXML (list);

      System.out.println("test1; json text: " + result);
      }

      Output:

      test1; json text: {"CriterionValueTypeArgument-array":{"CriterionValueTypeArgument":

      {"value":"value","dataValue":"dataValue","dataType":"dataType","displayValue":"displayValue"}

      }}

      Tom
      ________________________________

      Unfortunately you're right. I could have sweared, that aliasing array types work and I already used it. Can you open a JIRA issue for this as enhancement request?

      • Jörg

      ---------------------------------------------------------------------

      A work-around provided by Jörg is as follows:

      Yeah, since it is as I already stated: It is currently not supported. At serialization time XStream will add its array marker to the tag name to be able to deserialize it. This marker is added before any alias handling. The deserialization works though, since the array marker is missing and XStream will find then the alias.

      You can implement a small workaround though using a custom mapper:

      XStream xstream = new XStream() {
      MapperWrapper wrapMapper(MapperWrapper next) {
      return new MapperWrapper(next) {
      public String serializedClass(Class type)

      { return type == CriterionValueTypeArgument[].class ? "CriterionValueTypeArguments" : super.serializedClass(type); }

      }
      }
      };

        People

        • Assignee:
          Jörg Schaible
          Reporter:
          Thomas P. Fuller
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: