public void Example_WriteJsonUsingICodedOutputStream() { TestXmlMessage message = TestXmlMessage.CreateBuilder() .SetValid(true) .Build(); using (TextWriter output = new StringWriter()) { ICodedOutputStream writer = JsonFormatWriter.CreateInstance(output); writer.WriteMessageStart(); //manually begin the message, output is '{' writer.Flush(); Assert.AreEqual("{", output.ToString()); ICodedOutputStream stream = writer; message.WriteTo(stream); //write the message normally writer.Flush(); Assert.AreEqual(@"{""valid"":true", output.ToString()); writer.WriteMessageEnd(); //manually write the end message '}' Assert.AreEqual(@"{""valid"":true}", output.ToString()); } }
public void TestXmlReadWriteWithoutRoot() { TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder(); TestXmlMessage message = builder.SetText("abc").SetNumber(123).Build(); string xml; using (StringWriter sw = new StringWriter()) { ICodedOutputStream output = XmlFormatWriter.CreateInstance( XmlWriter.Create(sw, new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Fragment })); message.WriteTo(output); output.Flush(); xml = sw.ToString(); } Assert.AreEqual("<text>abc</text><number>123</number>", xml); TestXmlMessage copy; using (XmlReader xr = XmlReader.Create(new StringReader(xml), new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment })) { ICodedInputStream input = XmlFormatReader.CreateInstance(xr); copy = TestXmlMessage.CreateBuilder().MergeFrom(input).Build(); } Assert.AreEqual(message, copy); }
/// <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(); }
public void TestReadWriteJsonWithoutRoot() { TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder(); TestXmlMessage message = builder.SetText("abc").SetNumber(123).Build(); string Json; using (StringWriter sw = new StringWriter()) { ICodedOutputStream output = JsonFormatWriter.CreateInstance(sw); message.WriteTo(output); output.Flush(); Json = sw.ToString(); } Assert.AreEqual(@"""text"":""abc"",""number"":123", Json); ICodedInputStream input = JsonFormatReader.CreateInstance(Json); TestXmlMessage copy = TestXmlMessage.CreateBuilder().MergeFrom(input).Build(); Assert.AreEqual(message, copy); }