| Index: third_party/protobuf/csharp/src/Google.Protobuf/JsonFormatter.cs
|
| diff --git a/third_party/protobuf/csharp/src/Google.Protobuf/JsonFormatter.cs b/third_party/protobuf/csharp/src/Google.Protobuf/JsonFormatter.cs
|
| index 83772473260dea197a2fd2ee4aa2b8e9bee2caf3..bb1a361e04c5f21b58a78a9b9c5bcf68f6f31638 100644
|
| --- a/third_party/protobuf/csharp/src/Google.Protobuf/JsonFormatter.cs
|
| +++ b/third_party/protobuf/csharp/src/Google.Protobuf/JsonFormatter.cs
|
| @@ -248,80 +248,26 @@ namespace Google.Protobuf
|
| return !first;
|
| }
|
|
|
| - /// <summary>
|
| - /// Camel-case converter with added strictness for field mask formatting.
|
| - /// </summary>
|
| - /// <exception cref="InvalidOperationException">The field mask is invalid for JSON representation</exception>
|
| - private static string ToCamelCaseForFieldMask(string input)
|
| - {
|
| - for (int i = 0; i < input.Length; i++)
|
| - {
|
| - char c = input[i];
|
| - if (c >= 'A' && c <= 'Z')
|
| - {
|
| - throw new InvalidOperationException($"Invalid field mask to be converted to JSON: {input}");
|
| - }
|
| - if (c == '_' && i < input.Length - 1)
|
| - {
|
| - char next = input[i + 1];
|
| - if (next < 'a' || next > 'z')
|
| - {
|
| - throw new InvalidOperationException($"Invalid field mask to be converted to JSON: {input}");
|
| - }
|
| - }
|
| - }
|
| - return ToCamelCase(input);
|
| - }
|
| -
|
| - // Converted from src/google/protobuf/util/internal/utility.cc ToCamelCase
|
| - // TODO: Use the new field in FieldDescriptor.
|
| - internal static string ToCamelCase(string input)
|
| + // Converted from java/core/src/main/java/com/google/protobuf/Descriptors.java
|
| + internal static string ToJsonName(string name)
|
| {
|
| - bool capitalizeNext = false;
|
| - bool wasCap = true;
|
| - bool isCap = false;
|
| - bool firstWord = true;
|
| - StringBuilder result = new StringBuilder(input.Length);
|
| -
|
| - for (int i = 0; i < input.Length; i++, wasCap = isCap)
|
| + StringBuilder result = new StringBuilder(name.Length);
|
| + bool isNextUpperCase = false;
|
| + foreach (char ch in name)
|
| {
|
| - isCap = char.IsUpper(input[i]);
|
| - if (input[i] == '_')
|
| + if (ch == '_')
|
| {
|
| - capitalizeNext = true;
|
| - if (result.Length != 0)
|
| - {
|
| - firstWord = false;
|
| - }
|
| - continue;
|
| + isNextUpperCase = true;
|
| }
|
| - else if (firstWord)
|
| + else if (isNextUpperCase)
|
| {
|
| - // Consider when the current character B is capitalized,
|
| - // first word ends when:
|
| - // 1) following a lowercase: "...aB..."
|
| - // 2) followed by a lowercase: "...ABc..."
|
| - if (result.Length != 0 && isCap &&
|
| - (!wasCap || (i + 1 < input.Length && char.IsLower(input[i + 1]))))
|
| - {
|
| - firstWord = false;
|
| - }
|
| - else
|
| - {
|
| - result.Append(char.ToLowerInvariant(input[i]));
|
| - continue;
|
| - }
|
| + result.Append(char.ToUpperInvariant(ch));
|
| + isNextUpperCase = false;
|
| }
|
| - else if (capitalizeNext)
|
| + else
|
| {
|
| - capitalizeNext = false;
|
| - if (char.IsLower(input[i]))
|
| - {
|
| - result.Append(char.ToUpperInvariant(input[i]));
|
| - continue;
|
| - }
|
| + result.Append(ch);
|
| }
|
| - result.Append(input[i]);
|
| }
|
| return result.ToString();
|
| }
|
| @@ -377,8 +323,16 @@ namespace Google.Protobuf
|
| throw new ArgumentException("Invalid field type");
|
| }
|
| }
|
| -
|
| - private void WriteValue(TextWriter writer, object value)
|
| +
|
| + /// <summary>
|
| + /// Writes a single value to the given writer as JSON. Only types understood by
|
| + /// Protocol Buffers can be written in this way. This method is only exposed for
|
| + /// advanced use cases; most users should be using <see cref="Format(IMessage)"/>
|
| + /// or <see cref="Format(IMessage, TextWriter)"/>.
|
| + /// </summary>
|
| + /// <param name="writer">The writer to write the value to. Must not be null.</param>
|
| + /// <param name="value">The value to write. May be null.</param>
|
| + public void WriteValue(TextWriter writer, object value)
|
| {
|
| if (value == null)
|
| {
|
| @@ -447,15 +401,7 @@ namespace Google.Protobuf
|
| }
|
| else if (value is IMessage)
|
| {
|
| - IMessage message = (IMessage) value;
|
| - if (message.Descriptor.IsWellKnownType)
|
| - {
|
| - WriteWellKnownTypeValue(writer, message.Descriptor, value);
|
| - }
|
| - else
|
| - {
|
| - WriteMessage(writer, (IMessage)value);
|
| - }
|
| + Format((IMessage)value, writer);
|
| }
|
| else
|
| {
|
| @@ -724,20 +670,6 @@ namespace Google.Protobuf
|
| }
|
|
|
| /// <summary>
|
| - /// Returns whether or not a singular value can be represented in JSON.
|
| - /// Currently only relevant for enums, where unknown values can't be represented.
|
| - /// For repeated/map fields, this always returns true.
|
| - /// </summary>
|
| - private bool CanWriteSingleValue(object value)
|
| - {
|
| - if (value is System.Enum)
|
| - {
|
| - return System.Enum.IsDefined(value.GetType(), value);
|
| - }
|
| - return true;
|
| - }
|
| -
|
| - /// <summary>
|
| /// Writes a string (including leading and trailing double quotes) to a builder, escaping as required.
|
| /// </summary>
|
| /// <remarks>
|
| @@ -899,6 +831,16 @@ namespace Google.Protobuf
|
| return originalName;
|
| }
|
|
|
| +#if DOTNET35
|
| + // 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)
|
| + .ToDictionary(f => f.GetValue(null),
|
| + f => (f.GetCustomAttributes(typeof(OriginalNameAttribute), false)
|
| + .FirstOrDefault() as OriginalNameAttribute)
|
| + // If the attribute hasn't been applied, fall back to the name of the field.
|
| + ?.Name ?? f.Name);
|
| +#else
|
| private static Dictionary<object, string> GetNameMapping(System.Type enumType) =>
|
| enumType.GetTypeInfo().DeclaredFields
|
| .Where(f => f.IsStatic)
|
| @@ -907,6 +849,7 @@ namespace Google.Protobuf
|
| .FirstOrDefault()
|
| // If the attribute hasn't been applied, fall back to the name of the field.
|
| ?.Name ?? f.Name);
|
| +#endif
|
| }
|
| }
|
| }
|
|
|