XStream
  1. XStream
  2. XSTR-341

Unmarshalling of implicit arrays

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.4
    • Component/s: None
    • Labels:
      None

      Description

      The implicit unmarshalling support should be expanded beyond collections to include java array types. The use of array binding has the advantage infering the type of the element when it is not provided by the type attribute in the message. It's a common binding for XSD based services.

      This is not an easy case to extend because it involves overriding of ReflectionConverer, which has a lot of additional responsiblities. However, I was able to make it work with relatively easy changes. I've included changes I made to ReflectionConverter below as snippets.

      ....
      private class ArrayElem {
      public List list;
      public Class type;
      ArrayElem(List list, Class type)

      { this.list = list; this.type = type; }

      }

      ...

      Map implciitArrays = null;

      // ... add as additional condition before fieldClass.isAssiableFrom(...) ....

      if (fieldClass.isArray()) {
      Class elementClass = fieldClass.getComponentType();

      // optional type may be present for polymorphic types:
      String typeName = reader.getAttribute(mapper.attributeForImplementationClass());
      if (typeName != null)

      { elementClass = mapper.realClass(typeName); if (elementClass == null) throw new RuntimeException("realClass returned null for " + typeName); }

      Object value = context.convertAnother(result, elementClass);
      if (implciitArrays == null) implciitArrays = new HashMap(); // lazy instantiation

      ArrayElem elem = (ArrayElem) implciitArrays.get(fieldName);
      if (elem == null)

      { elem = new ArrayElem(new ArrayList(), elementClass); implciitArrays.put(fieldName, elem); }

      elem.list.add(value);
      } else....

      ///.... At end of unmarshall method....

      // write implict arrays after all values are read

      if (implciitArrays != null) {
      for (Iterator iter = implciitArrays.entrySet().iterator(); iter.hasNext()

      { Map.Entry e = (Map.Entry) iter.next(); String fieldName = (String) e.getKey(); ArrayElem elem = (ArrayElem) e.getValue(); List list = elem.list; Object array = Array.newInstance(elem.type, list.size()); reflectionProvider.writeField( result, fieldName, list.toArray((Object[]) array),null); }

      }

        Activity

        Hide
        johan1 added a comment -

        This issue was reported over a year ago and a workaround seems to be provided. However no comment or response so far.

        I think implicit arrays are a quite common use case. Is there any valid reason why this has not yet been implemented?

        Regards,
        Johan

        Show
        johan1 added a comment - This issue was reported over a year ago and a workaround seems to be provided. However no comment or response so far. I think implicit arrays are a quite common use case. Is there any valid reason why this has not yet been implemented? Regards, Johan
        Hide
        Jörg Schaible added a comment -

        This is an enhancement request with a basic implementation idea. However, snippets are no replacement for a patch. For an integration we need the changes for the AbstractReflectionProvider, we must implement or reuse the same functionality for the SerializableConverter and the JavaBeanConverter, should provide annotation support for the functionality, have to support the functionality in the XStream facade and must ensure the functionality with unit tests.

        Unless nobody actually does all the work, this issue is what it is: An enhancement request for an interesting feature that is kept here in JIRA, so that it is not forgotten and a developer may have a good starting point.

        Show
        Jörg Schaible added a comment - This is an enhancement request with a basic implementation idea. However, snippets are no replacement for a patch. For an integration we need the changes for the AbstractReflectionProvider, we must implement or reuse the same functionality for the SerializableConverter and the JavaBeanConverter, should provide annotation support for the functionality, have to support the functionality in the XStream facade and must ensure the functionality with unit tests. Unless nobody actually does all the work, this issue is what it is: An enhancement request for an interesting feature that is kept here in JIRA, so that it is not forgotten and a developer may have a good starting point.
        Hide
        Jörg Schaible added a comment -

        Support available in HEAD now.

        Show
        Jörg Schaible added a comment - Support available in HEAD now.

          People

          • Assignee:
            Jörg Schaible
            Reporter:
            Erik Smith
          • Votes:
            1 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: