Created
October 22, 2015 00:19
-
-
Save aasokan/e928e3939650bb37d19b to your computer and use it in GitHub Desktop.
Gists for JsonSnakeCase Annotation (From Dropwizard)
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 com.yammer.dropwizard.json; | |
import com.fasterxml.jackson.databind.PropertyNamingStrategy; | |
import com.fasterxml.jackson.databind.cfg.MapperConfig; | |
import com.fasterxml.jackson.databind.introspect.AnnotatedField; | |
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; | |
import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; | |
/** | |
* A {@link PropertyNamingStrategy} implementation which, if the declaring class of a property is | |
* annotated with {@link JsonSnakeCase}, uses a {@link LowerCaseWithUnderscoresStrategy}, and uses | |
* the default {@link PropertyNamingStrategy} otherwise. | |
*/ | |
public class AnnotationSensitivePropertyNamingStrategy extends PropertyNamingStrategy { | |
private static final long serialVersionUID = -1372862028366311230L; | |
private final LowerCaseWithUnderscoresStrategy snakeCase; | |
public AnnotationSensitivePropertyNamingStrategy() { | |
super(); | |
this.snakeCase = new LowerCaseWithUnderscoresStrategy(); | |
} | |
@Override | |
public String nameForConstructorParameter(MapperConfig<?> config, | |
AnnotatedParameter ctorParam, | |
String defaultName) { | |
if (ctorParam.getDeclaringClass().isAnnotationPresent(JsonSnakeCase.class)) { | |
return snakeCase.nameForConstructorParameter(config, ctorParam, defaultName); | |
} | |
return super.nameForConstructorParameter(config, ctorParam, defaultName); | |
} | |
@Override | |
public String nameForField(MapperConfig<?> config, | |
AnnotatedField field, | |
String defaultName) { | |
if (field.getDeclaringClass().isAnnotationPresent(JsonSnakeCase.class)) { | |
return snakeCase.nameForField(config, field, defaultName); | |
} | |
return super.nameForField(config, field, defaultName); | |
} | |
@Override | |
public String nameForGetterMethod(MapperConfig<?> config, | |
AnnotatedMethod method, | |
String defaultName) { | |
if (method.getDeclaringClass().isAnnotationPresent(JsonSnakeCase.class)) { | |
return snakeCase.nameForGetterMethod(config, method, defaultName); | |
} | |
return super.nameForGetterMethod(config, method, defaultName); | |
} | |
@Override | |
public String nameForSetterMethod(MapperConfig<?> config, | |
AnnotatedMethod method, | |
String defaultName) { | |
if (method.getDeclaringClass().isAnnotationPresent(JsonSnakeCase.class)) { | |
return snakeCase.nameForSetterMethod(config, method, defaultName); | |
} | |
return super.nameForSetterMethod(config, method, defaultName); | |
} | |
} |
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 com.yammer.dropwizard.json; | |
import com.fasterxml.jackson.annotation.JacksonAnnotation; | |
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
/** | |
* Marker annotation which indicates that the annotated case class should be | |
* serialized and deserialized using {@code snake_case} JSON field names instead | |
* of {@code camelCase} field names. | |
*/ | |
@Target(ElementType.TYPE) | |
@Retention(RetentionPolicy.RUNTIME) | |
@JacksonAnnotation | |
public @interface JsonSnakeCase { | |
} |
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
private static ObjectMapper configure(ObjectMapper mapper) { | |
mapper.registerModule(new GuavaModule()); | |
mapper.registerModule(new LogbackModule()); | |
mapper.registerModule(new GuavaExtrasModule()); | |
mapper.registerModule(new JodaModule()); | |
mapper.registerModule(new AfterburnerModule()); | |
mapper.registerModule(new FuzzyEnumModule()); | |
mapper.registerModule(new Jdk7Module()); | |
mapper.setPropertyNamingStrategy(new AnnotationSensitivePropertyNamingStrategy()); | |
mapper.setSubtypeResolver(new DiscoverableSubtypeResolver()); | |
return mapper; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment