private static object DeserializeMessage(byte[] body, string messageAsString, MessageFormat format, string messageType) { object result = null; if (format.Equals(MessageFormat.Json)) { result = Newtonsoft.Json.JsonConvert.DeserializeObject(messageAsString, MessageTypeDictionary[messageType]); } else if (format.Equals(MessageFormat.Xml)) { var messageStream = new MemoryStream(); messageStream.Write(body, 0, body.Length); messageStream.Seek(0, SeekOrigin.Begin); var xmlSerializer = new XmlSerializer(MessageTypeDictionary[messageType]); result = xmlSerializer.Deserialize(messageStream); } else if (format.Equals(MessageFormat.Binary)) { var messageStream = new MemoryStream(); messageStream.Write(body, 0, body.Length); messageStream.Seek(0, SeekOrigin.Begin); var binarySerializer = new BinaryFormatter(); if (messageType.Contains(nameof(MyOtherMessage))) { result = binarySerializer.Deserialize(messageStream) as MyOtherMessage; } else if (messageType.Contains(nameof(MyMessage))) { result = binarySerializer.Deserialize(messageStream) as MyMessage; } } return(result); }
private static byte[] SerializeMessage(object myMessage, MessageFormat format) { byte[] result = null; if (format.Equals(MessageFormat.Json)) { var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(myMessage); result = Encoding.Default.GetBytes(jsonString); } else if (format.Equals(MessageFormat.Xml)) { var messageStream = new MemoryStream(); var xmlSerializer = new XmlSerializer(myMessage.GetType()); xmlSerializer.Serialize(messageStream, myMessage); messageStream.Flush(); messageStream.Seek(0, SeekOrigin.Begin); result = messageStream.GetBuffer(); } else if (format.Equals(MessageFormat.Binary)) { var messageStream = new MemoryStream(); var binarySerializer = new BinaryFormatter(); binarySerializer.Serialize(messageStream, myMessage); messageStream.Flush(); messageStream.Seek(0, SeekOrigin.Begin); result = messageStream.GetBuffer(); } return(result); }
private static string GetMessageAsString(byte[] body, MessageFormat messageFormat) { var messageAsString = string.Empty; if (messageFormat.Equals(MessageFormat.Json)) { messageAsString = Encoding.Default.GetString(body); } else if (messageFormat.Equals(MessageFormat.Xml)) { messageAsString = Encoding.Default.GetString(body); } else if (messageFormat.Equals(MessageFormat.Binary)) { messageAsString = Convert.ToBase64String(body); } return(messageAsString); }
private static string GetContentType(MessageFormat format) { var contentType = string.Empty; if (format.Equals(MessageFormat.Json)) { contentType = "application/json"; } else if (format.Equals(MessageFormat.Xml)) { contentType = "text/xml"; } else if (format.Equals(MessageFormat.Binary)) { contentType = "application/octet-stream"; } return(contentType); }
public void Test4112104() { MessageFormat format = new MessageFormat(""); try { // This should NOT throw an exception if (format.Equals(null)) { // It also should return false Errln("MessageFormat.Equals(null) returns false"); } } catch (ArgumentNullException e) { Errln("MessageFormat.Equals(null) throws " + e); } }
public bool Equals(DiagnosticDescriptor?other) { if (ReferenceEquals(this, other)) { return(true); } return (other != null && Category == other.Category && DefaultSeverity == other.DefaultSeverity && Description.Equals(other.Description) && HelpLinkUri == other.HelpLinkUri && Id == other.Id && IsEnabledByDefault == other.IsEnabledByDefault && MessageFormat.Equals(other.MessageFormat) && Title.Equals(other.Title)); }