Created
February 27, 2009 23:22
-
-
Save cowtowncoder/71767 to your computer and use it in GitHub Desktop.
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 test; | |
import java.io.InputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.lang.annotation.Annotation; | |
import java.lang.reflect.Type; | |
import javax.ws.rs.*; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.MultivaluedMap; | |
import javax.ws.rs.ext.MessageBodyReader; | |
import javax.ws.rs.ext.MessageBodyWriter; | |
import javax.ws.rs.ext.Provider; | |
import org.codehaus.jackson.*; | |
import org.codehaus.jackson.type.JavaType; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.codehaus.jackson.map.type.TypeFactory; | |
@Provider | |
@Consumes(MediaType.APPLICATION_JSON) | |
@Produces(MediaType.APPLICATION_JSON) | |
public class JsonProvider | |
implements | |
MessageBodyReader<Object>, MessageBodyWriter<Object> | |
{ | |
JsonFactory _jsonFactory; | |
ObjectMapper _objectMapper; | |
/** | |
* Default constructor that is usually used when instances are to | |
* be constructed from given class. If so, an instance of | |
* {@link ObjectMapper} is constructed with default settings. | |
*/ | |
public JsonProvider() | |
{ | |
this(new ObjectMapper()); | |
} | |
public JsonProvider(ObjectMapper m) | |
{ | |
_objectMapper = m; | |
_jsonFactory = m.getJsonFactory(); | |
/* note: we have to prevent underlying parser/generator from | |
* closing the stream we deal with, so: | |
*/ | |
_jsonFactory.disableParserFeature(JsonParser.Feature.AUTO_CLOSE_SOURCE); | |
_jsonFactory.disableGeneratorFeature(JsonGenerator.Feature.AUTO_CLOSE_TARGET); | |
} | |
public ObjectMapper getObjectMapper() { return _objectMapper; } | |
public void setObjectMapper(ObjectMapper m) { _objectMapper = m; } | |
/* | |
//////////////////////////////////////////////////// | |
// MessageBodyReader impl | |
//////////////////////////////////////////////////// | |
*/ | |
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) | |
{ | |
/* To know for sure we'd need to find a deserializer; for now, | |
* let's claim we can handle anything. | |
*/ | |
return (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)); | |
} | |
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, InputStream entityStream) | |
throws IOException | |
{ | |
JavaType jtype = TypeFactory.fromType(genericType); | |
JsonParser jp = _jsonFactory.createJsonParser(entityStream); | |
return _objectMapper.readValue(jp, jtype); | |
} | |
/* | |
//////////////////////////////////////////////////// | |
// MessageBodyWriter impl | |
//////////////////////////////////////////////////// | |
*/ | |
public long getSize(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) | |
{ | |
return -1; | |
} | |
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) | |
{ | |
/* To know for sure we'd need to find a serializer; for now, | |
* let's claim we can handle anything. | |
*/ | |
return (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)); | |
} | |
public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream) | |
throws IOException | |
{ | |
/* 27-Feb-2009, tatu: Where can we find desired encoding? Within | |
* http headers? | |
*/ | |
JsonGenerator jg = _jsonFactory.createJsonGenerator(entityStream, JsonEncoding.UTF8); | |
_objectMapper.writeValue(jg, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment