Skip to content

Instantly share code, notes, and snippets.

@cdmatta
Created April 14, 2018 21:14
Show Gist options
  • Save cdmatta/5768a0e693bdacbe4b44d9c893fae9d5 to your computer and use it in GitHub Desktop.
Save cdmatta/5768a0e693bdacbe4b44d9c893fae9d5 to your computer and use it in GitHub Desktop.
Jackson custom annotations. Create alternate serialization of the object maintaining the original serialization intact.
package jackson;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class Check {
public static void main(String[] args) throws Exception {
Person person = new Person("George G. Shelton", "4444555566667890", "010199-111T");
System.out.println("To String = " + person);
ObjectWriter jsonWriter = new ObjectMapper().writer();
System.out.println("Regular json serialize = " + jsonWriter.writeValueAsString(person));
ObjectWriter customWriter = new ObjectMapper().setAnnotationIntrospector(new CustomAnnotationIntrospector()).writer();
System.out.println("Custom annotation json serialize = " + customWriter.writeValueAsString(person));
}
}
package jackson;
import com.fasterxml.jackson.databind.util.StdConverter;
import org.apache.commons.lang.StringUtils;
public class CreditCardObfuscator extends StdConverter<String, String> {
@Override
public String convert(String value) {
if (StringUtils.isBlank(value)) {
return value;
}
return StringUtils.repeat("*", 12) + value.substring(12);
}
}
package jackson;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
public class CustomAnnotationIntrospector extends JacksonAnnotationIntrospector {
@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
if (m.hasAnnotation(HideValue.class)) {
return true;
}
return super.hasIgnoreMarker(m);
}
@Override
public Object findSerializationConverter(Annotated a) {
if (a.hasAnnotation(ObfuscateValue.class)) {
return a.getAnnotation(ObfuscateValue.class).value();
}
return super.findSerializationConverter(a);
}
}
package jackson;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HideValue {
}
package jackson;
import com.fasterxml.jackson.databind.util.StdConverter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ObfuscateValue {
Class<? extends StdConverter> value();
}
package jackson;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Person {
private String name;
@ObfuscateValue(CreditCardObfuscator.class)
private String creditCardNumber;
@HideValue
private String ssn;
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.cm</groupId>
<artifactId>jackson-custom-serialize</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<compilerArgument>-Xlint:all</compilerArgument>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment