XStream

Multiple implicit collections with @XStreamImplicitCollection annotation

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.2.2
  • Component/s: None
  • Labels:
    None
  • JDK version and platform:
    1.5

Description

Hello,
I was trying to map an XML file with multiple collections that must mapped as implicit collections.

I would like to map them using Java 5 annotations, but at the class level I can use a single @XStreamImplicitCollection having a single String parameter for the collection. Don't know if I missed something, or if I'm using the wrong approach. Please let me know if I'm in the latter case.

At present, for having things working, I modified XStreamImplicitCollection.java and Annotation.java sources for having an array of parameters. So I can map multiple collection writing the following code:

@XStreamAlias("Widget")
@XStreamImplicitCollection({"properties", "containers"})
public class Widget {
private String Name;
private List<Property> properties = new ArrayList<Property>();
private List<Container> containers = new ArrayList<Container>();
}

I attached the modified sources and the diff file. Hope this can help.

  1. annotations.diff
    01/Dec/06 10:09 AM
    1 kB
    Lucio Benfante
  2. Annotations.java
    01/Dec/06 10:08 AM
    9 kB
    Lucio Benfante
  3. Annotations.java
    27/Nov/06 9:27 AM
    8 kB
    Lucio Benfante
  4. implicit_collections.diff
    27/Nov/06 9:27 AM
    3 kB
    Lucio Benfante
  5. ImplicitCollectionTest.java
    01/Dec/06 10:08 AM
    2 kB
    Lucio Benfante
  6. XStreamImplicit.java
    01/Dec/06 10:08 AM
    0.6 kB
    Lucio Benfante
  7. XStreamImplicitCollection.java
    27/Nov/06 9:27 AM
    0.4 kB
    Lucio Benfante

Activity

Hide
Joerg Schaible added a comment -

Hi Lucio,

the problem with this modification is, that it breaks backward compatibility. Nevertheless, we were discussing internally about this annotation anyway, since we have the impression, it should be assigned to a field and not to a class.

... we'll see ...

Show
Joerg Schaible added a comment - Hi Lucio, the problem with this modification is, that it breaks backward compatibility. Nevertheless, we were discussing internally about this annotation anyway, since we have the impression, it should be assigned to a field and not to a class. ... we'll see ...
Hide
Lucio Benfante added a comment -

Hi Joerg,
yes, I know it's breaking compatibility. It was just an hack for pressure.

An field annotation could be a better solution. Have you an estimate of when that feature could be availble in XStream? Let me know if I could help.

Lucio

Show
Lucio Benfante added a comment - Hi Joerg, yes, I know it's breaking compatibility. It was just an hack for pressure. An field annotation could be a better solution. Have you an estimate of when that feature could be availble in XStream? Let me know if I could help. Lucio
Hide
Lucio Benfante added a comment -

Hi,
I just relized that my patch is in fact backward compatible.

If you have a single implicit collection in you class, you can still write:

@XStreamImplicitCollection("properties")...

Of course a field annotation could still be better...

Lucio

Show
Lucio Benfante added a comment - Hi, I just relized that my patch is in fact backward compatible. If you have a single implicit collection in you class, you can still write: @XStreamImplicitCollection("properties")... Of course a field annotation could still be better... Lucio
Hide
Joerg Schaible added a comment -

Hehehe, you simply revealed my lack of JDK 5 knowledge

Show
Joerg Schaible added a comment - Hehehe, you simply revealed my lack of JDK 5 knowledge
Hide
Lucio Benfante added a comment -

Hello,
as discussed in the dev mailing-list, a field annotation is a better approach for defining an implicit collection.

I implemented it for my purposes. Attached you'll find:

  • XStreamImplicit.java : source of the new field level annotation
  • ImplicitCollectionTest.java : some tests of the new annotation
  • Annotations.java : modified source of the annotation configurator
  • annotations.diff : diff file between the original Annotations.java and my modifed file.

Hope this help, and could be integrated in XStream.

Lucio

Show
Lucio Benfante added a comment - Hello, as discussed in the dev mailing-list, a field annotation is a better approach for defining an implicit collection. I implemented it for my purposes. Attached you'll find:
  • XStreamImplicit.java : source of the new field level annotation
  • ImplicitCollectionTest.java : some tests of the new annotation
  • Annotations.java : modified source of the annotation configurator
  • annotations.diff : diff file between the original Annotations.java and my modifed file.
Hope this help, and could be integrated in XStream. Lucio
Hide
Lucio Benfante added a comment -

Implementation and tests of the @XStreamImplicit field annotation.

Show
Lucio Benfante added a comment - Implementation and tests of the @XStreamImplicit field annotation.
Hide
Lucio Benfante added a comment -

Diff file of Annotations.java after the introduction of the @XStreamImplicit field annotation

Show
Lucio Benfante added a comment - Diff file of Annotations.java after the introduction of the @XStreamImplicit field annotation
Hide
Joerg Schaible added a comment -

Hi Lucio,

thanks a lot for the patch and the test cases. I've applied it to the subversion repository. You might build a SNAPSHOT version of XStream yourself.

  • Jörg
Show
Joerg Schaible added a comment - Hi Lucio, thanks a lot for the patch and the test cases. I've applied it to the subversion repository. You might build a SNAPSHOT version of XStream yourself.
  • Jörg

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: