Skip to content

Instantly share code, notes, and snippets.

@shoooone
Last active February 4, 2019 05:29
Show Gist options
  • Save shoooone/2b90139cf34a9dcf47c0528a1b724cbd to your computer and use it in GitHub Desktop.
Save shoooone/2b90139cf34a9dcf47c0528a1b724cbd to your computer and use it in GitHub Desktop.
JacksonのPropertyNamingStrategyまとめ ref: https://qiita.com/Shoone/items/5d23dfcc8687ba2305fe
@SneakyThrows
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
print("PropertyNamingStrategy", mapper.writeValueAsString(new CamelCase()));
print("UpperCamelCaseStrategy", mapper.writeValueAsString(new PascalCase()));
print("SnakeCaseStrategy", mapper.writeValueAsString(new SnakeCase()));
print("KebabCaseStrategy", mapper.writeValueAsString(new KebabCase()));
print("LowerCaseStrategy", mapper.writeValueAsString(new LowerCase()));
}
private static void print(String strategy, String json) {
String format = "%-23s: %s";
System.out.println(String.format(format, strategy, json));
}
@Getter
// デフォルトはこれ。アノテーションを省略しても同じ
@JsonNaming(PropertyNamingStrategy.class)
static class CamelCase {
private String testString = "hoge";
}
@Getter
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
static class PascalCase {
private String testString = "fuga";
}
@Getter
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
static class SnakeCase {
private String testString = "piyo";
}
@Getter
@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy.class)
static class KebabCase {
private String testString = "hogera";
}
@Getter
@JsonNaming(PropertyNamingStrategy.LowerCaseStrategy.class)
static class LowerCase {
private String testString = "hogehoge";
}
PropertyNamingStrategy : {"testString":"hoge"}
UpperCamelCaseStrategy : {"TestString":"fuga"}
SnakeCaseStrategy : {"test_string":"piyo"}
KebabCaseStrategy : {"test-string":"hogera"}
LowerCaseStrategy : {"teststring":"hogehoge"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment