Commit ad2d775e authored by avgweb's avatar avgweb

Replace StringBuilder with TextWriter in JsonFormatter

parent 9242d9b7
......@@ -710,5 +710,14 @@ namespace Google.Protobuf
Assert.AreEqual("{ \"singleForeignMessage\": { \"c\": 16, \"@cInHex\": \"10\" } }", message.ToString());
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;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Google.Protobuf.Compatibility;
......@@ -474,9 +475,9 @@ namespace Google.Protobuf.Collections
/// </summary>
public override string ToString()
{
var builder = new StringBuilder();
JsonFormatter.Default.WriteDictionary(builder, this);
return builder.ToString();
var writer = new StringWriter();
JsonFormatter.Default.WriteDictionary(writer, this);
return writer.ToString();
}
#region IDictionary explicit interface implementation
......
......@@ -33,6 +33,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Google.Protobuf.Collections
......@@ -474,9 +475,9 @@ namespace Google.Protobuf.Collections
/// </summary>
public override string ToString()
{
var builder = new StringBuilder();
JsonFormatter.Default.WriteList(builder, this);
return builder.ToString();
var writer = new StringWriter();
JsonFormatter.Default.WriteList(writer, this);
return writer.ToString();
}
/// <summary>
......
This diff is collapsed.
......@@ -33,6 +33,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
......@@ -57,19 +58,19 @@ namespace Google.Protobuf.WellKnownTypes
var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p));
if (firstInvalid == null)
{
var builder = new StringBuilder();
JsonFormatter.WriteString(builder, string.Join(",", paths.Select(JsonFormatter.ToCamelCase)));
return builder.ToString();
var writer = new StringWriter();
JsonFormatter.WriteString(writer, string.Join(",", paths.Select(JsonFormatter.ToCamelCase)));
return writer.ToString();
}
else
{
if (diagnosticOnly)
{
var builder = new StringBuilder();
builder.Append("{ \"@warning\": \"Invalid FieldMask\", \"paths\": ");
JsonFormatter.Default.WriteList(builder, (IList) paths);
builder.Append(" }");
return builder.ToString();
var writer = new StringWriter();
writer.Write("{ \"@warning\": \"Invalid FieldMask\", \"paths\": ");
JsonFormatter.Default.WriteList(writer, (IList)paths);
writer.Write(" }");
return writer.ToString();
}
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