Created
April 9, 2015 16:07
-
-
Save makasprzak/f5a641e1b5d996e908de to your computer and use it in GitHub Desktop.
example with JsonProperty 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.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.mockito.runners.MockitoJUnitRunner; | |
import static makasprzak.stackoverflow.JsonPropertyTest.User.Builder.user; | |
import static org.fest.assertions.Assertions.assertThat; | |
@RunWith(MockitoJUnitRunner.class) | |
public class JsonPropertyTest { | |
private final ObjectMapper objectMapper = new ObjectMapper(); | |
@Test | |
public void shouldSerializeWithUpperCasedUsername() throws Exception { | |
String serialized = objectMapper.writeValueAsString(user() | |
.withId(1L) | |
.withUsername("macias") | |
.build()); | |
System.out.println(serialized); | |
assertThat(serialized).contains("MACIAS").doesNotContain("macias").contains("1"); | |
} | |
@Test | |
public void shouldSerializeWithLowerCasedUsername() throws Exception { | |
String serialized = objectMapper.writeValueAsString(user() | |
.withId(0L) | |
.withUsername("macias") | |
.build()); | |
System.out.println(serialized); | |
assertThat(serialized).contains("macias").doesNotContain("MACIAS").contains("0").doesNotContain("uppercase"); | |
} | |
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; | |
} | |
@JsonProperty("username") | |
public String getUppercaseUsername() { | |
return this.id % 2 == 0 ? username : username.toUpperCase(); | |
} | |
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