You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@juneau.apache.org by "Steve Blackmon (JIRA)" <ji...@apache.org> on 2017/06/14 21:59:00 UTC

[jira] [Created] (JUNEAU-57) seamless serialization and parsing of string fields to/from enum

Steve Blackmon created JUNEAU-57:
------------------------------------

             Summary: seamless serialization and parsing of string fields to/from enum
                 Key: JUNEAU-57
                 URL: https://issues.apache.org/jira/browse/JUNEAU-57
             Project: Juneau
          Issue Type: Improvement
            Reporter: Steve Blackmon


i’m working to replace jackson with juneau, while maintaining the ability to define certain fields as enumerations rather than strings.

jackson ObjectMapper handles enums transparently, presumably juneau could too?

relevant piece of the pojo is below.

once this is working, the following test snippets should pass:

@Test
public void testEnumParse() throws Exception {
  String json = "{\"type\":\"message_create\"}";
  DirectMessageEvent event = JsonParser.DEFAULT.parse(json, DirectMessageEvent.class);
}

@Test
public void testEnumSerialize() throws Exception {
  DirectMessageEvent event = new DirectMessageEvent();
  event.setType(DirectMessageEvent.Type.MESSAGE_CREATE);
  String json = JsonSerializer.DEFAULT.serialize(event);
}

----

@Generated("org.jsonschema2pojo")
public class DirectMessageEvent implements Serializable
{

@JsonProperty("type")
@BeanProperty(name = "type")
private DirectMessageEvent.Type type = DirectMessageEvent.Type.fromValue("message_create");

@JsonProperty("type")
@BeanProperty(name = "type")
public DirectMessageEvent.Type getType() {
    return type;
}

@JsonProperty("type")
@BeanProperty(name = "type")
public void setType(DirectMessageEvent.Type type) {
    this.type = type;
}

@Generated("org.jsonschema2pojo")
public static enum Type {

    MESSAGE_CREATE("message_create");
    private final String value;
    private static Map<String, DirectMessageEvent.Type> constants = new HashMap<String, DirectMessageEvent.Type>();

    static {
        for (DirectMessageEvent.Type c: values()) {
            constants.put(c.value, c);
        }
    }

    private Type(String value) {
        this.value = value;
    }

    @JsonValue
    @Override
    public String toString() {
        return this.value;
    }

    @JsonCreator
    public static DirectMessageEvent.Type fromValue(String value) {
        DirectMessageEvent.Type constant = constants.get(value);
        if (constant == null) {
            throw new IllegalArgumentException(value);
        } else {
            return constant;
        }
    }

}

}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)