aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2015-11-04 09:09:14 +0000
committerGravatar Jon Skeet <jonskeet@google.com>2015-11-04 09:09:14 +0000
commit6a942735497e185bb5fc0a3d1e8698726dc754c9 (patch)
tree365fa8efb3ee4cae43d3df5a2cd62e1db441e11e /csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs
parentb6a32e909b1f58f157c19276af233e44627093f4 (diff)
Move the creation of the "fields by JSON name" dictionary to the descriptor.
Diffstat (limited to 'csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs')
-rw-r--r--csharp/src/Google.Protobuf/Reflection/MessageDescriptor.cs15
1 files changed, 15 insertions, 0 deletions
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<EnumDescriptor> enumTypes;
private readonly IList<FieldDescriptor> fieldsInDeclarationOrder;
private readonly IList<FieldDescriptor> fieldsInNumberOrder;
+ private readonly IDictionary<string, FieldDescriptor> jsonFieldMap;
private readonly FieldCollection fields;
private readonly IList<OneofDescriptor> 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<FieldDescriptor>(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<string, FieldDescriptor>(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...)
+
+ /// <value>
+ /// 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 <c>foo_bar</c>
+ /// in the message would result in an entry with a key <c>fooBar</c>.
+ /// </value>
+ internal IDictionary<string, FieldDescriptor> ByJsonName()
+ {
+ return messageDescriptor.jsonFieldMap;
+ }
+
/// <summary>
/// Retrieves the descriptor for the field with the given number.
/// </summary>