/** * Converts the given object into another. * * @param original the value to convert * @param targetType The type of data to convert to * @param <T> the target data type * @return the converted value */ default <T> T convert(Object original, Class<T> targetType) { return convert(original, original.getClass(), targetType); }
@Override public <D> SerializedObject<D> getData(Class<D> requiredType) { return converter.convert(getData(), requiredType); }
@Override @SuppressWarnings("unchecked") public <D> SerializedObject<D> getData(Class<D> requiredType) { return serializer.getConverter().convert(data, requiredType); }
/** * Convert the given {@code source}, of type {@code sourceType} to the given {@code targetType}. * * @param <S> The type of data that needs to be converted * @param <T> The target type of the conversion * @param source The object to convert * @param sourceType The source type of the conversion * @param targetType The target type of the conversion * @return The converted object */ protected <S, T> T convert(S source, Class<S> sourceType, Class<T> targetType) { return getConverter().convert(source, sourceType, targetType); }
/** * Returns the data contained in the serialized object in the given {@code desiredFormat}, if supported. If the * desired format is unsupported, an exception is thrown, depending on the serializer. * <p> * To verify whether a format is supported, use {@link #supportsFormat(Class)}. * * @param desiredFormat the format in which the data is desired * @param <T> the format in which the data is desired * @return the data in the desired format */ public <T> T readData(Class<T> desiredFormat) { return serializer.getConverter().convert(serializedObject, desiredFormat).getData(); }
@SuppressWarnings({"NonSerializableObjectPassedToObjectStream", "ThrowFromFinallyBlock"}) @Override public <T> SerializedObject<T> serialize(Object instance, Class<T> expectedType) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); try { oos.writeObject(instance); } finally { oos.flush(); } } catch (IOException e) { throw new SerializationException("An exception occurred writing serialized data to the output stream", e); } T converted = converter.convert(baos.toByteArray(), expectedType); return new SimpleSerializedObject<>(converted, expectedType, getSerializedType(instance)); }
@SuppressWarnings("unchecked") public <T> SerializedObject<T> serializeMetaData(Serializer serializer, Class<T> expectedRepresentation) { synchronized (metaDataGuard) { SerializedObject existingForm = serializedMetaData.get(serializer); if (existingForm == null) { SerializedObject<T> serialized = serializer.serialize(message.getMetaData(), expectedRepresentation); serializedMetaData.put(serializer, serialized); return serialized; } else { return serializer.getConverter().convert(existingForm, expectedRepresentation); } } } }
@Override @SuppressWarnings("unchecked") public SerializedObject<T> getData() { SerializedObject<?> serializedInput = converter.convert(source.getData(), requiredType); return new SimpleSerializedObject<>(upcastFunction.apply((T) serializedInput.getData()), requiredType, getType()); }
@SuppressWarnings("unchecked") @Override public <R> SerializedObject<R> serializePayload(Serializer serializer, Class<R> expectedRepresentation) { if (serializer.equals(payload.getSerializer())) { return serializer.getConverter().convert(payload.getSerializedObject(), expectedRepresentation); } return serializer.serialize(payload.getObject(), expectedRepresentation); }
@SuppressWarnings("unchecked") @Override public <R> SerializedObject<R> serializeMetaData(Serializer serializer, Class<R> expectedRepresentation) { if (serializer.equals(metaData.getSerializer())) { return serializer.getConverter().convert(metaData.getSerializedObject(), expectedRepresentation); } return serializer.serialize(metaData.getObject(), expectedRepresentation); }
@SuppressWarnings("unchecked") @Override public <S, T> T deserialize(SerializedObject<S> serializedObject) { if (SerializedType.emptyType().equals(serializedObject.getType())) { return null; } if (UnknownSerializedType.class.isAssignableFrom(classForType(serializedObject.getType()))) { return (T) new UnknownSerializedType(this, serializedObject); } SerializedObject<InputStream> converted = converter.convert(serializedObject, InputStream.class); ObjectInputStream ois = null; try { ois = new ObjectInputStream(converted.getData()); return (T) ois.readObject(); } catch (ClassNotFoundException | IOException e) { throw new SerializationException("An error occurred while deserializing: " + e.getMessage(), e); } finally { IOUtils.closeQuietly(ois); } }
/** * Converts the data format of the given {@code original} IntermediateRepresentation to the target data type. * * @param original The source to convert * @param targetType The type of data to convert to * @param <T> the target data type * @return the converted representation */ @SuppressWarnings("unchecked") default <T> SerializedObject<T> convert(SerializedObject<?> original, Class<T> targetType) { if (original.getContentType().equals(targetType)) { return (SerializedObject<T>) original; } return new SimpleSerializedObject<>(convert(original.getData(), original.getContentType(), targetType), targetType, original.getType()); }
@Override public <S, T> T deserialize(SerializedObject<S> serializedObject) { try { if (SerializedType.emptyType().equals(serializedObject.getType())) { return null; } Class<?> type = classForType(serializedObject.getType()); if (UnknownSerializedType.class.isAssignableFrom(type)) { return (T) new UnknownSerializedType(this, serializedObject); } if (JsonNode.class.equals(serializedObject.getContentType())) { return getReader(type) .readValue((JsonNode) serializedObject.getData()); } SerializedObject<byte[]> byteSerialized = converter.convert(serializedObject, byte[].class); return getReader(type).readValue(byteSerialized.getData()); } catch (IOException e) { throw new SerializationException("Error while deserializing object", e); } }
@Override public <T> SerializedObject<T> serialize(Object object, Class<T> expectedRepresentation) { try { if (String.class.equals(expectedRepresentation)) { //noinspection unchecked return new SimpleSerializedObject<>((T) getWriter().writeValueAsString(object), expectedRepresentation, typeForClass(ObjectUtils.nullSafeTypeOf(object))); } byte[] serializedBytes = getWriter().writeValueAsBytes(object); T serializedContent = converter.convert(serializedBytes, expectedRepresentation); return new SimpleSerializedObject<>(serializedContent, expectedRepresentation, typeForClass(ObjectUtils.nullSafeTypeOf(object))); } catch (JsonProcessingException e) { throw new SerializationException("Unable to serialize object", e); } }
@SuppressWarnings("unchecked") public <T> SerializedObject<T> serializePayload(Serializer serializer, Class<T> expectedRepresentation) { synchronized (payloadGuard) { SerializedObject existingForm = serializedPayload.get(serializer); if (existingForm == null) { SerializedObject<T> serialized = serializer.serialize(message.getPayload(), expectedRepresentation); if (message.getPayload() == null) { // make sure the payload type is maintained serialized = new SimpleSerializedObject<>(serialized.getData(), serialized.getContentType(), serializer.typeForClass(message.getPayloadType())); } serializedPayload.put(serializer, serialized); return serialized; } else { return serializer.getConverter().convert(existingForm, expectedRepresentation); } } }
@Override @SuppressWarnings("unchecked") public <D> SerializedObject<D> getData(Class<D> requiredType) { return serializer.getConverter().convert(data, requiredType); }
@Override public <D> SerializedObject<D> getData(Class<D> requiredType) { return converter.convert(getData(), requiredType); }
@Override @SuppressWarnings("unchecked") public <D> SerializedObject<D> getData(Class<D> requiredType) { return serializer.getConverter().convert(data, requiredType); }
/** * Convert the given {@code source}, of type {@code sourceType} to the given {@code targetType}. * * @param <S> The type of data that needs to be converted * @param <T> The target type of the conversion * @param source The object to convert * @param sourceType The source type of the conversion * @param targetType The target type of the conversion * @return The converted object */ protected <S, T> T convert(S source, Class<S> sourceType, Class<T> targetType) { return getConverter().convert(source, sourceType, targetType); }
@SuppressWarnings("unchecked") @Override public <R> SerializedObject<R> serializeMetaData(Serializer serializer, Class<R> expectedRepresentation) { if (serializer.equals(metaData.getSerializer())) { return serializer.getConverter().convert(metaData.getSerializedObject(), expectedRepresentation); } return serializer.serialize(metaData.getObject(), expectedRepresentation); }