Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Configure JSON-B to serialize snake_case globally.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Jun 19, 2024
1 parent 6e72cab commit e00948d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.schulung.sample.quarkus.boundary;

import jakarta.json.bind.annotation.JsonbProperty;
import jakarta.json.bind.annotation.JsonbTransient;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
Expand All @@ -25,8 +24,8 @@ public class CustomerDto {
@Size(min = 3, max = 100)
@NotNull
private String name;
@JsonbProperty("birth_date") // TODO -> use snake_case globally?
private LocalDate birthdate;
//@JsonbProperty("birth_date")
private LocalDate birthDate;
@Pattern(regexp = "active|locked|disabled")
@NotNull
private String state;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package de.schulung.sample.quarkus.boundary;

import de.schulung.sample.quarkus.domain.Customer;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(componentModel = "cdi")
public interface CustomerDtoMapper {

@Mapping(source = "birthDate", target = "birthdate")
Customer map(CustomerDto source);

@InheritInverseConfiguration
CustomerDto map(Customer source);

default String mapState(Customer.CustomerState source) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.schulung.sample.quarkus.boundary.config;

import io.quarkus.jsonb.JsonbConfigCustomizer;
import jakarta.enterprise.context.Dependent;
import jakarta.json.bind.JsonbConfig;
import jakarta.json.bind.config.PropertyNamingStrategy;

@Dependent
public class JsonSnakeCaseConfiguration implements JsonbConfigCustomizer {

@Override
public void customize(JsonbConfig jsonbConfig) {
jsonbConfig.withPropertyNamingStrategy(
PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES
);
}

}

0 comments on commit e00948d

Please sign in to comment.