XStream

Provide reflection & annotation data to converter code

Details

  • Type: Improvement Improvement
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.2.1
  • Fix Version/s: 1.4.2
  • Component/s: Converters, Core
  • Labels:
    None

Description

While trying to implement a workaround to accomplish XSTR-405, I found no way for the code in a marshal method to access the annotations associated with the field/value that is being converted.

If this were possible, then it would be easy for developers to attach their own annotations and read them from the code that would convert a given class, without having to provide their own top-level converter.

It may seem like @XStreamConverter is an out here, but it offers no mechanism for providing per-use data values – and I don't think it could.

That is, you could not have (and would not want):

@XStreamConverter(class=MyClassConverter.class, customTweak=true)
MyClass fieldA;

@XStreamConverter(class=MyClassConverter.class, customTweak=false, tagName="foo")
MyClass fieldB;

But, if annotations were passed along, perhaps as part of the MarshallingContext, then I could write:

@XStreamConverter(MyClassConverter.class)
@MyAnno(customTweak=true)
MyClass fieldA;

@XStreamConverter(MyClassConverter.class)
@MyAnno(customTweak=true, tagName="foo")
MyClass fieldB;

It would also be very useful in the canConvert class to get access to this data. For compatibility, you could perhaps have:

interface ReflectiveConverter {
canConvert(Class clazz, AccessibleObject aobject);
}

and call that instead if the converter implements it.

Activity

Hide
paul hinds added a comment -

This would also help in marshalling

private List<String[]> myValues;

Currentty you can only register converters per class, and there is no way at runtime to revert to the default converter.

<myValues> <!-- can get rid of this with XStreamImplicit but this contains the element name we want -->
<string-array> <!-- generics determine the output here, I don't want to implement a String[] converter -->
<string>some value</string> <!-- this "string" can be reduced to <s> but only for all String[]s -->
<string>other value</string>

We also marshal and unmarshal object in both JSON and XML it would be good to be able to tune the marshalling for XML without impacting the JSON output (which is difficult enough already)

Show
paul hinds added a comment - This would also help in marshalling private List<String[]> myValues; Currentty you can only register converters per class, and there is no way at runtime to revert to the default converter. <myValues> <!-- can get rid of this with XStreamImplicit but this contains the element name we want --> <string-array> <!-- generics determine the output here, I don't want to implement a String[] converter --> <string>some value</string> <!-- this "string" can be reduced to <s> but only for all String[]s --> <string>other value</string> We also marshal and unmarshal object in both JSON and XML it would be good to be able to tune the marshalling for XML without impacting the JSON output (which is difficult enough already)
Hide
Joerg Schaible added a comment -

@XStreamConverter annotation does support now arbitrary parameters that are made available with a simple constructor based dependency injection mechanism. This works as well for local converter declarations. Already available in latest snapshot.

Show
Joerg Schaible added a comment - @XStreamConverter annotation does support now arbitrary parameters that are made available with a simple constructor based dependency injection mechanism. This works as well for local converter declarations. Already available in latest snapshot.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: