forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serialization (JSON-B vs Jackson) for Helidon SE Server generator (Op…
…enAPITools#27) Serialization (JSON-B vs Jackson) for Helidon SE Server generator Signed-off-by: aserkes <[email protected]>
- Loading branch information
Showing
10 changed files
with
237 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 51 additions & 5 deletions
56
.../openapi-generator/src/main/resources/java-helidon/server/libraries/se/enumClass.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,62 @@ | ||
|
||
public enum {{{datatypeWithEnum}}} { | ||
/** | ||
* {{^description}}Gets or Sets {{{name}}}{{/description}}{{{description}}} | ||
*/ | ||
{{#jsonb}} | ||
@JsonbTypeSerializer({{datatypeWithEnum}}.Serializer.class) | ||
@JsonbTypeDeserializer({{datatypeWithEnum}}.Deserializer.class) | ||
{{/jsonb}} | ||
{{>additionalEnumTypeAnnotations}}public enum {{{datatypeWithEnum}}} { | ||
{{#allowableValues}}{{#enumVars}}{{{name}}}({{{value}}}){{^-last}}, | ||
{{/-last}}{{#-last}};{{/-last}}{{/enumVars}}{{/allowableValues}} | ||
|
||
private String value; | ||
private {{{dataType}}} value; | ||
|
||
{{{datatypeWithEnum}}}(String value) { | ||
{{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}({{{dataType}}} value) { | ||
this.value = value; | ||
} | ||
|
||
{{#jackson}} | ||
@JsonValue | ||
{{/jackson}} | ||
public {{{dataType}}} getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return value; | ||
return String.valueOf(value); | ||
} | ||
|
||
{{#jsonb}} | ||
public static final class Deserializer implements JsonbDeserializer<{{datatypeWithEnum}}> { | ||
@Override | ||
public {{datatypeWithEnum}} deserialize(JsonParser parser, DeserializationContext ctx, Type rtType) { | ||
for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) { | ||
if (String.valueOf(b.value).equals(parser.getString())) { | ||
return b; | ||
} | ||
} | ||
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + parser.getString() + "'");{{/useNullForUnknownEnumValue}} | ||
} | ||
} | ||
|
||
public static final class Serializer implements JsonbSerializer<{{datatypeWithEnum}}> { | ||
@Override | ||
public void serialize({{datatypeWithEnum}} obj, JsonGenerator generator, SerializationContext ctx) { | ||
generator.write(obj.value); | ||
} | ||
} | ||
{{/jsonb}} | ||
|
||
{{#jackson}} | ||
@JsonCreator | ||
{{/jackson}} | ||
public static {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} fromValue(String text) { | ||
for ({{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}} b : {{{datatypeWithEnum}}}{{^datatypeWithEnum}}{{{classname}}}{{/datatypeWithEnum}}.values()) { | ||
if (String.valueOf(b.value).equals(text)) { | ||
return b; | ||
} | ||
} | ||
{{#useNullForUnknownEnumValue}}return null;{{/useNullForUnknownEnumValue}}{{^useNullForUnknownEnumValue}}throw new IllegalArgumentException("Unexpected value '" + text + "'");{{/useNullForUnknownEnumValue}} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...enapi-generator/src/main/resources/java-helidon/server/libraries/se/jsonProvider.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package {{apiPackage}}; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
public class JsonProvider { | ||
public static ObjectMapper objectMapper() { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.registerModule(new JavaTimeModule()); | ||
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); | ||
mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false); | ||
return mapper; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.