Skip to content

Instantly share code, notes, and snippets.

@yamsellem
Created July 21, 2012 13:30
Show Gist options
  • Save yamsellem/3155831 to your computer and use it in GitHub Desktop.
Save yamsellem/3155831 to your computer and use it in GitHub Desktop.
Jongo Jackson provider to make Jersey serves ObjectId as String
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.ANY;
import static org.codehaus.jackson.map.DeserializationConfig.Feature.AUTO_DETECT_SETTERS;
import static org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES;
import static org.codehaus.jackson.map.SerializationConfig.Feature.AUTO_DETECT_GETTERS;
import static org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion.NON_DEFAULT;
import java.io.IOException;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.bson.types.ObjectId;
import org.codehaus.jackson.JsonGenerator;
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.JsonSerializer;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.introspect.VisibilityChecker.Std;
import org.codehaus.jackson.map.module.SimpleModule;
@Provider
public class JacksonMapperProvider implements ContextResolver<ObjectMapper> {
private final ObjectMapper mapper;
public JacksonMapperProvider() {
mapper = createMapper();
}
public ObjectMapper getContext(Class<?> type) {
return mapper;
}
private static ObjectMapper createMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(AUTO_DETECT_GETTERS, false);
mapper.configure(AUTO_DETECT_SETTERS, false);
mapper.setDeserializationConfig(mapper.getDeserializationConfig().without(FAIL_ON_UNKNOWN_PROPERTIES));
mapper.setSerializationConfig(mapper.getSerializationConfig().withSerializationInclusion(NON_DEFAULT));
mapper.setVisibilityChecker(Std.defaultInstance().withFieldVisibility(ANY));
mapper.registerModule(new SimpleModule("jersey", new Version(1, 0, 0, null)) //
.addSerializer(_id, _idSerializer()) //
.addDeserializer(_id, _idDeserializer()));
return mapper;
}
private static Class<ObjectId> _id = ObjectId.class;
private static JsonDeserializer<ObjectId> _idDeserializer() {
return new JsonDeserializer<ObjectId>() {
public ObjectId deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
return new ObjectId(jp.readValueAs(String.class));
}
};
}
private static JsonSerializer<Object> _idSerializer() {
return new JsonSerializer<Object>() {
public void serialize(Object obj, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
jsonGenerator.writeString(obj == null ? null : obj.toString());
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment