private static void putDeadlineScope(JobDataMap jobData, ScopeDescriptor deadlineScope, Serializer serializer) { SerializedObject<byte[]> serializedDeadlineScope = serializer.serialize(deadlineScope, byte[].class); jobData.put(SERIALIZED_DEADLINE_SCOPE, serializedDeadlineScope.getData()); jobData.put(SERIALIZED_DEADLINE_SCOPE_CLASS_NAME, serializedDeadlineScope.getType().getName()); }
/** * 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) { if (Map.class.equals(serializedObject.getContentType())) { // this is the MetaDataMap, deserialize differently Map<String, MetaDataValue> metaDataMap = (Map<String, MetaDataValue>) serializedObject.getData(); return (T) metaDataConverter.convert(metaDataMap); } return delegate.deserialize(serializedObject); }
/** * Returns the type of the serialized object, for which no class could be resolved. * * @return the type of the serialized object */ public SerializedType serializedType() { return serializedObject.getType(); } }
/** * 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(); }
/** * Indicates whether the given {@code desiredFormat} is supported as a representation for this type. More * specifically, it will consult the serializer that returned this instance whether it has a converter to convert * the serialized object's data to the desired type * * @param desiredFormat the format in which the serialized object is desired * @param <T> the format in which the serialized object is desired * @return {@code true} if the format is supported, otherwise {@code false} */ public <T> boolean supportsFormat(Class<T> desiredFormat) { return serializer.getConverter().canConvert(serializedObject.getContentType(), desiredFormat); }
@SuppressWarnings({"unchecked"}) @Override public Object doDeserialize(SerializedObject serializedObject, XStream xStream) { if ("org.dom4j.Document".equals(serializedObject.getContentType().getName())) { return xStream.unmarshal(new Dom4JReader((org.dom4j.Document) serializedObject.getData())); } if ("nu.xom.Document".equals(serializedObject.getContentType().getName())) { return xStream.unmarshal(new XomReader((nu.xom.Document) serializedObject.getData())); } InputStream serializedData = convert(serializedObject.getData(), serializedObject.getContentType(), InputStream.class); return xStream.fromXML(new InputStreamReader(serializedData, getCharset())); }
/** * Creates an instance which will deserialize given {@code serializedObject} upon request. Use this constructor * if the {@code serializedObject} is already available and does not need to undergo a process of e.g. * upcasting. * * @param serializedObject The serialized payload of the message * @param serializer The serializer to deserialize the payload data with */ public LazyDeserializingObject(SerializedObject<?> serializedObject, Serializer serializer) { this(() -> serializedObject, serializedObject.getType(), serializer); }
@Override @SuppressWarnings("unchecked") public SerializedObject<T> getData() { SerializedObject<?> serializedInput = converter.convert(source.getData(), requiredType); return new SimpleSerializedObject<>(upcastFunction.apply((T) serializedInput.getData()), requiredType, getType()); }
/** * Indicates whether the given {@code desiredFormat} is supported as a representation for this type. More * specifically, it will consult the serializer that returned this instance whether it has a converter to convert * the serialized object's data to the desired type * * @param desiredFormat the format in which the serialized object is desired * @param <T> the format in which the serialized object is desired * @return {@code true} if the format is supported, otherwise {@code false} */ public <T> boolean supportsFormat(Class<T> desiredFormat) { return serializer.getConverter().canConvert(serializedObject.getContentType(), desiredFormat); }
/** * Constructs a new SagaEntry for the given {@code saga}. The given saga must be serializable. The provided * saga is not modified by this operation. * * @param saga The saga to store * @param sagaIdentifier The saga identifier * @param serializer The serialization mechanism to convert the Saga to a byte stream * @param contentType The saga content type to serialize to */ public AbstractSagaEntry(Object saga, String sagaIdentifier, Serializer serializer, Class<T> contentType) { this.sagaId = sagaIdentifier; SerializedObject<T> serialized = serializer.serialize(saga, contentType); this.serializedSaga = serialized.getData(); this.sagaType = serialized.getType().getName(); this.revision = serialized.getType().getRevision(); }
@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); } }
@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); } } }
/** * Indicates whether the given {@code serializedObject} represents a serialized form of a MetaData object, * such as the ones created by this class (see {@link #SerializedMetaData(Object, Class)}. * * @param serializedObject The object to check for Meta Data * @return {@code true} if the serialized objects represents serialized meta data, otherwise * {@code false}. */ public static boolean isSerializedMetaData(SerializedObject<?> serializedObject) { return serializedObject != null && serializedObject.getType() != null && METADATA_CLASS_NAME.equals(serializedObject.getType().getName()); }
/** * 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(); }
/** * Initializes a ReplyMessage containing a reply to the command with given {commandIdentifier} and given * {@code commandResultMessage}. * * @param commandIdentifier the identifier of the command to which the message is a reply * @param commandResultMessage the result message of command process * @param serializer the serializer to serialize the message contents with */ public ReplyMessage(String commandIdentifier, CommandResultMessage<?> commandResultMessage, Serializer serializer) { this.commandIdentifier = commandIdentifier; SerializedObject<byte[]> metaData = commandResultMessage.serializeMetaData(serializer, byte[].class); this.serializedMetaData = metaData.getData(); SerializedObject<byte[]> payload = commandResultMessage.serializePayload(serializer, byte[].class); this.serializedPayload = payload.getData(); this.payloadType = payload.getType().getName(); this.payloadRevision = payload.getType().getRevision(); SerializedObject<byte[]> exception = commandResultMessage.serializeExceptionResult(serializer, byte[].class); this.serializedException = exception.getData(); this.exceptionType = exception.getType().getName(); this.exceptionRevision = exception.getType().getRevision(); }
/** * 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) { if (Map.class.equals(serializedObject.getContentType())) { // this is the MetaDataMap, deserialize differently Map<String, MetaDataValue> metaDataMap = (Map<String, MetaDataValue>) serializedObject.getData(); return (T) metaDataConverter.convert(metaDataMap); } return delegate.deserialize(serializedObject); }
@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); } return (T) doDeserialize(serializedObject, xStream); }
@Override public ProducerRecord<String, byte[]> createKafkaMessage(EventMessage<?> eventMessage, String topic) { SerializedObject<byte[]> serializedObject = serializePayload(eventMessage, serializer, byte[].class); byte[] payload = serializedObject.getData(); return new ProducerRecord<>(topic, null, null, key(eventMessage), payload, toHeaders(eventMessage, serializedObject, headerValueMapper)); }