diff options
author | Jie Luo <anandolee@gmail.com> | 2015-04-17 17:29:39 -0700 |
---|---|---|
committer | Jie Luo <anandolee@gmail.com> | 2015-04-17 17:29:39 -0700 |
commit | 7646eec9ceb73e1ffa29dd7b226c019c40a68050 (patch) | |
tree | 25233a3a1841d1aea35bcc38e579376c527bedc0 /csharp/src/ProtocolBuffers.Serialization/Extensions.cs | |
parent | 4e63b52088ce409bbe43e4a37863d43b136143f0 (diff) | |
parent | 7eaf4380315624ad34bd7751bfd04c068d2bdd39 (diff) | |
download | protobuf-7646eec9ceb73e1ffa29dd7b226c019c40a68050.tar.gz protobuf-7646eec9ceb73e1ffa29dd7b226c019c40a68050.tar.bz2 protobuf-7646eec9ceb73e1ffa29dd7b226c019c40a68050.zip |
Merge pull request #288 from jtattermusch/csharp_protobuf_reviewed
C# protocol buffers (initial pull request)
Diffstat (limited to 'csharp/src/ProtocolBuffers.Serialization/Extensions.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers.Serialization/Extensions.cs | 213 |
1 files changed, 213 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers.Serialization/Extensions.cs b/csharp/src/ProtocolBuffers.Serialization/Extensions.cs new file mode 100644 index 00000000..8aef0a9e --- /dev/null +++ b/csharp/src/ProtocolBuffers.Serialization/Extensions.cs @@ -0,0 +1,213 @@ +using System;
+using System.Text;
+using System.IO;
+using System.Xml;
+using Google.ProtocolBuffers.Serialization;
+using Google.ProtocolBuffers.Serialization.Http;
+
+namespace Google.ProtocolBuffers
+{
+ /// <summary>
+ /// Extension methods for using serializers on instances of IMessageLite/IBuilderLite
+ /// </summary>
+ public static class Extensions
+ {
+ #region IMessageLite Extension
+ /// <summary>
+ /// Serializes the message to JSON text. This is a trivial wrapper
+ /// around Serialization.JsonFormatWriter.WriteMessage.
+ /// </summary>
+ public static string ToJson(
+#if !NOEXTENSIONS
+ this
+#endif
+ IMessageLite message)
+ {
+ JsonFormatWriter w = JsonFormatWriter.CreateInstance();
+ w.WriteMessage(message);
+ return w.ToString();
+ }
+ /// <summary>
+ /// Serializes the message to XML text. This is a trivial wrapper
+ /// around Serialization.XmlFormatWriter.WriteMessage.
+ /// </summary>
+ public static string ToXml(
+#if !NOEXTENSIONS
+ this
+#endif
+ IMessageLite message)
+ {
+ StringWriter w = new StringWriter(new StringBuilder(4096));
+ XmlFormatWriter.CreateInstance(w).WriteMessage(message);
+ return w.ToString();
+ }
+ /// <summary>
+ /// Serializes the message to XML text using the element name provided.
+ /// This is a trivial wrapper around Serialization.XmlFormatWriter.WriteMessage.
+ /// </summary>
+ public static string ToXml(
+#if !NOEXTENSIONS
+ this
+#endif
+ IMessageLite message, string rootElementName)
+ {
+ StringWriter w = new StringWriter(new StringBuilder(4096));
+ XmlFormatWriter.CreateInstance(w).WriteMessage(rootElementName, message);
+ return w.ToString();
+ }
+
+ /// <summary>
+ /// Writes the message instance to the stream using the content type provided
+ /// </summary>
+ /// <param name="message">An instance of a message</param>
+ /// <param name="options">Options specific to writing this message and/or content type</param>
+ /// <param name="contentType">The mime type of the content to be written</param>
+ /// <param name="output">The stream to write the message to</param>
+ public static void WriteTo(
+#if !NOEXTENSIONS
+ this
+#endif
+ IMessageLite message, MessageFormatOptions options, string contentType, Stream output)
+ {
+ ICodedOutputStream codedOutput = MessageFormatFactory.CreateOutputStream(options, contentType, output);
+
+ // Output the appropriate message preamble
+ codedOutput.WriteMessageStart();
+
+ // Write the message content to the output
+ message.WriteTo(codedOutput);
+
+ // Write the closing message fragment
+ codedOutput.WriteMessageEnd();
+ codedOutput.Flush();
+ }
+
+ #endregion
+ #region IBuilderLite Extensions
+ /// <summary>
+ /// Merges a JSON object into this builder and returns
+ /// </summary>
+ public static TBuilder MergeFromJson<TBuilder>(
+#if !NOEXTENSIONS
+ this
+#endif
+ TBuilder builder, string jsonText) where TBuilder : IBuilderLite
+ {
+ return JsonFormatReader.CreateInstance(jsonText)
+ .Merge(builder);
+ }
+ /// <summary>
+ /// Merges a JSON object into this builder and returns
+ /// </summary>
+ public static TBuilder MergeFromJson<TBuilder>(
+#if !NOEXTENSIONS
+ this
+#endif
+ TBuilder builder, TextReader reader) where TBuilder : IBuilderLite
+ {
+ return MergeFromJson(builder, reader, ExtensionRegistry.Empty);
+ }
+ /// <summary>
+ /// Merges a JSON object into this builder using the extensions provided and returns
+ /// </summary>
+ public static TBuilder MergeFromJson<TBuilder>(
+#if !NOEXTENSIONS
+ this
+#endif
+ TBuilder builder, TextReader reader, ExtensionRegistry extensionRegistry) where TBuilder : IBuilderLite
+ {
+ return JsonFormatReader.CreateInstance(reader)
+ .Merge(builder, extensionRegistry);
+ }
+
+ /// <summary>
+ /// Merges an XML object into this builder and returns
+ /// </summary>
+ public static TBuilder MergeFromXml<TBuilder>(
+#if !NOEXTENSIONS
+ this
+#endif
+ TBuilder builder, XmlReader reader) where TBuilder : IBuilderLite
+ {
+ return MergeFromXml(builder, XmlFormatReader.DefaultRootElementName, reader, ExtensionRegistry.Empty);
+ }
+
+ /// <summary>
+ /// Merges an XML object into this builder and returns
+ /// </summary>
+ public static TBuilder MergeFromXml<TBuilder>(
+#if !NOEXTENSIONS
+ this
+#endif
+ TBuilder builder, string rootElementName, XmlReader reader) where TBuilder : IBuilderLite
+ {
+ return MergeFromXml(builder, rootElementName, reader, ExtensionRegistry.Empty);
+ }
+
+ /// <summary>
+ /// Merges an XML object into this builder using the extensions provided and returns
+ /// </summary>
+ public static TBuilder MergeFromXml<TBuilder>(
+#if !NOEXTENSIONS
+ this
+#endif
+ TBuilder builder, string rootElementName, XmlReader reader,
+ ExtensionRegistry extensionRegistry) where TBuilder : IBuilderLite
+ {
+ return XmlFormatReader.CreateInstance(reader)
+ .Merge(rootElementName, builder, extensionRegistry);
+ }
+
+ /// <summary>
+ /// Merges the message from the input stream based on the contentType provided
+ /// </summary>
+ /// <typeparam name="TBuilder">A type derived from IBuilderLite</typeparam>
+ /// <param name="builder">An instance of a message builder</param>
+ /// <param name="options">Options specific to reading this message and/or content type</param>
+ /// <param name="contentType">The mime type of the input stream content</param>
+ /// <param name="input">The stream to read the message from</param>
+ /// <returns>The same builder instance that was supplied in the builder parameter</returns>
+ public static TBuilder MergeFrom<TBuilder>(
+#if !NOEXTENSIONS
+ this
+#endif
+ TBuilder builder, MessageFormatOptions options, string contentType, Stream input) where TBuilder : IBuilderLite
+ {
+ ICodedInputStream codedInput = MessageFormatFactory.CreateInputStream(options, contentType, input);
+ codedInput.ReadMessageStart();
+ builder.WeakMergeFrom(codedInput, options.ExtensionRegistry);
+ codedInput.ReadMessageEnd();
+ return builder;
+ }
+
+ #endregion
+ #region IRpcServerStub Extensions
+
+ /// <summary>
+ /// Used to implement a service endpoint on an HTTP server. This works with services generated with the
+ /// service_generator_type option set to IRPCDISPATCH.
+ /// </summary>
+ /// <param name="stub">The service execution stub</param>
+ /// <param name="methodName">The name of the method being invoked</param>
+ /// <param name="options">optional arguments for the format reader/writer</param>
+ /// <param name="contentType">The mime type for the input stream</param>
+ /// <param name="input">The input stream</param>
+ /// <param name="responseType">The mime type for the output stream</param>
+ /// <param name="output">The output stream</param>
+ public static void HttpCallMethod(
+#if !NOEXTENSIONS
+ this
+#endif
+ IRpcServerStub stub, string methodName, MessageFormatOptions options,
+ string contentType, Stream input, string responseType, Stream output)
+ {
+ ICodedInputStream codedInput = MessageFormatFactory.CreateInputStream(options, contentType, input);
+ codedInput.ReadMessageStart();
+ IMessageLite response = stub.CallMethod(methodName, codedInput, options.ExtensionRegistry);
+ codedInput.ReadMessageEnd();
+ WriteTo(response, options, responseType, output);
+ }
+
+ #endregion
+ }
+}
|