From 689e4bf5f4d42c4adda3bdbd29d6666f3ddea971 Mon Sep 17 00:00:00 2001 From: Jie Luo Date: Mon, 20 Mar 2017 16:47:23 -0700 Subject: Add FormatEnumAsInt support for Json Format. And scale JsonFormatter.Settings to multiple options. --- csharp/src/Google.Protobuf/JsonFormatter.cs | 56 +++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 7 deletions(-) (limited to 'csharp/src/Google.Protobuf') 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 /// public TypeRegistry TypeRegistry { get; } - // TODO: Work out how we're going to scale this to multiple settings. "WithXyz" methods? + /// + /// Whether to format enums as ints. Defaults to false. + /// + public bool FormatEnumsAsIntegers { get; } + /// /// Creates a new object with the specified formatting of default values @@ -795,11 +806,42 @@ namespace Google.Protobuf /// /// true if default values (0, empty strings etc) should be formatted; false otherwise. /// The to use when formatting messages. - public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) + public Settings(bool formatDefaultValues, TypeRegistry typeRegistry) : this(formatDefaultValues, typeRegistry, false) + { + } + + /// + /// Creates a new object with the specified parameters. + /// + /// true if default values (0, empty strings etc) should be formatted; false otherwise. + /// The to use when formatting messages. TypeRegistry.Empty will be used if it is null. + /// true to format the enums as integers; false to format enums as enum names. + private Settings(bool formatDefaultValues, + TypeRegistry typeRegistry, + bool formatEnumsAsIntegers) { FormatDefaultValues = formatDefaultValues; - TypeRegistry = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry)); + TypeRegistry = typeRegistry ?? TypeRegistry.Empty; + FormatEnumsAsIntegers = formatEnumsAsIntegers; } + + /// + /// Creates a new object with the specified formatting of default values and the current settings. + /// + /// true if default values (0, empty strings etc) should be formatted; false otherwise. + public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers); + + /// + /// Creates a new object with the specified type registry and the current settings. + /// + /// The to use when formatting messages. + public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers); + + /// + /// Creates a new object with the specified enums formatting option and the current settings. + /// + /// true to format the enums as integers; false to format enums as enum names. + 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, -- cgit v1.2.3