History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: XSTR-394
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Assignee: Joerg Schaible
Reporter: Piotr Metel
Votes: 1
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
XStream

enums as attributes

Created: 24/Apr/07 11:35 AM   Updated: 25/Feb/08 05:01 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 1.3

File Attachments: 1. Zip Archive src.zip (1 kb)

Issue Links:
dependent
 

JDK version and platform: 1.5+


 Description  « Hide
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.

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Joerg Schaible - 11/Feb/08 05:52 PM
Enums can now be defined as attributes with the standard API. You may give the head revision a try.

Joerg Schaible - 25/Feb/08 05:01 PM
Closing issues before next release.