aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf
diff options
context:
space:
mode:
Diffstat (limited to 'csharp/src/Google.Protobuf')
-rwxr-xr-xcsharp/src/Google.Protobuf/JsonFormatter.cs5
-rw-r--r--csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs9
2 files changed, 13 insertions, 1 deletions
diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs
index 05282775..90c2e937 100755
--- a/csharp/src/Google.Protobuf/JsonFormatter.cs
+++ b/csharp/src/Google.Protobuf/JsonFormatter.cs
@@ -835,6 +835,9 @@ namespace Google.Protobuf
// TODO: Consider adding functionality to TypeExtensions to avoid this difference.
private static Dictionary<object, string> GetNameMapping(System.Type enumType) =>
enumType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
+ .Where(f => (f.GetCustomAttributes(typeof(OriginalNameAttribute), false)
+ .FirstOrDefault() as OriginalNameAttribute)
+ ?.PreferredAlias ?? true)
.ToDictionary(f => f.GetValue(null),
f => (f.GetCustomAttributes(typeof(OriginalNameAttribute), false)
.FirstOrDefault() as OriginalNameAttribute)
@@ -844,6 +847,8 @@ namespace Google.Protobuf
private static Dictionary<object, string> GetNameMapping(System.Type enumType) =>
enumType.GetTypeInfo().DeclaredFields
.Where(f => f.IsStatic)
+ .Where(f => f.GetCustomAttributes<OriginalNameAttribute>()
+ .FirstOrDefault()?.PreferredAlias ?? true)
.ToDictionary(f => f.GetValue(null),
f => f.GetCustomAttributes<OriginalNameAttribute>()
.FirstOrDefault()
diff --git a/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs b/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs
index 27f9ab98..07d0fd99 100644
--- a/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs
+++ b/csharp/src/Google.Protobuf/Reflection/OriginalNameAttribute.cs
@@ -47,12 +47,19 @@ namespace Google.Protobuf.Reflection
public string Name { get; set; }
/// <summary>
+ /// If the name is preferred in the .proto file.
+ /// </summary>
+ public bool PreferredAlias { get; set; }
+
+ /// <summary>
/// Constructs a new attribute instance for the given name.
/// </summary>
/// <param name="name">The name of the element in the .proto file.</param>
public OriginalNameAttribute(string name)
{
Name = ProtoPreconditions.CheckNotNull(name, nameof(name));
+ PreferredAlias = true;
}
+
}
-} \ No newline at end of file
+}