Skip to content

Instantly share code, notes, and snippets.

@rasmusfaber
Created May 28, 2020 17:36
Show Gist options
  • Save rasmusfaber/462f967e0dcab1b0a68622d5c811f0d8 to your computer and use it in GitHub Desktop.
Save rasmusfaber/462f967e0dcab1b0a68622d5c811f0d8 to your computer and use it in GitHub Desktop.
Json Mixin
package xyz.faber;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import java.io.StringWriter;
public class JsonTest {
@Test
public void testCustom() 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());
}
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment