Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. using Apache.NMS;
  2. using Newtonsoft.Json;
  3. using Spring.Messaging.Nms.Support.Converter;
  4. using System;
  5. using System.Collections;
  6.  
  7. namespace Spring.Messaging.Nms.Support.Converter
  8. {
  9. /// <summary>
  10. /// Convert an object via JSON serialization for sending via an ITextMessage
  11. /// </summary>
  12. /// <author>Ivan R. Perez</author>
  13. public class JsonMessageConverter : IMessageConverter
  14. {
  15. private IMessageConverter defaultMessageConverter = new SimpleMessageConverter();
  16. private TypeMapper typeMapper = new TypeMapper();
  17.  
  18. public JsonMessageConverter()
  19. {
  20. typeMapper.UseAssemblyQualifiedName = true;
  21. }
  22.  
  23. /// <summary>
  24. /// Sets the type mapper
  25. /// </summary>
  26. /// <value>The type mapper.</value>
  27. public TypeMapper TypeMapper
  28. {
  29. set { typeMapper = value; }
  30. }
  31.  
  32. /// <summary>
  33. /// Convert a .NET object to a NMS Message using the supplied session
  34. /// to create the message object.
  35. /// </summary>
  36. /// <param name="objectToConvert">THe object to convert</param>
  37. /// <param name="session">The Session to use for creating a NMS Message</param>
  38. /// <returns>The NMS Message</returns>
  39. /// <throws>NMSException if thrown by NMS API methods</throws>
  40. /// <throws>MessageConversionException in case of conversion failure</throws>
  41. public IMessage ToMessage(object objectToConvert, ISession session)
  42. {
  43. if(objectToConvert == null)
  44. {
  45. throw new MessageConversionException("Can't convert null object");
  46. }
  47.  
  48. try
  49. {
  50. if(objectToConvert.GetType().Equals(typeof(string)) ||
  51. typeof(IDictionary).IsAssignableFrom(objectToConvert.GetType()) ||
  52. objectToConvert.GetType().Equals(typeof(Byte[])))
  53. {
  54. return defaultMessageConverter.ToMessage(objectToConvert, session);
  55. }
  56. string jsonString = GetJsonString(objectToConvert);
  57. IMessage msg = session.CreateTextMessage(jsonString);
  58. msg.Properties.SetString(typeMapper.TypeIdFieldName, typeMapper.FromType(objectToConvert.GetType()));
  59. return msg;
  60. }catch (Exception ex)
  61. {
  62. throw new MessageConversionException("Can't convert object of type " + objectToConvert.GetType(), ex);
  63. }
  64. }
  65.  
  66. /// <summary>
  67. /// Gets the JSON string for an object
  68. /// </summary>
  69. /// <param name="objectToConvert">The object to convert.</param>
  70. /// <returns>JSON string</returns>
  71. protected virtual string GetJsonString(object objectToConvert)
  72. {
  73. string jsonString;
  74. try
  75. {
  76. jsonString = JsonConvert.SerializeObject(objectToConvert);
  77. } catch (Exception ex)
  78. {
  79. throw new MessageConversionException("Can't convert object of type " + objectToConvert.GetType(), ex);
  80. }
  81.  
  82. return jsonString;
  83. }
  84.  
  85. /// <summary>
  86. /// Convert from a NMS Message to a .NET object.
  87. /// </summary>
  88. /// <param name="messageToConvert">The message to convert</param>
  89. /// <returns>The converted .NET object</returns>
  90. /// <throws>MessageConversionException in case of converesion failure</throws>
  91. public object FromMessage(IMessage messageToConvert)
  92. {
  93. if(messageToConvert == null)
  94. {
  95. throw new MessageConversionException("Can't convert null message");
  96. }
  97.  
  98. try
  99. {
  100. string converterId = messageToConvert.Properties.GetString(typeMapper.TypeIdFieldName);
  101. if(converterId == null)
  102. {
  103. return defaultMessageConverter.FromMessage(messageToConvert);
  104. }
  105. ITextMessage textMessage = messageToConvert as ITextMessage;
  106. if(textMessage == null)
  107. {
  108. throw new MessageConversionException("Can't convert message of type " + messageToConvert.GetType());
  109. }
  110.  
  111. var obj = JsonConvert.DeserializeObject(textMessage.Text, GetTargetType(textMessage));
  112. return obj;
  113. } catch(Exception ex)
  114. {
  115. throw new MessageConversionException("Can't convert message of type " + messageToConvert.GetType(), ex);
  116. }
  117. }
  118.  
  119. /// <summary>
  120. /// Gets the type of the target given the message.
  121. /// </summary>
  122. /// <param name="message">The message.</param>
  123. /// <returns>Type of the target</returns>
  124. protected virtual Type GetTargetType(ITextMessage message)
  125. {
  126. return typeMapper.ToType(message.Properties.GetString(typeMapper.TypeIdFieldName));
  127. }
  128. }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement