From 022a9b267561a3fccfcfaa7023779b969692bf74 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 15 Jan 2016 11:39:54 +0000 Subject: Allow the original field name (rather than camel-cased) when parsing JSON --- csharp/src/Google.Protobuf.Test/JsonParserTest.cs | 8 ++++++++ .../Google.Protobuf/Reflection/MessageDescriptor.cs | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) (limited to 'csharp') diff --git a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs index d3c3a489..aa090d12 100644 --- a/csharp/src/Google.Protobuf.Test/JsonParserTest.cs +++ b/csharp/src/Google.Protobuf.Test/JsonParserTest.cs @@ -71,6 +71,14 @@ namespace Google.Protobuf Assert.Throws(() => JsonParser.Default.Parse(json)); } + [Test] + public void OriginalFieldNameAccepted() + { + var json = "{ \"single_int32\": 10 }"; + var expected = new TestAllTypes { SingleInt32 = 10 }; + Assert.AreEqual(expected, TestAllTypes.Parser.ParseJson(json)); + } + [Test] public void SourceContextRoundtrip() { diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index d1732b2e..f43803a6 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -92,11 +92,22 @@ namespace Google.Protobuf.Reflection new FieldDescriptor(field, file, this, index, generatedCodeInfo?.PropertyNames[index])); fieldsInNumberOrder = new ReadOnlyCollection(fieldsInDeclarationOrder.OrderBy(field => field.FieldNumber).ToArray()); // TODO: Use field => field.Proto.JsonName when we're confident it's appropriate. (And then use it in the formatter, too.) - jsonFieldMap = new ReadOnlyDictionary(fieldsInNumberOrder.ToDictionary(field => JsonFormatter.ToCamelCase(field.Name))); + jsonFieldMap = CreateJsonFieldMap(fieldsInNumberOrder); file.DescriptorPool.AddSymbol(this); Fields = new FieldCollection(this); } + private static ReadOnlyDictionary CreateJsonFieldMap(IList fields) + { + var map = new Dictionary(); + foreach (var field in fields) + { + map[JsonFormatter.ToCamelCase(field.Name)] = field; + map[field.Name] = field; + } + return new ReadOnlyDictionary(map); + } + /// /// The brief name of the descriptor's target. /// @@ -255,9 +266,10 @@ namespace Google.Protobuf.Reflection // TODO: consider making this public in the future. (Being conservative for now...) /// - /// Returns a read-only dictionary mapping the field names in this message as they're used + /// Returns a read-only dictionary mapping the field names in this message as they're available /// in the JSON representation to the field descriptors. For example, a field foo_bar - /// in the message would result in an entry with a key fooBar. + /// in the message would result two entries, one with a key fooBar and one with a key + /// foo_bar, both referring to the same field. /// internal IDictionary ByJsonName() => messageDescriptor.jsonFieldMap; -- cgit v1.2.3