Skip to content

Instantly share code, notes, and snippets.

@kjunine
Created May 1, 2013 02:35
Show Gist options
  • Select an option

  • Save kjunine/5493402 to your computer and use it in GitHub Desktop.

Select an option

Save kjunine/5493402 to your computer and use it in GitHub Desktop.
JacksonObjectMapperUtils for JSON conversion in Spring MVC
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.DeserializationContext;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.module.SimpleModule;
import org.codehaus.jackson.node.ObjectNode;
public class JacksonObjectMapperUtils {
public static ObjectMapper newObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("json", new Version(1, 0, 0,
null));
module.addDeserializer(SampleConnection.class,
new SampleConnectionDeserializer());
module.addDeserializer(SampleAuthentication.class,
new SampleAuthenticationDeserializer());
mapper.registerModule(module);
return mapper;
}
public static class SampleConnectionDeserializer extends
JsonDeserializer<SampleConnection> {
@Override
public SampleConnection deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException,
JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode root = (ObjectNode) mapper.readTree(jp);
SampleProtocolType type = SampleProtocolType.valueOf(root.get(
"protocol").asText());
switch (type) {
case TCP:
return mapper
.readValue(root, SampleTCPConnection.class);
case UDP:
return mapper
.readValue(root, SampleUDPConnection.class);
default:
throw new IllegalArgumentException("Invalid type: " + type);
}
}
}
public static class SampleAuthenticationDeserializer extends
JsonDeserializer<SampleAuthentication> {
@Override
public SampleAuthentication deserialize(JsonParser jp,
DeserializationContext ctxt) throws IOException,
JsonProcessingException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode root = (ObjectNode) mapper.readTree(jp);
SampleAuthenticationType type = SampleAuthenticationType
.valueOf(root.get("type").asText());
switch (type) {
case ANONYMOUS:
return mapper.readValue(root,
SampleAnonymousAuthentication.class);
case PASSWORD:
return mapper.readValue(root,
SamplePasswordAuthentication.class);
case CUSTOM:
return mapper.readValue(root,
SampleCustomAuthentication.class);
default:
throw new IllegalArgumentException("Invalid type: " + type);
}
}
}
}
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper">
<bean class="package.JacksonObjectMapperUtils"
factory-method="newObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment