aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-11 10:41:57 -0500
committerrogerk <devnull@localhost>2011-06-11 10:41:57 -0500
commitf292523df9d3a754e496f90bc26c0995a1d332a4 (patch)
treefba719d05650571b8f7f62bbe6bbcb8ff0032244 /src
parent7fc785c1d463c4424cf3e3c8d7fbe796e0197b70 (diff)
downloadprotobuf-f292523df9d3a754e496f90bc26c0995a1d332a4.tar.gz
protobuf-f292523df9d3a754e496f90bc26c0995a1d332a4.tar.bz2
protobuf-f292523df9d3a754e496f90bc26c0995a1d332a4.zip
Added convenience methods for to/from xml and json
Diffstat (limited to 'src')
-rw-r--r--src/ProtoBench/Program.cs2
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs6
-rw-r--r--src/ProtocolBuffers.Test/CompatTests/XmlCompatibilityTests.cs1
-rw-r--r--src/ProtocolBuffers.Test/TestWriterFormatJson.cs22
-rw-r--r--src/ProtocolBuffers.Test/TestWriterFormatXml.cs22
-rw-r--r--src/ProtocolBuffers/AbstractMessage.cs21
-rw-r--r--src/ProtocolBuffers/ExtendableBuilder.cs2
-rw-r--r--src/ProtocolBuffers/ExtendableMessage.cs2
-rw-r--r--src/ProtocolBuffers/GeneratedBuilder.cs2
-rw-r--r--src/ProtocolBuffers/GeneratedMessage.cs32
-rw-r--r--src/ProtocolBuffers/IMessage.cs18
-rw-r--r--src/ProtocolBuffers/Serialization/XmlFormatWriter.cs8
12 files changed, 129 insertions, 9 deletions
diff --git a/src/ProtoBench/Program.cs b/src/ProtoBench/Program.cs
index 23af195c..0dbf1236 100644
--- a/src/ProtoBench/Program.cs
+++ b/src/ProtoBench/Program.cs
@@ -158,7 +158,7 @@ namespace Google.ProtocolBuffers.ProtoBench
} );
RunBenchmark("Serialize to json", jsonBytes.Length, () =>
{
- JsonFormatWriter.CreateInstance(new MemoryStream()).WriteMessage(sampleMessage);
+ JsonFormatWriter.CreateInstance().WriteMessage(sampleMessage);
});
RunBenchmark("Serialize to json via xml", jsonBytes.Length,
() =>
diff --git a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
index b54900bb..3f3d62cd 100644
--- a/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
+++ b/src/ProtocolBuffers.Test/CompatTests/CompatibilityTests.cs
@@ -73,7 +73,7 @@ namespace Google.ProtocolBuffers.CompatTests
#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 @@ namespace Google.ProtocolBuffers.CompatTests
;
}
- 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 @@ namespace Google.ProtocolBuffers.CompatTests
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 14616b74..b7cfca6a 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 88c059d2..052d8f2b 100644
--- a/src/ProtocolBuffers.Test/TestWriterFormatJson.cs
+++ b/src/ProtocolBuffers.Test/TestWriterFormatJson.cs
@@ -32,6 +32,28 @@ namespace Google.ProtocolBuffers
}
[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 2ddf39e5..b5eb60bf 100644
--- a/src/ProtocolBuffers.Test/TestWriterFormatXml.cs
+++ b/src/ProtocolBuffers.Test/TestWriterFormatXml.cs
@@ -13,6 +13,28 @@ namespace Google.ProtocolBuffers
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()
diff --git a/src/ProtocolBuffers/AbstractMessage.cs b/src/ProtocolBuffers/AbstractMessage.cs
index 038fc0c5..52401acd 100644
--- a/src/ProtocolBuffers/AbstractMessage.cs
+++ b/src/ProtocolBuffers/AbstractMessage.cs
@@ -121,6 +121,27 @@ namespace Google.ProtocolBuffers
return TextFormat.PrintToString(this);
}
+ public string ToJson()
+ {
+ Serialization.JsonFormatWriter w = Serialization.JsonFormatWriter.CreateInstance();
+ w.WriteMessage(this);
+ return w.ToString();
+ }
+
+ public string ToXml()
+ {
+ StringWriter w = new StringWriter(new System.Text.StringBuilder(4096));
+ Serialization.XmlFormatWriter.CreateInstance(w).WriteMessage(this);
+ return w.ToString();
+ }
+
+ public string ToXml(string rootElementName)
+ {
+ StringWriter w = new StringWriter(new System.Text.StringBuilder(4096));
+ Serialization.XmlFormatWriter.CreateInstance(w).WriteMessage(rootElementName, this);
+ return w.ToString();
+ }
+
public override sealed void PrintTo(TextWriter writer)
{
TextFormat.Print(this, writer);
diff --git a/src/ProtocolBuffers/ExtendableBuilder.cs b/src/ProtocolBuffers/ExtendableBuilder.cs
index cbe203dd..137762ab 100644
--- a/src/ProtocolBuffers/ExtendableBuilder.cs
+++ b/src/ProtocolBuffers/ExtendableBuilder.cs
@@ -42,7 +42,7 @@ namespace Google.ProtocolBuffers
{
public abstract class ExtendableBuilder<TMessage, TBuilder> : GeneratedBuilder<TMessage, TBuilder>
where TMessage : ExtendableMessage<TMessage, TBuilder>
- where TBuilder : GeneratedBuilder<TMessage, TBuilder>
+ where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
{
protected ExtendableBuilder()
{
diff --git a/src/ProtocolBuffers/ExtendableMessage.cs b/src/ProtocolBuffers/ExtendableMessage.cs
index b5fbac37..e8f9ce41 100644
--- a/src/ProtocolBuffers/ExtendableMessage.cs
+++ b/src/ProtocolBuffers/ExtendableMessage.cs
@@ -43,7 +43,7 @@ namespace Google.ProtocolBuffers
{
public abstract class ExtendableMessage<TMessage, TBuilder> : GeneratedMessage<TMessage, TBuilder>
where TMessage : GeneratedMessage<TMessage, TBuilder>
- where TBuilder : GeneratedBuilder<TMessage, TBuilder>
+ where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
{
protected ExtendableMessage()
{
diff --git a/src/ProtocolBuffers/GeneratedBuilder.cs b/src/ProtocolBuffers/GeneratedBuilder.cs
index 4d4be211..94948e49 100644
--- a/src/ProtocolBuffers/GeneratedBuilder.cs
+++ b/src/ProtocolBuffers/GeneratedBuilder.cs
@@ -49,7 +49,7 @@ namespace Google.ProtocolBuffers
/// </summary>
public abstract class GeneratedBuilder<TMessage, TBuilder> : AbstractBuilder<TMessage, TBuilder>
where TMessage : GeneratedMessage<TMessage, TBuilder>
- where TBuilder : GeneratedBuilder<TMessage, TBuilder>
+ where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
{
/// <summary>
/// Returns the message being built at the moment.
diff --git a/src/ProtocolBuffers/GeneratedMessage.cs b/src/ProtocolBuffers/GeneratedMessage.cs
index 0ec02e88..d030a17f 100644
--- a/src/ProtocolBuffers/GeneratedMessage.cs
+++ b/src/ProtocolBuffers/GeneratedMessage.cs
@@ -50,7 +50,7 @@ namespace Google.ProtocolBuffers
/// </summary>
public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
where TMessage : GeneratedMessage<TMessage, TBuilder>
- where TBuilder : GeneratedBuilder<TMessage, TBuilder>
+ where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
{
private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
@@ -175,5 +175,35 @@ namespace Google.ProtocolBuffers
{
unknownFields = fieldSet;
}
+
+ public static TMessage ParseFromJson(string jsonText)
+ {
+ return Serialization.JsonFormatReader.CreateInstance(jsonText)
+ .Merge(new TBuilder())
+ .Build();
+ }
+
+ public static TMessage ParseFromJson(System.IO.TextReader reader)
+ { return ParseFromJson(reader, ExtensionRegistry.Empty); }
+
+ public static TMessage ParseFromJson(System.IO.TextReader reader, ExtensionRegistry extensionRegistry)
+ {
+ return Serialization.JsonFormatReader.CreateInstance(reader)
+ .Merge(new TBuilder(), extensionRegistry)
+ .Build();
+ }
+
+ public static TMessage ParseFromXml(System.Xml.XmlReader reader)
+ { return ParseFromXml(Serialization.XmlFormatReader.DefaultRootElementName, reader, ExtensionRegistry.Empty); }
+
+ public static TMessage ParseFromXml(string rootElementName, System.Xml.XmlReader reader)
+ { return ParseFromXml(rootElementName, reader, ExtensionRegistry.Empty); }
+
+ public static TMessage ParseFromXml(string rootElementName, System.Xml.XmlReader reader, ExtensionRegistry extensionRegistry)
+ {
+ return Serialization.XmlFormatReader.CreateInstance(reader)
+ .Merge(rootElementName, new TBuilder(), extensionRegistry)
+ .Build();
+ }
}
} \ No newline at end of file
diff --git a/src/ProtocolBuffers/IMessage.cs b/src/ProtocolBuffers/IMessage.cs
index c23bc3f7..514c4bf8 100644
--- a/src/ProtocolBuffers/IMessage.cs
+++ b/src/ProtocolBuffers/IMessage.cs
@@ -185,6 +185,24 @@ namespace Google.ProtocolBuffers
new byte[] ToByteArray();
/// <summary>
+ /// Serializes the message to JSON text. This is a trivial wrapper
+ /// around Serialization.JsonFormatWriter.WriteMessage.
+ /// </summary>
+ string ToJson();
+
+ /// <summary>
+ /// Serializes the message to XML text. This is a trivial wrapper
+ /// around Serialization.XmlFormatWriter.WriteMessage.
+ /// </summary>
+ string ToXml();
+
+ /// <summary>
+ /// Serializes the message to XML text using the element name provided.
+ /// This is a trivial wrapper around Serialization.XmlFormatWriter.WriteMessage.
+ /// </summary>
+ string ToXml(string rootElementName);
+
+ /// <summary>
/// Serializes the message and writes it to the given stream.
/// This is just a wrapper around WriteTo(ICodedOutputStream). This
/// does not flush or close the stream.
diff --git a/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs b/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
index 2bb4f159..1be2f390 100644
--- a/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
+++ b/src/ProtocolBuffers/Serialization/XmlFormatWriter.cs
@@ -20,7 +20,13 @@ namespace Google.ProtocolBuffers.Serialization
static XmlWriterSettings DefaultSettings(Encoding encoding)
{
- return new XmlWriterSettings() { CheckCharacters = false, NewLineHandling = NewLineHandling.Entitize, Encoding = encoding };
+ return new XmlWriterSettings()
+ {
+ CheckCharacters = false,
+ NewLineHandling = NewLineHandling.Entitize,
+ OmitXmlDeclaration = true,
+ Encoding = encoding,
+ };
}
/// <summary>