Commit ad2d775e authored by avgweb's avatar avgweb

Replace StringBuilder with TextWriter in JsonFormatter

parent 9242d9b7
...@@ -710,5 +710,14 @@ namespace Google.Protobuf ...@@ -710,5 +710,14 @@ namespace Google.Protobuf
Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16, \"@cInHex\": \"10\" } }", message.ToString()); Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16, \"@cInHex\": \"10\" } }", message.ToString());
Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16 } }", JsonFormatter.Default.Format(message)); Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16 } }", JsonFormatter.Default.Format(message));
} }
[Test]
public void CustomDiagnosticMessage_DirectToTextWriterCall()
{
var message = new ForeignMessage { C = 31 };
var writer = new StringWriter();
JsonFormatter.Default.Format(message, writer);
Assert.AreEqual("{ \"c\": 31 }", writer.ToString());
}
} }
} }
...@@ -34,6 +34,7 @@ using Google.Protobuf.Reflection; ...@@ -34,6 +34,7 @@ using Google.Protobuf.Reflection;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Google.Protobuf.Compatibility; using Google.Protobuf.Compatibility;
...@@ -474,9 +475,9 @@ namespace Google.Protobuf.Collections ...@@ -474,9 +475,9 @@ namespace Google.Protobuf.Collections
/// </summary> /// </summary>
public override string ToString() public override string ToString()
{ {
var builder = new StringBuilder(); var writer = new StringWriter();
JsonFormatter.Default.WriteDictionary(builder, this); JsonFormatter.Default.WriteDictionary(writer, this);
return builder.ToString(); return writer.ToString();
} }
#region IDictionary explicit interface implementation #region IDictionary explicit interface implementation
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Text; using System.Text;
namespace Google.Protobuf.Collections namespace Google.Protobuf.Collections
...@@ -474,9 +475,9 @@ namespace Google.Protobuf.Collections ...@@ -474,9 +475,9 @@ namespace Google.Protobuf.Collections
/// </summary> /// </summary>
public override string ToString() public override string ToString()
{ {
var builder = new StringBuilder(); var writer = new StringWriter();
JsonFormatter.Default.WriteList(builder, this); JsonFormatter.Default.WriteList(writer, this);
return builder.ToString(); return writer.ToString();
} }
/// <summary> /// <summary>
......
This diff is collapsed.
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
...@@ -57,19 +58,19 @@ namespace Google.Protobuf.WellKnownTypes ...@@ -57,19 +58,19 @@ namespace Google.Protobuf.WellKnownTypes
var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p)); var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p));
if (firstInvalid == null) if (firstInvalid == null)
{ {
var builder = new StringBuilder(); var writer = new StringWriter();
JsonFormatter.WriteString(builder, string.Join(",", paths.Select(JsonFormatter.ToCamelCase))); JsonFormatter.WriteString(writer, string.Join(",", paths.Select(JsonFormatter.ToCamelCase)));
return builder.ToString(); return writer.ToString();
} }
else else
{ {
if (diagnosticOnly) if (diagnosticOnly)
{ {
var builder = new StringBuilder(); var writer = new StringWriter();
builder.Append("{ \"@warning\": \"Invalid FieldMask\", \"paths\": "); writer.Write("{ \"@warning\": \"Invalid FieldMask\", \"paths\": ");
JsonFormatter.Default.WriteList(builder, (IList) paths); JsonFormatter.Default.WriteList(writer, (IList)paths);
builder.Append(" }"); writer.Write(" }");
return builder.ToString(); return writer.ToString();
} }
else else
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment