juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steve Blackmon (JIRA)" <j...@apache.org>
Subject [jira] [Created] (JUNEAU-57) seamless serialization and parsing of string fields to/from enum
Date Wed, 14 Jun 2017 21:59:00 GMT
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)

Mime
View raw message