aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/Google.Protobuf/JsonFormatter.cs
diff options
context:
space:
mode:
authorGravatar Jie Luo <jieluo@google.com>2017-03-20 16:47:23 -0700
committerGravatar Jie Luo <jieluo@google.com>2017-03-24 12:05:52 -0700
commit689e4bf5f4d42c4adda3bdbd29d6666f3ddea971 (patch)
tree4446fba0c1907c64816b30b56e5a0788d2b12f7e /csharp/src/Google.Protobuf/JsonFormatter.cs
parentd59592af61852023f549e91683ade4f062c2df83 (diff)
Add FormatEnumAsInt support for Json Format. And scale JsonFormatter.Settings to multiple options.
Diffstat (limited to 'csharp/src/Google.Protobuf/JsonFormatter.cs')
-rwxr-xr-xcsharp/src/Google.Protobuf/JsonFormatter.cs56
1 files changed, 49 insertions, 7 deletions
diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs
index 90c2e937..4ae10d8b 100755
--- a/csharp/src/Google.Protobuf/JsonFormatter.cs
+++ b/csharp/src/Google.Protobuf/JsonFormatter.cs
@@ -375,14 +375,21 @@ namespace Google.Protobuf
}
else if (value is System.Enum)
{
- string name = OriginalEnumValueHelper.GetOriginalName(value);
- if (name != null)
+ if (settings.FormatEnumsAsIntegers)
{
- WriteString(writer, name);
+ WriteValue(writer, (int)value);
}
else
{
- WriteValue(writer, (int)value);
+ string name = OriginalEnumValueHelper.GetOriginalName(value);
+ if (name != null)
+ {
+ WriteString(writer, name);
+ }
+ else
+ {
+ WriteValue(writer, (int)value);
+ }
}
}
else if (value is float || value is double)
@@ -778,7 +785,11 @@ namespace Google.Protobuf
/// </summary>
public TypeRegistry TypeRegistry { get; }
- // TODO: Work out how we're going to scale this to multiple settings. "WithXyz" methods?
+ /// <summary>
+ /// Whether to format enums as ints. Defaults to false.
+ /// </summary>
+ public bool FormatEnumsAsIntegers { get; }
+
/// <summary>
/// Creates a new <see cref="Settings"/> object with the specified formatting of default values
@@ -795,11 +806,42 @@ namespace Google.Protobuf
/// </summary>
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
- public Settings(bool formatDefaultValues, TypeRegistry typeRegistry)
+ public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) : this(formatDefaultValues, typeRegistry, false)
+ {
+ }
+
+ /// <summary>
+ /// Creates a new <see cref="Settings"/> object with the specified parameters.
+ /// </summary>
+ /// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
+ /// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages. TypeRegistry.Empty will be used if it is null.</param>
+ /// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
+ private Settings(bool formatDefaultValues,
+ TypeRegistry typeRegistry,
+ bool formatEnumsAsIntegers)
{
FormatDefaultValues = formatDefaultValues;
- TypeRegistry = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry));
+ TypeRegistry = typeRegistry ?? TypeRegistry.Empty;
+ FormatEnumsAsIntegers = formatEnumsAsIntegers;
}
+
+ /// <summary>
+ /// Creates a new <see cref="Settings"/> object with the specified formatting of default values and the current settings.
+ /// </summary>
+ /// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
+ public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers);
+
+ /// <summary>
+ /// Creates a new <see cref="Settings"/> object with the specified type registry and the current settings.
+ /// </summary>
+ /// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
+ public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers);
+
+ /// <summary>
+ /// Creates a new <see cref="Settings"/> object with the specified enums formatting option and the current settings.
+ /// </summary>
+ /// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
+ public Settings WithFormatEnumsAsIntegers(bool formatEnumsAsIntegers) => new Settings(FormatDefaultValues, TypeRegistry, formatEnumsAsIntegers);
}
// Effectively a cache of mapping from enum values to the original name as specified in the proto file,