Skip to content

Instantly share code, notes, and snippets.

@zacscoding
Created December 4, 2018 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zacscoding/c52957e37a82be7cba525ab5da14bc7f to your computer and use it in GitHub Desktop.
Save zacscoding/c52957e37a82be7cba525ab5da14bc7f to your computer and use it in GitHub Desktop.
ObjectMapper naming strategy test
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.junit.Test;
/**
* @author zacconding
* @Date 2018-12-04
* @GitHub : https://github.com/zacscoding
*/
public class ObjectMapperTest {
@Test
public void objectMapperNamingStrategy() throws Exception {
PropertyNamingStrategy[] strategies = new PropertyNamingStrategy[] {
PropertyNamingStrategy.LOWER_CASE
, PropertyNamingStrategy.SNAKE_CASE
, PropertyNamingStrategy.KEBAB_CASE
, PropertyNamingStrategy.LOWER_CAMEL_CASE
, PropertyNamingStrategy.UPPER_CAMEL_CASE
};
for (PropertyNamingStrategy strategy : strategies) {
System.out.print(">> Test " + strategy.getClass().getSimpleName() + " : ");
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(strategy);
TempClass inst = new TempClass("hivava", "hivavava");
System.out.println("\t" + mapper.writeValueAsString(inst));
}
/*
output
>> Test LowerCaseStrategy : {"testname":"hivava","test_value":"hivavava"}
>> Test SnakeCaseStrategy : {"test_name":"hivava","test_value":"hivavava"}
>> Test KebabCaseStrategy : {"test-name":"hivava","test_value":"hivavava"}
>> Test PropertyNamingStrategy : {"testName":"hivava","test_value":"hivavava"}
>> Test UpperCamelCaseStrategy : {"TestName":"hivava","Test_value":"hivavava"}
*/
}
@Getter
@Setter
@AllArgsConstructor
public static class TempClass {
private String testName;
private String test_value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment