public MessageSerDesOptionsBuilder(MessageSerDesOptions options = null) { Options = new MessageSerDesOptions(); if (options != null) { Options.DeserializationType = options.DeserializationType; Options.DynamicDeserializationScannedAssemblies = options.DynamicDeserializationScannedAssemblies; } }
public string SerializeMessageEnvelope(MessagingEnvelope message, MessageSerDesOptions options = null) { var messageTypeId = _messageTypeRegistry.GetTypeId(message.Payload.GetType()); message.SetHeader(MessagingHeaders.MessageType, messageTypeId, true); var json = JsonConvert.SerializeObject(message, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); return(json); }
public MessagingEnvelope DeserializeMessageEnvelope(string envelopeString, MessageSerDesOptions options = null) { var serDesOptions = options ?? new MessageSerDesOptions(); if (serDesOptions.DeserializationType == DeserializationType.TypeSafe) { throw new Exception("Cannot deserialize generic envelope using the TypeSafe deserialization option"); } var partialEnvelope = DeserializePartialEnvelope(envelopeString, out var concreteType, serDesOptions); return(serDesOptions.DeserializationType == DeserializationType.HeadersOnly ? new MessagingEnvelope(partialEnvelope.Headers, partialEnvelope.Payload.ToString()) : new MessagingEnvelope(partialEnvelope.Headers, partialEnvelope.Payload.ToObject(concreteType))); }
private (MessagingEnvelope <JObject> envelope, Type runtimeType) DeserializePartialEnvelope( string envelopeString, Type expectedType = null, MessageSerDesOptions options = null) { options ??= MessageSerDesOptions.Default; var partialEnvelope = JsonConvert.DeserializeObject <MessagingEnvelope <JObject> >(envelopeString); if (!options.UseDynamicDeserialization) { return(partialEnvelope, null); } var messageTypeId = partialEnvelope.GetMessageTypeId(); if (messageTypeId == null) { if (expectedType == null || expectedType.IsAbstract || expectedType.IsInterface) { throw new Exception("Type information was not found in MessageType header"); } return(partialEnvelope, null); } var runtimeType = _messageTypeRegistry.ResolveType(messageTypeId, options.DynamicDeserializationScannedAssemblies); if (runtimeType == null) { return(partialEnvelope, null); } if (expectedType != null && !expectedType.IsAssignableFrom(runtimeType)) { throw new Exception($"Incompatible types: expected {expectedType} and found {runtimeType}"); } return(partialEnvelope, runtimeType); }
private MessagingEnvelope <JObject> DeserializePartialEnvelope(string envelopeString, out Type concreteType, MessageSerDesOptions serDesOptions, Type expectedType = null) { var partialEnvelope = JsonConvert.DeserializeObject <MessagingEnvelope <JObject> >(envelopeString); if (serDesOptions.DeserializationType == DeserializationType.HeadersOnly) { concreteType = null; return(partialEnvelope); } var messageTypeId = partialEnvelope.GetMessageTypeId(); if (messageTypeId == null) { if (expectedType == null || expectedType.IsAbstract || expectedType.IsInterface) { throw new Exception("Type information was not found in MessageType header"); } concreteType = expectedType; return(partialEnvelope); } concreteType = _messageTypeRegistry.ResolveType(messageTypeId, serDesOptions.DynamicDeserializationScannedAssemblies); if (expectedType != null && !expectedType.IsAssignableFrom(concreteType)) { throw new Exception($"Incompatible types: expected {expectedType} and found {concreteType}"); } return(partialEnvelope); }
public MessagingEnvelope <TMessage> DeserializeMessageEnvelope <TMessage>(string envelopeString, MessageSerDesOptions options = null) { var serDesOptions = options ?? new MessageSerDesOptions(); if (serDesOptions.DeserializationType == DeserializationType.HeadersOnly) { throw new Exception("Cannot use typed deserialization using 'HeadersOnly' deserialization setting."); } if (serDesOptions.DeserializationType == DeserializationType.Dynamic) { var partialEnvelope = DeserializePartialEnvelope(envelopeString, out var concreteType, serDesOptions, typeof(TMessage)); return(new MessagingEnvelope <TMessage>(partialEnvelope.Headers, (TMessage)partialEnvelope.Payload.ToObject(concreteType))); } var message = JsonConvert.DeserializeObject <MessagingEnvelope <TMessage> >(envelopeString); return(message); }
public MessagingEnvelope <TMessage> DeserializeMessageEnvelope <TMessage>(byte[] envelopeData, MessageSerDesOptions options = null) { var envelopeString = System.Text.Encoding.UTF8.GetString(envelopeData); var(partialEnvelope, runtimeType) = DeserializePartialEnvelope(envelopeString, typeof(TMessage), options); var outputType = runtimeType ?? (typeof(TMessage) == typeof(object) ? typeof(ExpandoObject) : typeof(TMessage)); return(new MessagingEnvelope <TMessage>(partialEnvelope.Headers, (TMessage)partialEnvelope.Payload.ToObject(outputType))); }