Skip to content

Instantly share code, notes, and snippets.

@varren
Created March 27, 2016 12:56
Show Gist options
  • Save varren/84ce830d07932b6a9c18 to your computer and use it in GitHub Desktop.
Save varren/84ce830d07932b6a9c18 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.io.IOException;
import java.util.*;
public class Main {
private static String json =
"[{\"email\": \"user@email\",\"roles\": [\"REGISTERED_USER\"]}]";
public static void main(String[] args) throws IOException{
System.out.println(json);
ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(Role.class, new RoleDeserializer());
simpleModule.addSerializer(Role.class, new RoleSerializer());
mapper.registerModule(simpleModule);
List<User> users = Arrays.asList(mapper.readValue(json, User[].class));
System.out.println(users);
String jsonInString = mapper.writeValueAsString(users);
System.out.println(jsonInString);
}
public static class RoleSerializer extends JsonSerializer {
@Override
public void serialize(Object value, JsonGenerator gen,
SerializerProvider serializers)
throws IOException, JsonProcessingException {
gen.writeString(((Role) value).getRole());
}
}
public static class RoleDeserializer extends JsonDeserializer {
@Override
public Role deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext)
throws IOException {
ObjectCodec oc = jsonParser.getCodec();
JsonNode node = oc.readTree(jsonParser);
Role role = new Role();
role.setRole(node.asText());
return role;
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public static class User {
@JsonProperty(value = "id")
private Long id;
@JsonProperty(value = "email")
private String email;
@JsonProperty(value = "password")
private String password;
@JsonProperty(value = "roles")
private List<Role> roles;
@Override
public String toString() {
String listString = "";
String delim = "";
for (Role s : roles) {
listString += delim + s ;
delim = ", ";
}
return "User{" +
"id=" + id +
", email='" + email + '\'' +
", password='" + password + '\'' +
", roles=" + listString +
'}';
}
}
public static class Role {
private String role;
public String getRole() {return role;}
public void setRole(String role) {this.role = role;}
@Override
public String toString() {
return "Role{role='" + role + "'}";
}
}
}
@varren
Copy link
Author

varren commented Mar 27, 2016

Output:

[{"email": "user@email","roles": ["REGISTERED_USER"]}]
[User{id=null, email='user@email', password='null', roles=Role{role='REGISTERED_USER'}}]
[{"email":"user@email","roles":["REGISTERED_USER"]}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment