aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/Google.Protobuf/Collections
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2015-09-28 17:28:02 +0100
committerGravatar Jon Skeet <jonskeet@google.com>2015-10-01 13:07:47 +0100
commit9ed6d4da3710daae65cdc60719ebff79a472b648 (patch)
treeb0b8546db228e95ac55f9f4a9b8bc3f4db7001fd /csharp/src/Google.Protobuf/Collections
parentebf3eb630de34105432fb998492346929b577dcb (diff)
Support ToString in RepeatedField and MapField.
This changes how we approach JSON formatting in general - instead of looking at the field a value came from, we just look at the type of the value. It's possible this *could* be slightly inefficient, but if we start caring about JSON performance deeply, we'll probably want to rewrite all of this anyway. It's definitely simpler this way. When we support dynamic messages, we'll need to modify JsonFormatter to handle enum values, as they won't come be "real" .NET enums at that point. It shouldn't be hard to do though.
Diffstat (limited to 'csharp/src/Google.Protobuf/Collections')
-rw-r--r--csharp/src/Google.Protobuf/Collections/MapField.cs19
-rw-r--r--csharp/src/Google.Protobuf/Collections/RepeatedField.cs16
2 files changed, 35 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf/Collections/MapField.cs b/csharp/src/Google.Protobuf/Collections/MapField.cs
index 0fa63bef..c0ed28ae 100644
--- a/csharp/src/Google.Protobuf/Collections/MapField.cs
+++ b/csharp/src/Google.Protobuf/Collections/MapField.cs
@@ -35,6 +35,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
+using System.Text;
using Google.Protobuf.Compatibility;
namespace Google.Protobuf.Collections
@@ -45,10 +46,17 @@ namespace Google.Protobuf.Collections
/// <typeparam name="TKey">Key type in the map. Must be a type supported by Protocol Buffer map keys.</typeparam>
/// <typeparam name="TValue">Value type in the map. Must be a type supported by Protocol Buffers.</typeparam>
/// <remarks>
+ /// <para>
/// This implementation preserves insertion order for simplicity of testing
/// code using maps fields. Overwriting an existing entry does not change the
/// position of that entry within the map. Equality is not order-sensitive.
/// For string keys, the equality comparison is provided by <see cref="StringComparer.Ordinal" />.
+ /// </para>
+ /// <para>
+ /// This implementation does not generally prohibit the use of key/value types which are not
+ /// supported by Protocol Buffers (e.g. using a key type of <code>byte</code>) but nor does it guarantee
+ /// that all operations will work in such cases.
+ /// </para>
/// </remarks>
public sealed class MapField<TKey, TValue> : IDeepCloneable<MapField<TKey, TValue>>, IDictionary<TKey, TValue>, IEquatable<MapField<TKey, TValue>>, IDictionary
{
@@ -482,6 +490,17 @@ namespace Google.Protobuf.Collections
return size;
}
+ /// <summary>
+ /// Returns a string representation of this repeated field, in the same
+ /// way as it would be represented by the default JSON formatter.
+ /// </summary>
+ public override string ToString()
+ {
+ var builder = new StringBuilder();
+ JsonFormatter.Default.WriteDictionary(builder, this);
+ return builder.ToString();
+ }
+
#region IDictionary explicit interface implementation
void IDictionary.Add(object key, object value)
{
diff --git a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs
index d9ced6ec..e3f65afe 100644
--- a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs
+++ b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs
@@ -33,6 +33,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.Text;
using Google.Protobuf.Compatibility;
namespace Google.Protobuf.Collections
@@ -41,6 +42,10 @@ namespace Google.Protobuf.Collections
/// The contents of a repeated field: essentially, a collection with some extra
/// restrictions (no null values) and capabilities (deep cloning).
/// </summary>
+ /// <remarks>
+ /// This implementation does not generally prohibit the use of types which are not
+ /// supported by Protocol Buffers but nor does it guarantee that all operations will work in such cases.
+ /// </remarks>
/// <typeparam name="T">The element type of the repeated field.</typeparam>
public sealed class RepeatedField<T> : IList<T>, IList, IDeepCloneable<RepeatedField<T>>, IEquatable<RepeatedField<T>>
{
@@ -465,6 +470,17 @@ namespace Google.Protobuf.Collections
}
/// <summary>
+ /// Returns a string representation of this repeated field, in the same
+ /// way as it would be represented by the default JSON formatter.
+ /// </summary>
+ public override string ToString()
+ {
+ var builder = new StringBuilder();
+ JsonFormatter.Default.WriteList(builder, this);
+ return builder.ToString();
+ }
+
+ /// <summary>
/// Gets or sets the item at the specified index.
/// </summary>
/// <value>