Added convenience methods for to/from xml and json
diff --git a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
index b54900b..3f3d62c 100644
--- a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
+++ b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
@@ -73,7 +73,7 @@
#region Test message builders
- private static TestAllTypes.Builder AddAllTypes(TestAllTypes.Builder builder)
+ protected static TestAllTypes.Builder AddAllTypes(TestAllTypes.Builder builder)
{
return builder.SetOptionalInt32(1001)
.SetOptionalInt64(1001)
@@ -96,7 +96,7 @@
;
}
- private static TestAllTypes.Builder AddRepeatedTypes(TestAllTypes.Builder builder, int size)
+ protected static TestAllTypes.Builder AddRepeatedTypes(TestAllTypes.Builder builder, int size)
{
//repeated values
for (int i = 0; i < size; i++)
@@ -122,7 +122,7 @@
return builder;
}
- private static TestPackedTypes.Builder AddPackedTypes(TestPackedTypes.Builder builder, int size)
+ protected static TestPackedTypes.Builder AddPackedTypes(TestPackedTypes.Builder builder, int size)
{
for(int i=0; i < size; i++ )
builder.AddPackedInt32(1001)
diff --git a/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs
index 14616b7..b7cfca6 100644
--- a/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs
+++ b/src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs
@@ -1,5 +1,6 @@
using System.IO;
using Google.ProtocolBuffers.Serialization;
+using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;
namespace Google.ProtocolBuffers.CompatTests
diff --git a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs
index 88c059d..052d8f2 100644
--- a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs
+++ b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs
@@ -32,6 +32,28 @@
}
[Test]
+ public void TestToJsonParseFromJson()
+ {
+ TestAllTypes msg = new TestAllTypes.Builder().SetDefaultBool(true).Build();
+ string json = msg.ToJson();
+ Assert.AreEqual("{\"default_bool\":true}", json);
+ TestAllTypes copy = TestAllTypes.ParseFromJson(json);
+ Assert.IsTrue(copy.HasDefaultBool && copy.DefaultBool);
+ Assert.AreEqual(msg, copy);
+ }
+
+ [Test]
+ public void TestToJsonParseFromJsonReader()
+ {
+ TestAllTypes msg = new TestAllTypes.Builder().SetDefaultBool(true).Build();
+ string json = msg.ToJson();
+ Assert.AreEqual("{\"default_bool\":true}", json);
+ TestAllTypes copy = TestAllTypes.ParseFromJson(new StringReader(json));
+ Assert.IsTrue(copy.HasDefaultBool && copy.DefaultBool);
+ Assert.AreEqual(msg, copy);
+ }
+
+ [Test]
public void TestJsonFormatted()
{
TestXmlMessage message = TestXmlMessage.CreateBuilder()
diff --git a/src/ProtocolBuffers.Test/TestWriterFormatXml.cs b/src/ProtocolBuffers.Test/TestWriterFormatXml.cs
index 2ddf39e..b5eb60b 100644
--- a/src/ProtocolBuffers.Test/TestWriterFormatXml.cs
+++ b/src/ProtocolBuffers.Test/TestWriterFormatXml.cs
@@ -13,6 +13,28 @@
public class TestWriterFormatXml
{
[Test]
+ public void TestToXmlParseFromXml()
+ {
+ TestAllTypes msg = new TestAllTypes.Builder().SetDefaultBool(true).Build();
+ string xml = msg.ToXml();
+ Assert.AreEqual("<root><default_bool>true</default_bool></root>", xml);
+ TestAllTypes copy = TestAllTypes.ParseFromXml(XmlReader.Create(new StringReader(xml)));
+ Assert.IsTrue(copy.HasDefaultBool && copy.DefaultBool);
+ Assert.AreEqual(msg, copy);
+ }
+
+ [Test]
+ public void TestToXmlParseFromXmlWithRootName()
+ {
+ TestAllTypes msg = new TestAllTypes.Builder().SetDefaultBool(true).Build();
+ string xml = msg.ToXml("message");
+ Assert.AreEqual("<message><default_bool>true</default_bool></message>", xml);
+ TestAllTypes copy = TestAllTypes.ParseFromXml("message", XmlReader.Create(new StringReader(xml)));
+ Assert.IsTrue(copy.HasDefaultBool && copy.DefaultBool);
+ Assert.AreEqual(msg, copy);
+ }
+
+ [Test]
public void TestEmptyMessage()
{
TestXmlChild message = TestXmlChild.CreateBuilder()