Skip to content
Permalink
Browse files
Add encoding for deserializing objects.
  • Loading branch information
Jim Gomes committed Nov 11, 2009
1 parent e6ccca2 commit 959aff8ea7695d38bb04c245c5a4da4678d6c336
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 30 deletions.
@@ -16,6 +16,7 @@
*/

using System;
using System.Text;
using Apache.NMS.Util;

namespace Apache.NMS
@@ -27,27 +28,45 @@ public static class MessageExtensions
/// Deserializes the object from Xml, and returns it.
/// </summary>
public static object ToObject(this IMessage message)
{
if(null != message)
{
return NMSConvert.DeserializeObjFromMessage(message);
}

return null;
{
return ToObject<object>(message);
}

/// <summary>
/// Deserializes the object from Xml, and returns it.
/// </summary>
public static object ToObject(this IMessage message, Encoding encoding)
{
return ToObject<object>(message, encoding);
}

/// <summary>
/// Deserializes the object from Xml, and returns it.
/// </summary>
public static T ToObject<T>(this IMessage message) where T : class
{
if(null != message)
{
return (T) NMSConvert.DeserializeObjFromMessage(message);
}

return null;
{
return ToObject<T>(message, Encoding.Unicode);
}

/// <summary>
/// Deserializes the object from Xml, and returns it.
/// </summary>
public static T ToObject<T>(this IMessage message, Encoding encoding) where T : class
{
try
{
if(null != message)
{
return (T) NMSConvert.DeserializeObjFromMessage(message, encoding);
}
}
catch(Exception ex)
{
Tracer.ErrorFormat("Error converting message to object: {0}", ex.Message);
}

return null;
}
}
#endif
}
@@ -108,7 +108,7 @@ public static ITextMessage ToXmlMessage(ISession session, object obj, Encoding e
#endif
public static object FromXmlMessage(IMessage message)
{
return DeserializeObjFromMessage(message);
return DeserializeObjFromMessage(message, Encoding.Unicode);
}

/// <summary>
@@ -131,14 +131,21 @@ internal static ITextMessage SerializeObjToMessage(ITextMessage message, object
/// Deserialize the object from the text message. The object must be serializable from XML.
/// </summary>
/// <param name="message"></param>
/// <param name="encoding"></param>
/// <returns></returns>
internal static object DeserializeObjFromMessage(IMessage message)
internal static object DeserializeObjFromMessage(IMessage message, Encoding encoding)
{
ITextMessage textMessage = message as ITextMessage;

if(null == textMessage)
{
return null;
}

if(null == textMessage.NMSType || textMessage.NMSType.Length < 1)
{
Tracer.ErrorFormat("NMSType not set on message. Could not deserializing XML object.");
return null;
}

Type objType = GetRuntimeType(textMessage.NMSType);
@@ -148,7 +155,7 @@ internal static object DeserializeObjFromMessage(IMessage message)
return null;
}

return XmlUtil.Deserialize(objType, textMessage.Text);
return XmlUtil.Deserialize(objType, textMessage.Text, encoding);
}

/// <summary>
@@ -53,16 +53,16 @@ public static string Serialize(object obj, Encoding encoding)
byte[] encodedBytes = memoryStream.ToArray();
return encoding.GetString(encodedBytes, 0, encodedBytes.Length);
}
catch(Exception e)
catch(Exception ex)
{
Tracer.Error(e.Message);
Tracer.ErrorFormat("Error serializing object: {0}", ex.Message);
return null;
}
}

public static object Deserialize(Type objType, string text)
{
return Deserialize(objType, text, Encoding.UTF8);
return Deserialize(objType, text, Encoding.Unicode);
}

public static object Deserialize(Type objType, string text, Encoding encoding)
@@ -72,17 +72,25 @@ public static object Deserialize(Type objType, string text, Encoding encoding)
return null;
}

XmlSerializer serializer = new XmlSerializer(objType);
MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(text));
try
{
XmlSerializer serializer = new XmlSerializer(objType);
MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(text));

/*
* If the XML document has been altered with unknown
* nodes or attributes, handle them with the
* UnknownNode and UnknownAttribute events.
*/
serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
return serializer.Deserialize(memoryStream);
/*
* If the XML document has been altered with unknown
* nodes or attributes, handle them with the
* UnknownNode and UnknownAttribute events.
*/
serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
return serializer.Deserialize(memoryStream);
}
catch(Exception ex)
{
Tracer.ErrorFormat("Error deserializing object: {0}", ex.Message);
return null;
}
}

private static void serializer_UnknownNode(object sender, XmlNodeEventArgs e)

0 comments on commit 959aff8

Please sign in to comment.