using System; using System.Text; using System.IO; using System.Xml; using Google.ProtocolBuffers.Serialization; using Google.ProtocolBuffers.Serialization.Http; namespace Google.ProtocolBuffers { /// /// Extension methods for using serializers on instances of IMessageLite/IBuilderLite /// public static class Extensions { #region IMessageLite Extension /// /// Serializes the message to JSON text. This is a trivial wrapper /// around Serialization.JsonFormatWriter.WriteMessage. /// public static string ToJson( #if !NOEXTENSIONS this #endif IMessageLite message) { JsonFormatWriter w = JsonFormatWriter.CreateInstance(); w.WriteMessage(message); return w.ToString(); } /// /// Serializes the message to XML text. This is a trivial wrapper /// around Serialization.XmlFormatWriter.WriteMessage. /// 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(); } /// /// Serializes the message to XML text using the element name provided. /// This is a trivial wrapper around Serialization.XmlFormatWriter.WriteMessage. /// 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(); } /// /// Writes the message instance to the stream using the content type provided /// /// An instance of a message /// Options specific to writing this message and/or content type /// The mime type of the content to be written /// The stream to write the message to 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 /// /// Merges a JSON object into this builder and returns /// public static TBuilder MergeFromJson( #if !NOEXTENSIONS this #endif TBuilder builder, string jsonText) where TBuilder : IBuilderLite { return JsonFormatReader.CreateInstance(jsonText) .Merge(builder); } /// /// Merges a JSON object into this builder and returns /// public static TBuilder MergeFromJson( #if !NOEXTENSIONS this #endif TBuilder builder, TextReader reader) where TBuilder : IBuilderLite { return MergeFromJson(builder, reader, ExtensionRegistry.Empty); } /// /// Merges a JSON object into this builder using the extensions provided and returns /// public static TBuilder MergeFromJson( #if !NOEXTENSIONS this #endif TBuilder builder, TextReader reader, ExtensionRegistry extensionRegistry) where TBuilder : IBuilderLite { return JsonFormatReader.CreateInstance(reader) .Merge(builder, extensionRegistry); } /// /// Merges an XML object into this builder and returns /// public static TBuilder MergeFromXml( #if !NOEXTENSIONS this #endif TBuilder builder, XmlReader reader) where TBuilder : IBuilderLite { return MergeFromXml(builder, XmlFormatReader.DefaultRootElementName, reader, ExtensionRegistry.Empty); } /// /// Merges an XML object into this builder and returns /// public static TBuilder MergeFromXml( #if !NOEXTENSIONS this #endif TBuilder builder, string rootElementName, XmlReader reader) where TBuilder : IBuilderLite { return MergeFromXml(builder, rootElementName, reader, ExtensionRegistry.Empty); } /// /// Merges an XML object into this builder using the extensions provided and returns /// public static TBuilder MergeFromXml( #if !NOEXTENSIONS this #endif TBuilder builder, string rootElementName, XmlReader reader, ExtensionRegistry extensionRegistry) where TBuilder : IBuilderLite { return XmlFormatReader.CreateInstance(reader) .Merge(rootElementName, builder, extensionRegistry); } /// /// Merges the message from the input stream based on the contentType provided /// /// A type derived from IBuilderLite /// An instance of a message builder /// Options specific to reading this message and/or content type /// The mime type of the input stream content /// The stream to read the message from /// The same builder instance that was supplied in the builder parameter public static TBuilder MergeFrom( #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 } }