juneau-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Bognar (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (JUNEAU-57) seamless serialization and parsing of string fields to/from enum
Date Thu, 15 Jun 2017 00:49:00 GMT

    [ https://issues.apache.org/jira/browse/JUNEAU-57?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16049866#comment-16049866
] 

James Bognar commented on JUNEAU-57:
------------------------------------

Added fromValue() to the list of method names to look for when converting back from strings.

> 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
>             Fix For: 6.2.1
>
>   Original Estimate: 1h
>  Remaining Estimate: 1h
>
> 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