Exemple #1
0
        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);
        }
        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());
            }
        }
Exemple #3
0
        public void Example_WriteXmlUsingICodedOutputStream()
        {
            TestXmlMessage message =
                TestXmlMessage.CreateBuilder()
                .SetValid(true)
                .Build();

            using (TextWriter output = new StringWriter())
            {
                ICodedOutputStream writer = XmlFormatWriter.CreateInstance(output);
                writer.WriteMessageStart();      //manually begin the message, output is '{'

                ICodedOutputStream stream = writer;
                message.WriteTo(stream);         //write the message normally

                writer.WriteMessageEnd();        //manually write the end message '}'
                Assert.AreEqual(@"<root><valid>true</valid></root>", output.ToString());
            }
        }
        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);
        }