diff options
author | Jon Skeet <jonskeet@google.com> | 2016-01-15 10:41:56 +0000 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2016-01-15 10:41:56 +0000 |
commit | 730c38ad8c11429bc7ea0310bc1b82f0831b42a6 (patch) | |
tree | efff1d3d8df8934f4891f4f237576013f49c9a79 /csharp/src/Google.Protobuf | |
parent | f262611ff65d5a5d1b4665e2d339f108ef29fcf9 (diff) | |
download | protobuf-730c38ad8c11429bc7ea0310bc1b82f0831b42a6.tar.gz protobuf-730c38ad8c11429bc7ea0310bc1b82f0831b42a6.tar.bz2 protobuf-730c38ad8c11429bc7ea0310bc1b82f0831b42a6.zip |
Support (and test) numeric enum parsing in JSON
Diffstat (limited to 'csharp/src/Google.Protobuf')
-rw-r--r-- | csharp/src/Google.Protobuf/JsonParser.cs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/csharp/src/Google.Protobuf/JsonParser.cs b/csharp/src/Google.Protobuf/JsonParser.cs index f7ebd057..25afd0f2 100644 --- a/csharp/src/Google.Protobuf/JsonParser.cs +++ b/csharp/src/Google.Protobuf/JsonParser.cs @@ -608,6 +608,15 @@ namespace Google.Protobuf throw new InvalidProtocolBufferException($"Value out of range: {value}"); } return (float) value; + case FieldType.Enum: + CheckInteger(value); + var enumValue = field.EnumType.FindValueByNumber((int) value); + if (enumValue == null) + { + throw new InvalidProtocolBufferException($"Invalid enum value: {value} for enum type: {field.EnumType.FullName}"); + } + // Just return it as an int, and let the CLR convert it. + return enumValue.Number; default: throw new InvalidProtocolBufferException($"Unsupported conversion from JSON number for field type {field.FieldType}"); } |