CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-dropwizard--dropwizard-jackson

Provides comprehensive Jackson JSON processing support for Dropwizard applications with pre-configured ObjectMapper instances and essential modules.

Pending
Overview
Eval results
Files

property-naming.mddocs/

Property Naming

Dropwizard Jackson provides flexible property naming support that allows classes to use either camelCase or snake_case JSON field names based on annotations.

AnnotationSensitivePropertyNamingStrategy

A property naming strategy that conditionally applies snake_case naming based on the presence of the @JsonSnakeCase annotation.

public class AnnotationSensitivePropertyNamingStrategy extends PropertyNamingStrategy {
    public String nameForConstructorParameter(MapperConfig<?> config, 
                                              AnnotatedParameter ctorParam, 
                                              String defaultName)
    public String nameForField(MapperConfig<?> config, 
                               AnnotatedField field, 
                               String defaultName)
    public String nameForGetterMethod(MapperConfig<?> config, 
                                      AnnotatedMethod method, 
                                      String defaultName)
    public String nameForSetterMethod(MapperConfig<?> config, 
                                      AnnotatedMethod method, 
                                      String defaultName)
}

Behavior:

  • Annotated Classes: Uses snake_case naming for classes annotated with @JsonSnakeCase
  • Default Classes: Uses default Jackson naming (typically camelCase) for non-annotated classes
  • Null Safety: Handles null parameters gracefully, returning the default name

JsonSnakeCase Annotation

Marker annotation that indicates a class should use snake_case property names in JSON serialization.

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonSnakeCase {
}

Properties:

  • Target: Can only be applied to classes/interfaces (ElementType.TYPE)
  • Retention: Available at runtime for reflection
  • Jackson Integration: Recognized as a Jackson annotation

Usage Examples

Basic Snake Case Usage

@JsonSnakeCase
public class UserPreferences {
    private String displayName;
    private boolean enableNotifications;
    private int maxRetryCount;
    
    // constructors, getters, setters...
}

ObjectMapper mapper = Jackson.newObjectMapper();
UserPreferences prefs = new UserPreferences("John Doe", true, 3);

String json = mapper.writeValueAsString(prefs);
// Result: {"display_name":"John Doe","enable_notifications":true,"max_retry_count":3}

Mixed Naming Strategies

// This class uses default camelCase
public class StandardConfig {
    private String serverName;
    private int portNumber;
    
    // constructors, getters, setters...
}

// This class uses snake_case
@JsonSnakeCase
public class DatabaseConfig {
    private String connectionUrl;
    private int maxConnections;
    
    // constructors, getters, setters...
}

ObjectMapper mapper = Jackson.newObjectMapper();

StandardConfig standard = new StandardConfig("api-server", 8080);
DatabaseConfig database = new DatabaseConfig("jdbc:postgresql://localhost/db", 10);

String standardJson = mapper.writeValueAsString(standard);
// Result: {"serverName":"api-server","portNumber":8080}

String databaseJson = mapper.writeValueAsString(database);
// Result: {"connection_url":"jdbc:postgresql://localhost/db","max_connections":10}

Constructor Parameter Handling

@JsonSnakeCase
public class ApiKey {
    private final String keyValue;
    private final long createdAt;
    
    public ApiKey(@JsonProperty("key_value") String keyValue,
                  @JsonProperty("created_at") long createdAt) {
        this.keyValue = keyValue;
        this.createdAt = createdAt;
    }
    
    // getters...
}

ObjectMapper mapper = Jackson.newObjectMapper();

// Both serialization and deserialization use snake_case
String json = "{\"key_value\":\"abc123\",\"created_at\":1640995200000}";
ApiKey key = mapper.readValue(json, ApiKey.class);

String serialized = mapper.writeValueAsString(key);
// Result: {"key_value":"abc123","created_at":1640995200000}

Integration with ObjectMapper

The naming strategy is automatically configured when using Jackson factory methods:

ObjectMapper mapper = Jackson.newObjectMapper();
// AnnotationSensitivePropertyNamingStrategy is automatically set

ObjectMapper minimal = Jackson.newMinimalObjectMapper();
// Does NOT include the custom naming strategy (uses default)

Compatibility

The naming strategy works with:

  • Field-based serialization/deserialization
  • Getter/Setter method-based serialization/deserialization
  • Constructor parameter deserialization with preserved parameter names
  • Mixed approaches within the same application

Note: The strategy only affects classes directly annotated with @JsonSnakeCase. Nested objects follow their own annotation rules.

Install with Tessl CLI

npx tessl i tessl/maven-io-dropwizard--dropwizard-jackson

docs

index.md

jackson-modules.md

object-mapper-factory.md

property-naming.md

subtype-discovery.md

tile.json