Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Allow @Xml annotation on enum types #124

Open
seasox opened this issue Jan 24, 2019 · 0 comments
Open

Feature request: Allow @Xml annotation on enum types #124

seasox opened this issue Jan 24, 2019 · 0 comments

Comments

@seasox
Copy link

seasox commented Jan 24, 2019

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants