Skip to content

Instantly share code, notes, and snippets.

@573
Forked from rasmusfaber/JsonTest.java
Created February 5, 2024 13:56
Show Gist options
  • Save 573/5c6e10c422c80b9e51d409ffbcb6f672 to your computer and use it in GitHub Desktop.
Save 573/5c6e10c422c80b9e51d409ffbcb6f672 to your computer and use it in GitHub Desktop.
Rename field using mixin or naming-strategy
package xyz.faber;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
import org.junit.Test;
import java.io.IOException;
import java.io.StringWriter;
public class JsonTest {
@Test
public void testWithMixin() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.addMixIn(CustomClass.class, CustomClassMixin.class);
CustomClass cc = new CustomClass("id", 7);
StringWriter sw = new StringWriter();
mapper.writeValue(sw, cc);
System.out.println(sw.toString());
}
@Test
public void testWithNamingStrategy() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new IdNamingStrategy("s_id"));
CustomClass cc = new CustomClass("id", 7);
StringWriter sw = new StringWriter();
mapper.writeValue(sw, cc);
System.out.println(sw.toString());
}
private static class CustomClassMixin {
@JsonProperty("s_id")
private String id;
}
private static class CustomClass {
private String id;
private int foo;
public CustomClass() {
}
public CustomClass(String id, int foo) {
this.id = id;
this.foo = foo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getFoo() {
return foo;
}
public void setFoo(int foo) {
this.foo = foo;
}
}
public static class IdNamingStrategy extends PropertyNamingStrategy {
private final PropertyNamingStrategy inner;
private final String newIdPropertyName;
public IdNamingStrategy(String newIdPropertyName) {
this(PropertyNamingStrategy.LOWER_CAMEL_CASE, newIdPropertyName);
}
public IdNamingStrategy(PropertyNamingStrategy inner, String newIdPropertyName) {
this.inner = inner;
this.newIdPropertyName = newIdPropertyName;
}
private String translate(String propertyName) {
if ("id".equals(propertyName)) {
return newIdPropertyName;
} else {
return propertyName;
}
}
@Override
public String nameForField(MapperConfig<?> config, AnnotatedField field, String defaultName) {
return inner.nameForField(config, field, translate(defaultName));
}
@Override
public String nameForGetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return inner.nameForGetterMethod(config, method, translate(defaultName));
}
@Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method, String defaultName) {
return inner.nameForSetterMethod(config, method, translate(defaultName));
}
@Override
public String nameForConstructorParameter(MapperConfig<?> config, AnnotatedParameter ctorParam, String defaultName) {
return inner.nameForConstructorParameter(config, ctorParam, translate(defaultName));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment