public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
throws IOException
{
ClassAnnotationKey key = new ClassAnnotationKey(type, annotations);
JsonEndpointConfig endpoint;
endpoint = _writers.get(key);
// not yet resolved (or not cached any more)? Resolve!
if (endpoint == null) {
ObjectMapper mapper = locateMapper(type, mediaType);
endpoint = _configForWriting(mapper, annotations);
// and cache for future reuse
_writers.put(key, endpoint);
}
ObjectWriter writer = endpoint.getWriter();
boolean withIndentOutput = false; // no way to replace _serializationConfig
// we can't cache this.
if (annotations != null) {
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(Formatted.class)) {
withIndentOutput = true;
break;
}
}
}
/* 27-Feb-2009, tatu: Where can we find desired encoding? Within
* HTTP headers?
*/
JsonEncoding enc = findEncoding(mediaType, httpHeaders);
JsonGenerator jg = writer.getFactory().createGenerator(entityStream, enc);
try {
// Want indentation?
if (writer.isEnabled(SerializationFeature.INDENT_OUTPUT) || withIndentOutput) {
jg.useDefaultPrettyPrinter();
}
// 04-Mar-2010, tatu: How about type we were given? (if any)
JavaType rootType = null;
if (genericType != null && value != null) {
/* 10-Jan-2011, tatu: as per [JACKSON-456], it's not safe to just force root
* type since it prevents polymorphic type serialization. Since we really
* just need this for generics, let's only use generic type if it's truly
* generic.
*/
if (genericType.getClass() != Class.class) { // generic types are other impls of 'java.lang.reflect.Type'
/* This is still not exactly right; should root type be further
* specialized with 'value.getClass()'? Let's see how well this works before
* trying to come up with more complete solution.
*/
rootType = writer.getTypeFactory().constructType(genericType);
/* 26-Feb-2011, tatu: To help with [JACKSON-518], we better recognize cases where
* type degenerates back into "Object.class" (as is the case with plain TypeVariable,
* for example), and not use that.
*/
if (rootType.getRawClass() == Object.class) {
rootType = null;
}
}
}
// Most of the configuration now handled through EndpointConfig, ObjectWriter
// but we may need to force root type:
if (rootType != null) {
writer = writer.withType(rootType);
}
value = endpoint.modifyBeforeWrite(value);
writer.writeValue(jg, value);
} finally {
jg.close();
}
}