Created
February 29, 2016 06:24
-
-
Save tmarcus87/fc33799409a61558a17b to your computer and use it in GitHub Desktop.
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
package entity; | |
import com.fasterxml.jackson.annotation.JsonValue; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | |
import com.fasterxml.jackson.databind.SerializationFeature; | |
import com.google.common.collect.ImmutableMap; | |
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import java.util.Map; | |
/** | |
* @author ono_takahiko | |
* @since 16/02/29 | |
*/ | |
public class ObjectMapperTest { | |
private static final MyEntity ENTITY1 = | |
new MyEntity(MyEnum.TYPE1, "This is a 1st sample entity.", | |
ImmutableMap.of(MyEnum.TYPE1, "type1")); | |
private static final MyEntity ENTITY2 = | |
new MyEntity(MyEnum.TYPE2, "This is a 2nd sample entity.", | |
ImmutableMap.of(MyEnum.TYPE2, "type2")); | |
private ObjectMapper mapper; | |
public ObjectMapperTest(ObjectMapper mapper) { | |
this.mapper = mapper; | |
} | |
public void exec() throws JsonProcessingException { | |
System.out.println(mapper.writeValueAsString(ENTITY1)); | |
System.out.println(mapper.writeValueAsString(ENTITY2)); | |
System.out.println("----"); | |
} | |
public static void main(String... args) throws Exception { | |
ObjectMapper mapper1 = new ObjectMapper(); | |
ObjectMapper mapper2 = new ObjectMapper(); | |
mapper2.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); | |
mapper2.setPropertyNamingStrategy( | |
PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); | |
ObjectMapper mapper3 = new ObjectMapper(); | |
mapper3.configure(SerializationFeature.WRITE_ENUMS_USING_INDEX, true); | |
mapper3.setPropertyNamingStrategy(PropertyNamingStrategy.PASCAL_CASE_TO_CAMEL_CASE); | |
new ObjectMapperTest(mapper1).exec(); | |
new ObjectMapperTest(mapper2).exec(); | |
new ObjectMapperTest(mapper3).exec(); | |
} | |
@AllArgsConstructor | |
@Getter | |
public static class MyEntity { | |
private MyEnum type; | |
private String complexNameValue; | |
private Map<MyEnum, String> enumIndexedMap; | |
} | |
public static enum MyEnum { | |
TYPE1("type1"), | |
TYPE2("type2") | |
; | |
private String code; | |
private MyEnum(String code) { | |
this.code = code; | |
} | |
@JsonValue | |
public String getCode() { | |
return code; | |
} | |
} | |
} | |
/* | |
{"type":"type1","complexNameValue":"This is a 1st sample entity.","enumIndexedMap":{"TYPE1":"type1"}} | |
{"type":"type2","complexNameValue":"This is a 2nd sample entity.","enumIndexedMap":{"TYPE2":"type2"}} | |
---- | |
{"type":"type1","complex_name_value":"This is a 1st sample entity.","enum_indexed_map":{"TYPE1":"type1"}} | |
{"type":"type2","complex_name_value":"This is a 2nd sample entity.","enum_indexed_map":{"TYPE2":"type2"}} | |
---- | |
{"Type":"type1","ComplexNameValue":"This is a 1st sample entity.","EnumIndexedMap":{"TYPE1":"type1"}} | |
{"Type":"type2","ComplexNameValue":"This is a 2nd sample entity.","EnumIndexedMap":{"TYPE2":"type2"}} | |
---- | |
*/ |
Author
tmarcus87
commented
Feb 29, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment