XStream

enums as attributes

Details

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

Description

Serialization of Java 5 Enum to/from xml attribute doesn't work:
xstream.useAttributeFor(MyEnum.class)

unless SingleValueConverter is registered:
xstream.registerConverter(EnumSingleValueConverter.create(MyEnum.class));

below (and in attachment) is the converter code:

public class EnumSingleValueConverter<T extends Enum<T>>
extends EnumConverter implements SingleValueConverter {

private Class<T> enumType;

public static <V extends Enum<V>> SingleValueConverter create(Class<V> enumClass) { return new EnumSingleValueConverter<V>(enumClass); }
private EnumSingleValueConverter(Class<T> newEnumType) { this.enumType = newEnumType; }
public boolean canConvert(Class type) { return type == enumType; }
public Object fromString(String str) { return Enum.valueOf(enumType, str); }
public String toString(Object obj) { return obj.toString(); }
}

it should be possible to add support for that in XStream facade so user wouldn't have to register anything.

Issue Links

Activity

Hide
Joerg Schaible added a comment -

Enums can now be defined as attributes with the standard API. You may give the head revision a try.

Show
Joerg Schaible added a comment - Enums can now be defined as attributes with the standard API. You may give the head revision a try.
Hide
Joerg Schaible added a comment -

Closing issues before next release.

Show
Joerg Schaible added a comment - Closing issues before next release.

People

Vote (1)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: