XStream

Converter for JDK1.5 Enum

Details

  • Type: New Feature New Feature
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.1.3
  • Component/s: Converters
  • Labels:
    None

Description

— From Eric Snell —

Great product. I don't have time to become a developer right now, but I wanted to make this contribution. I'm using 1.5 and needed an Enum converter. Hope this method helps:

private void installEnumConverter(XStream stream) {
final Converter converter = stream.getConverterLookup().lookupConverterForType(Enum.class);
if (converter.getClass() == ReflectionConverter.class) {
stream.registerConverter(new Converter() {
public boolean canConvert(Class type) { return (type.isEnum()); }

public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { converter.marshal(source, writer, context); }

public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { Enum rc = (Enum)converter.unmarshal(reader, context); return Enum.valueOf(rc.getDeclaringClass(), rc.name()); }
});
}
}

Activity

Hide
Matt Beanland added a comment -

Support for EnumMap and EnumSet also useful.

Show
Matt Beanland added a comment - Support for EnumMap and EnumSet also useful.
Hide
B. K. Oxley (binkley) added a comment -

This works nicely in the unit test I wrote to test it, but I do not find the Enum converter support in the SNAPSHOT build I downloaded (2005-2-27).

Show
B. K. Oxley (binkley) added a comment - This works nicely in the unit test I wrote to test it, but I do not find the Enum converter support in the SNAPSHOT build I downloaded (2005-2-27).
Hide
Joe Walnes added a comment -

It would also be nice to be able to specify how the Enum should be represented in XML - as an integer index, or a string value.

For example. Given:

enum Dude { GOOD, BAD, UGLY; }

Dude.UGLY could be represented as <Dude>2</Dude> or <Dude>UGLY</Dude>.

This should be an option the user can change. However the default should probably be the string representation.

Show
Joe Walnes added a comment - It would also be nice to be able to specify how the Enum should be represented in XML - as an integer index, or a string value. For example. Given: enum Dude { GOOD, BAD, UGLY; } Dude.UGLY could be represented as <Dude>2</Dude> or <Dude>UGLY</Dude>. This should be an option the user can change. However the default should probably be the string representation.
Hide
Joe Walnes added a comment -

Done. Plus incorporated polymorphic enum support (see XSTR-204).

EnumSet and EnumMap support have moved to a new issue: XSTR-206

Show
Joe Walnes added a comment - Done. Plus incorporated polymorphic enum support (see XSTR-204). EnumSet and EnumMap support have moved to a new issue: XSTR-206
Hide
Joerg Schaible added a comment -

No further comment from reporter

Show
Joerg Schaible added a comment - No further comment from reporter

People

Vote (1)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: