@Override public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { String className = value.getClass().getName(); if (value instanceof Collection) { String collectionJson = writeCollection((Collection) value, customObjectMapper); jgen.writeRawValue(collectionJson); } else if (value instanceof Map) { String mapJson = writeMap((Map) value, customObjectMapper); jgen.writeRawValue(mapJson); } else if (value instanceof Object[] || value.getClass().isArray()) { String arrayJson = writeArray((Object[]) value, customObjectMapper); jgen.writeRawValue(arrayJson); } else { String json = customObjectMapper.writeValueAsString(value); // don't wrap java and javax classes as they are always available, in addition avoid double wrapping if (!className.startsWith("java.") && !className.startsWith("javax.") && !json.contains(className)) { json = "{\"" + className + "\":" + json + "}"; } jgen.writeRawValue(json); } }
@Override public JsonSerializer<?> findSerializer(Annotated am) { // replace JaxbUnknownAdapter as it breaks JSON marshaller for list and maps with wrapping serializer XmlJavaTypeAdapter adapterInfo = findAnnotation(XmlJavaTypeAdapter.class, am, true, false, false); if (adapterInfo != null && adapterInfo.value().isAssignableFrom(JaxbUnknownAdapter.class)) { if ( findAnnotation(JsonSerialize.class, am, true, false, false) != null ) { // .. unless there is also an explicitly specified serializer, in such case use the specified one: return super.findSerializer(am); } return new WrappingObjectSerializer(customObjectMapper); } return super.findSerializer(am); }