diff options
author | csharptest <roger@csharptest.net> | 2011-09-08 20:02:11 -0500 |
---|---|---|
committer | rogerk <devnull@localhost> | 2011-09-08 20:02:11 -0500 |
commit | c2d2c1adaf447bbc80194d8bce6c4e0442a7f47a (patch) | |
tree | a00d169fff4221e3779cb48e0e4c6001a1d5f604 /src/ProtocolBuffers.Test/TestWriterFormatJson.cs | |
parent | 8f0dcf3df1548a1eff0bed54a9b992f55b8f72d5 (diff) | |
download | protobuf-c2d2c1adaf447bbc80194d8bce6c4e0442a7f47a.tar.gz protobuf-c2d2c1adaf447bbc80194d8bce6c4e0442a7f47a.tar.bz2 protobuf-c2d2c1adaf447bbc80194d8bce6c4e0442a7f47a.zip |
first pass at adding required changes
Diffstat (limited to 'src/ProtocolBuffers.Test/TestWriterFormatJson.cs')
-rw-r--r-- | src/ProtocolBuffers.Test/TestWriterFormatJson.cs | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs index 3f534fc0..341f0b3c 100644 --- a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs +++ b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs @@ -10,6 +10,70 @@ namespace Google.ProtocolBuffers [TestFixture]
public class TestWriterFormatJson
{
+ [Test]
+ public void Example_FromJson()
+ {
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+
+ builder.MergeFromJson(@"{""valid"":true}");
+
+ TestXmlMessage message = builder.Build();
+ Assert.AreEqual(true, message.Valid);
+ }
+
+ [Test]
+ public void Example_ToJson()
+ {
+ TestXmlMessage message =
+ TestXmlMessage.CreateBuilder()
+ .SetValid(true)
+ .Build();
+
+ string json = message.ToJson();
+
+ Assert.AreEqual(@"{""valid"":true}", json);
+ }
+
+ [Test]
+ public void Example_WriteJsonUsingICodedOutputStream()
+ {
+ TestXmlMessage message =
+ TestXmlMessage.CreateBuilder()
+ .SetValid(true)
+ .Build();
+
+ using (TextWriter output = new StringWriter())
+ using (AbstractWriter writer = JsonFormatWriter.CreateInstance(output))
+ {
+ writer.StartMessage(); //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.EndMessage(); //manually write the end message '}'
+ Assert.AreEqual(@"{""valid"":true}", output.ToString());
+ }
+ }
+
+ [Test]
+ public void Example_ReadJsonUsingICodedInputStream()
+ {
+ TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder();
+ AbstractReader reader = JsonFormatReader.CreateInstance(@"{""valid"":true}");
+
+ AbstractReader stream = reader.ReadStartMessage(); //manually read the begin the message '{'
+
+ builder.MergeFrom(stream); //write the message normally
+
+ stream.ReadEndMessage(); //manually read the end message '}'
+ }
+
protected string Content;
[System.Diagnostics.DebuggerNonUserCode]
protected void FormatterAssert<TMessage>(TMessage message, params string[] expecting) where TMessage : IMessageLite
|