tikxml icon indicating copy to clipboard operation
tikxml copied to clipboard

Feature request: Allow @Xml annotation on enum types

Open seasox opened this issue 6 years ago • 0 comments

I'm currrently transitioning from SimpleXML to TikXML, but there seems to be no way to create enums from markup. Given a markup like this:

<user>
 <name>Jane Doe</name>
 <permissions>
  <permission>read</permission>
  <permission>write</permission>
  <permission>delete</permission>
 </permissions>
</user>

And classes like these:

@Xml
class User {
    @Element(name="name")
    String name;
    @Path("permissions")
    @Element(name="permission")
    List<Permission> permissions;
}

enum Permission {
    case READ("read")
    case WRITE("write")
    case DELETE("delete")

    private String name;

    Permission(String name) {
        this.name = name;
    }

    String getName() { return name; }

In SimpleXML, I was able to write a Converter like this:

class PermissionConverter implements TypeConverter<Permission> {
    public Permission read(String val) throws Exception {
       for (Permission p: Permission.values()) {
            if (p.getName().equals(val)) {
                return p;
            }
        }
        throw IllegalArgumentException("No case for " + p);
    }

    public String write(Permission p) {
         return p.getName();
    }
}

Are there any technical or performance problems with this approach? Thanks in advance!

seasox avatar Jan 24 '19 18:01 seasox