Created
April 9, 2015 16:06
-
-
Save makasprzak/5257e5c92139e28003c0 to your computer and use it in GitHub Desktop.
exapple with custom serializer for http://stackoverflow.com/questions/29540396/jackson-custom-serialization-getter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package makasprzak.stackoverflow; | |
import com.fasterxml.jackson.core.JsonGenerator; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.JsonSerializer; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.SerializerProvider; | |
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import com.fasterxml.jackson.databind.util.Converter; | |
import org.fest.assertions.Assertions; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import java.io.IOException; | |
import static makasprzak.stackoverflow.CustomSerializerTest.User.Builder.user; | |
import static org.fest.assertions.Assertions.assertThat; | |
@RunWith(MockitoJUnitRunner.class) | |
public class CustomSerializerTest { | |
private final ObjectMapper objectMapper = new ObjectMapper(); | |
@Test | |
public void shouldSerializeWithUpperCasedUsername() throws Exception { | |
assertThat(objectMapper.writeValueAsString(user() | |
.withId(1L) | |
.withUsername("macias") | |
.build())).contains("MACIAS").doesNotContain("macias"); | |
} | |
@Test | |
public void shouldSerializeWithLowerCasedUsername() throws Exception { | |
assertThat(objectMapper.writeValueAsString(user() | |
.withId(0L) | |
.withUsername("macias") | |
.build())).contains("macias").doesNotContain("MACIAS"); | |
} | |
public static class UserSerializer extends JsonSerializer<User> { | |
public static class UserWrapper { | |
public Long id; | |
public String username; | |
} | |
@Override | |
public void serialize(User user, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { | |
UserWrapper userWrapper = new UserWrapper(); | |
userWrapper.id = user.getId(); | |
userWrapper.username = user.getId() % 2 == 0 ? user.getUsername() : user.getUsername().toUpperCase(); | |
serializerProvider.defaultSerializeValue(userWrapper,jsonGenerator); | |
} | |
} | |
@JsonSerialize(using = UserSerializer.class) | |
public static class User { | |
private Long id; | |
private String username; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id = id; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public static interface IdStep { | |
UsernameStep withId(Long id); | |
} | |
public static interface UsernameStep { | |
BuildStep withUsername(String username); | |
} | |
public static interface BuildStep { | |
User build(); | |
} | |
public static class Builder implements IdStep, UsernameStep, BuildStep { | |
private Long id; | |
private String username; | |
private Builder() { | |
} | |
public static IdStep user() { | |
return new Builder(); | |
} | |
@Override | |
public UsernameStep withId(Long id) { | |
this.id = id; | |
return this; | |
} | |
@Override | |
public BuildStep withUsername(String username) { | |
this.username = username; | |
return this; | |
} | |
@Override | |
public User build() { | |
User user = new User(); | |
user.setId(this.id); | |
user.setUsername(this.username); | |
return user; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment