Extensions.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. using System.Xml;
  5. using Google.ProtocolBuffers.Serialization;
  6. using Google.ProtocolBuffers.Serialization.Http;
  7. namespace Google.ProtocolBuffers
  8. {
  9. /// <summary>
  10. /// Extension methods for using serializers on instances of IMessageLite/IBuilderLite
  11. /// </summary>
  12. public static class Extensions
  13. {
  14. #region IMessageLite Extension
  15. /// <summary>
  16. /// Serializes the message to JSON text. This is a trivial wrapper
  17. /// around Serialization.JsonFormatWriter.WriteMessage.
  18. /// </summary>
  19. public static string ToJson(
  20. #if !NOEXTENSIONS
  21. this
  22. #endif
  23. IMessageLite message)
  24. {
  25. JsonFormatWriter w = JsonFormatWriter.CreateInstance();
  26. w.WriteMessage(message);
  27. return w.ToString();
  28. }
  29. /// <summary>
  30. /// Serializes the message to XML text. This is a trivial wrapper
  31. /// around Serialization.XmlFormatWriter.WriteMessage.
  32. /// </summary>
  33. public static string ToXml(
  34. #if !NOEXTENSIONS
  35. this
  36. #endif
  37. IMessageLite message)
  38. {
  39. StringWriter w = new StringWriter(new StringBuilder(4096));
  40. XmlFormatWriter.CreateInstance(w).WriteMessage(message);
  41. return w.ToString();
  42. }
  43. /// <summary>
  44. /// Serializes the message to XML text using the element name provided.
  45. /// This is a trivial wrapper around Serialization.XmlFormatWriter.WriteMessage.
  46. /// </summary>
  47. public static string ToXml(
  48. #if !NOEXTENSIONS
  49. this
  50. #endif
  51. IMessageLite message, string rootElementName)
  52. {
  53. StringWriter w = new StringWriter(new StringBuilder(4096));
  54. XmlFormatWriter.CreateInstance(w).WriteMessage(rootElementName, message);
  55. return w.ToString();
  56. }
  57. /// <summary>
  58. /// Writes the message instance to the stream using the content type provided
  59. /// </summary>
  60. /// <param name="message">An instance of a message</param>
  61. /// <param name="options">Options specific to writing this message and/or content type</param>
  62. /// <param name="contentType">The mime type of the content to be written</param>
  63. /// <param name="output">The stream to write the message to</param>
  64. public static void WriteTo(
  65. #if !NOEXTENSIONS
  66. this
  67. #endif
  68. IMessageLite message, MessageFormatOptions options, string contentType, Stream output)
  69. {
  70. ICodedOutputStream codedOutput = MessageFormatFactory.CreateOutputStream(options, contentType, output);
  71. // Output the appropriate message preamble
  72. codedOutput.WriteMessageStart();
  73. // Write the message content to the output
  74. message.WriteTo(codedOutput);
  75. // Write the closing message fragment
  76. codedOutput.WriteMessageEnd();
  77. codedOutput.Flush();
  78. }
  79. #endregion
  80. #region IBuilderLite Extensions
  81. /// <summary>
  82. /// Merges a JSON object into this builder and returns
  83. /// </summary>
  84. public static TBuilder MergeFromJson<TBuilder>(
  85. #if !NOEXTENSIONS
  86. this
  87. #endif
  88. TBuilder builder, string jsonText) where TBuilder : IBuilderLite
  89. {
  90. return JsonFormatReader.CreateInstance(jsonText)
  91. .Merge(builder);
  92. }
  93. /// <summary>
  94. /// Merges a JSON object into this builder and returns
  95. /// </summary>
  96. public static TBuilder MergeFromJson<TBuilder>(
  97. #if !NOEXTENSIONS
  98. this
  99. #endif
  100. TBuilder builder, TextReader reader) where TBuilder : IBuilderLite
  101. {
  102. return MergeFromJson(builder, reader, ExtensionRegistry.Empty);
  103. }
  104. /// <summary>
  105. /// Merges a JSON object into this builder using the extensions provided and returns
  106. /// </summary>
  107. public static TBuilder MergeFromJson<TBuilder>(
  108. #if !NOEXTENSIONS
  109. this
  110. #endif
  111. TBuilder builder, TextReader reader, ExtensionRegistry extensionRegistry) where TBuilder : IBuilderLite
  112. {
  113. return JsonFormatReader.CreateInstance(reader)
  114. .Merge(builder, extensionRegistry);
  115. }
  116. /// <summary>
  117. /// Merges an XML object into this builder and returns
  118. /// </summary>
  119. public static TBuilder MergeFromXml<TBuilder>(
  120. #if !NOEXTENSIONS
  121. this
  122. #endif
  123. TBuilder builder, XmlReader reader) where TBuilder : IBuilderLite
  124. {
  125. return MergeFromXml(builder, XmlFormatReader.DefaultRootElementName, reader, ExtensionRegistry.Empty);
  126. }
  127. /// <summary>
  128. /// Merges an XML object into this builder and returns
  129. /// </summary>
  130. public static TBuilder MergeFromXml<TBuilder>(
  131. #if !NOEXTENSIONS
  132. this
  133. #endif
  134. TBuilder builder, string rootElementName, XmlReader reader) where TBuilder : IBuilderLite
  135. {
  136. return MergeFromXml(builder, rootElementName, reader, ExtensionRegistry.Empty);
  137. }
  138. /// <summary>
  139. /// Merges an XML object into this builder using the extensions provided and returns
  140. /// </summary>
  141. public static TBuilder MergeFromXml<TBuilder>(
  142. #if !NOEXTENSIONS
  143. this
  144. #endif
  145. TBuilder builder, string rootElementName, XmlReader reader,
  146. ExtensionRegistry extensionRegistry) where TBuilder : IBuilderLite
  147. {
  148. return XmlFormatReader.CreateInstance(reader)
  149. .Merge(rootElementName, builder, extensionRegistry);
  150. }
  151. /// <summary>
  152. /// Merges the message from the input stream based on the contentType provided
  153. /// </summary>
  154. /// <typeparam name="TBuilder">A type derived from IBuilderLite</typeparam>
  155. /// <param name="builder">An instance of a message builder</param>
  156. /// <param name="options">Options specific to reading this message and/or content type</param>
  157. /// <param name="contentType">The mime type of the input stream content</param>
  158. /// <param name="input">The stream to read the message from</param>
  159. /// <returns>The same builder instance that was supplied in the builder parameter</returns>
  160. public static TBuilder MergeFrom<TBuilder>(
  161. #if !NOEXTENSIONS
  162. this
  163. #endif
  164. TBuilder builder, MessageFormatOptions options, string contentType, Stream input) where TBuilder : IBuilderLite
  165. {
  166. ICodedInputStream codedInput = MessageFormatFactory.CreateInputStream(options, contentType, input);
  167. codedInput.ReadMessageStart();
  168. builder.WeakMergeFrom(codedInput, options.ExtensionRegistry);
  169. codedInput.ReadMessageEnd();
  170. return builder;
  171. }
  172. #endregion
  173. }
  174. }