@Override public Object findSerializationConverter(Annotated a) { Class<?> serType = _rawSerializationType(a); // Can apply to both container and regular type; no difference yet here XmlAdapter<?,?> adapter = findAdapter(a, true, serType); if (adapter != null) { return _converter(adapter, true); } return null; }
public JSONMarshaller(Set<Class<?>> classes, ClassLoader classLoader) { this.classLoader = classLoader; buildMarshaller(classes, classLoader); configureMarshaller(classes, classLoader); }
@Override public TypeDeserializer buildTypeDeserializer(DeserializationConfig config, JavaType baseType, Collection<NamedType> subtypes) { if (useForType(baseType)) { if (_idType == JsonTypeInfo.Id.NONE) { return null; } TypeIdResolver idRes = idResolver(config, baseType, subtypes, false, true); switch (_includeAs) { case WRAPPER_OBJECT: return new CustomAsWrapperTypeDeserializer(baseType, idRes, _typeProperty, true, baseType); } } return super.buildTypeDeserializer(config, baseType, subtypes); }
CaseServicesClient.class); String responseBody = caseServicesClient.startCase(containerId, caseDefId, new StringContentCaseFile(body)); resp.getOutputStream().write(responseBody.getBytes("UTF-8")); } else { ProcessServicesClient.class); Long responseBody = processServicesClient.startProcess(containerId, processId, new StringContentMap(body));
List<NamedType> customClasses = prepareCustomClasses(classes); AnnotationIntrospector primary = new ExtendedJaxbAnnotationIntrospector(customClasses, customSerializationMapper); AnnotationIntrospector secondary = new JacksonAnnotationIntrospector(); AnnotationIntrospector introspectorPair = AnnotationIntrospector.pair(primary, secondary); CustomObjectSerializer customObjectSerializer = new CustomObjectSerializer(customObjectMapper); modDeser.addDeserializer(Object.class, new CustomObjectDeserializer(classes)); extension.extend(this, objectMapper, deserializeObjectMapper);
private XmlAdapter<Object,Object> findAdapter(Annotated am, boolean forSerialization, Class<?> type) { // First of all, are we looking for annotations for class? if (am instanceof AnnotatedClass) { return findAdapterForClass((AnnotatedClass) am, forSerialization); } // Otherwise for a member. First, let's figure out type of property XmlJavaTypeAdapter adapterInfo = findAnnotation(XmlJavaTypeAdapter.class, am, true, false, false); if (adapterInfo != null) { XmlAdapter<Object,Object> adapter = checkAdapter(adapterInfo, type, forSerialization); if (adapter != null) { return adapter; } } XmlJavaTypeAdapters adapters = findAnnotation(XmlJavaTypeAdapters.class, am, true, false, false); if (adapters != null) { for (XmlJavaTypeAdapter info : adapters.value()) { XmlAdapter<Object,Object> adapter = checkAdapter(info, type, forSerialization); if (adapter != null) { return adapter; } } } return null; }
@Override public Object deserializeTypedFromArray(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ClassLoader current = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(_baseType.getRawClass().getClassLoader()); JsonDeserializer<Object> deser = _findDeserializer(ctxt, baseTypeName()); Object value = deser.deserialize(jp, ctxt); return value; } finally { Thread.currentThread().setContextClassLoader(current); } }
@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 Marshaller build(Set<Class<?>> classes, MarshallingFormat format, ClassLoader classLoader) { switch ( format ) { case XSTREAM: logger.debug("About to build default instance of XStream marshaller with classes {} and class loader {}", classes, classLoader); return new XStreamMarshaller( classes, classLoader ); case JAXB: logger.debug("About to build default instance of JAXB marshaller with classes {} and class loader {}", classes, classLoader); return new JaxbMarshaller(classes, classLoader); case JSON: logger.debug("About to build default instance of JSON marshaller with classes {} and class loader {}", classes, classLoader); return new JSONMarshaller(classes, classLoader); default: logger.error( "Unsupported marshalling format: " + format ); } return null; } }
@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); }
@Override public TypeDeserializer forProperty(BeanProperty prop) { if (prop != null) { if (useForType(prop.getType()) || useForType(prop.getType().getContentType()) ) { return new CustomAsWrapperTypeDeserializer(this, prop); } } return super.forProperty(prop); }
@Test public void testStringContentMap() { Set<Class<?>> extraClasses = new HashSet<Class<?>>(); Marshaller marshaller = MarshallerFactory.getMarshaller(extraClasses, MarshallingFormat.JSON, this.getClass().getClassLoader()); String jsonMap = "{\"name\" : \"value\"}"; StringContentMap map = new StringContentMap(jsonMap); // content must be of exact value as given in constructor String marshall = marshaller.marshall(map); assertEquals(jsonMap, marshall); }
@Test public void testStringContentCaseFile() { Set<Class<?>> extraClasses = new HashSet<Class<?>>(); Marshaller marshaller = MarshallerFactory.getMarshaller(extraClasses, MarshallingFormat.JSON, this.getClass().getClassLoader()); String jsonMap = "{\n" + " \"case-data\" : {\n" + " \"yearsOfService\" : 1\n" + " },\n" + " \"case-user-assignments\" : {\n" + " },\n" + " \"case-group-assignments\" : {\n" + " }\n" + "}"; StringContentCaseFile map = new StringContentCaseFile(jsonMap); // content must be of exact value as given in constructor String marshall = marshaller.marshall(map); assertEquals(jsonMap, marshall); } }
@Override public void serialize(StringContent value, JsonGenerator gen, SerializerProvider provider) throws IOException { gen.writeRaw(value.getContent()); }
@Override public String marshall(Object objectInput) { try { return objectMapper.writeValueAsString(wrap(objectInput)); } catch (IOException e) { throw new MarshallingException("Error marshalling input", e); } }
@Override public <T> T unmarshall(String serializedInput, Class<T> type) { try { Class actualType = classesSet.contains(type) ? Object.class : type; return (T) unwrap(deserializeObjectMapper.readValue(serializedInput, actualType)); } catch (IOException e) { throw new MarshallingException("Error unmarshalling input", e); } finally { stripped.set(false); } }
CaseServicesClient.class); String responseBody = caseServicesClient.startCase(containerId, caseDefId, new StringContentCaseFile(body)); resp.getOutputStream().write(responseBody.getBytes("UTF-8")); } else { ProcessServicesClient.class); Long responseBody = processServicesClient.startProcess(containerId, processId, new StringContentMap(body));
@Override public Object deserializeTypedFromScalar(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ClassLoader current = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(_baseType.getRawClass().getClassLoader()); if (classesSet.contains(_baseType.getRawClass())) { try { return super.deserializeTypedFromScalar(jp, ctxt); } catch (Exception e) { JsonDeserializer<Object> deser = _findDeserializer(ctxt, baseTypeName()); Object value = deser.deserialize(jp, ctxt); return value; } } JsonDeserializer<Object> deser = _findDeserializer(ctxt, baseTypeName()); Object value = deser.deserialize(jp, ctxt); return value; } finally { Thread.currentThread().setContextClassLoader(current); } }
public static Marshaller getMarshaller(MarshallingFormat format, ClassLoader classLoader) { switch ( format ) { case XSTREAM: return new XStreamMarshaller( classLoader ); case JAXB: return new JaxbMarshaller(); // has to be implemented case JSON: return new JSONMarshaller(); // has to be implemented default: logger.error( "Unsupported marshalling format: " + format ); } return null; } }
@Override public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ClassLoader current = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(_baseType.getRawClass().getClassLoader()); if (classesSet.contains(_baseType.getRawClass()) && !stripped.get()) { try { return super.deserializeTypedFromObject(jp, ctxt); } catch (Exception e) { JsonDeserializer<Object> deser = _findDeserializer(ctxt, baseTypeName()); Object value = deser.deserialize(jp, ctxt); return value; } } stripped.set(false); JsonDeserializer<Object> deser = _findDeserializer(ctxt, baseTypeName()); Object value = deser.deserialize(jp, ctxt); return value; } finally { Thread.currentThread().setContextClassLoader(current); } }