You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jg...@apache.org on 2009/11/11 01:30:34 UTC
svn commit: r834726 - in
/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp:
MessageExtensions.cs Util/Convert.cs Util/XmlUtils.cs
Author: jgomes
Date: Wed Nov 11 00:30:33 2009
New Revision: 834726
URL: http://svn.apache.org/viewvc?rev=834726&view=rev
Log:
Add encoding for deserializing objects.
Fixes [AMQNET-209]. (See https://issues.apache.org/activemq/browse/AMQNET-209)
Modified:
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/MessageExtensions.cs
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/Convert.cs
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs
Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/MessageExtensions.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/MessageExtensions.cs?rev=834726&r1=834725&r2=834726&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/MessageExtensions.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/MessageExtensions.cs Wed Nov 11 00:30:33 2009
@@ -16,6 +16,7 @@
*/
using System;
+using System.Text;
using Apache.NMS.Util;
namespace Apache.NMS
@@ -27,27 +28,45 @@
/// 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
}
Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/Convert.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/Convert.cs?rev=834726&r1=834725&r2=834726&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/Convert.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/Convert.cs Wed Nov 11 00:30:33 2009
@@ -108,7 +108,7 @@
#endif
public static object FromXmlMessage(IMessage message)
{
- return DeserializeObjFromMessage(message);
+ return DeserializeObjFromMessage(message, Encoding.Unicode);
}
/// <summary>
@@ -131,14 +131,21 @@
/// 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 @@
return null;
}
- return XmlUtil.Deserialize(objType, textMessage.Text);
+ return XmlUtil.Deserialize(objType, textMessage.Text, encoding);
}
/// <summary>
Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs?rev=834726&r1=834725&r2=834726&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs Wed Nov 11 00:30:33 2009
@@ -53,16 +53,16 @@
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 @@
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)