From 6a942735497e185bb5fc0a3d1e8698726dc754c9 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Wed, 4 Nov 2015 09:09:14 +0000 Subject: Move the creation of the "fields by JSON name" dictionary to the descriptor. --- csharp/src/Google.Protobuf/JsonParser.cs | 7 +------ .../src/Google.Protobuf/Reflection/MessageDescriptor.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/csharp/src/Google.Protobuf/JsonParser.cs b/csharp/src/Google.Protobuf/JsonParser.cs index 6d2638d9..9710e675 100644 --- a/csharp/src/Google.Protobuf/JsonParser.cs +++ b/csharp/src/Google.Protobuf/JsonParser.cs @@ -37,7 +37,6 @@ using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; -using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -164,11 +163,7 @@ namespace Google.Protobuf throw new InvalidProtocolBufferException("Expected an object"); } var descriptor = message.Descriptor; - // TODO: Make this more efficient, e.g. by building it once in the descriptor. - // Additionally, we need to consider whether to parse field names in their original proto form, - // and any overrides in the descriptor. But yes, all of this should be in the descriptor somehow... - // the descriptor can expose the dictionary. - var jsonFieldMap = descriptor.Fields.InDeclarationOrder().ToDictionary(field => JsonFormatter.ToCamelCase(field.Name)); + var jsonFieldMap = descriptor.Fields.ByJsonName(); while (true) { token = tokenizer.Next(); diff --git a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs index 82901f1b..e599998e 100644 --- a/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs @@ -62,6 +62,7 @@ namespace Google.Protobuf.Reflection private readonly IList enumTypes; private readonly IList fieldsInDeclarationOrder; private readonly IList fieldsInNumberOrder; + private readonly IDictionary jsonFieldMap; private readonly FieldCollection fields; private readonly IList oneofs; // CLR representation of the type described by this descriptor, if any. @@ -95,6 +96,8 @@ namespace Google.Protobuf.Reflection (field, index) => new FieldDescriptor(field, file, this, index, generatedCodeInfo == null ? null : 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))); file.DescriptorPool.AddSymbol(this); fields = new FieldCollection(this); } @@ -255,6 +258,18 @@ namespace Google.Protobuf.Reflection return messageDescriptor.fieldsInNumberOrder; } + // 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 + /// 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. + /// + internal IDictionary ByJsonName() + { + return messageDescriptor.jsonFieldMap; + } + /// /// Retrieves the descriptor for the field with the given number. /// -- cgit v1.2.3