aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf/JsonFormatter.cs
diff options
context:
space:
mode:
authorJon Skeet <jonskeet@google.com>2016-01-15 12:02:07 +0000
committerJon Skeet <jonskeet@google.com>2016-01-15 12:02:07 +0000
commitf437b67f600545f432863457a39870cb74675d34 (patch)
tree608f813163899f678c04726117f36a006199b332 /csharp/src/Google.Protobuf/JsonFormatter.cs
parent022a9b267561a3fccfcfaa7023779b969692bf74 (diff)
downloadprotobuf-f437b67f600545f432863457a39870cb74675d34.tar.gz
protobuf-f437b67f600545f432863457a39870cb74675d34.tar.bz2
protobuf-f437b67f600545f432863457a39870cb74675d34.zip
Extra strictness for FieldMask conversion
Diffstat (limited to 'csharp/src/Google.Protobuf/JsonFormatter.cs')
-rw-r--r--csharp/src/Google.Protobuf/JsonFormatter.cs27
1 files changed, 26 insertions, 1 deletions
diff --git a/csharp/src/Google.Protobuf/JsonFormatter.cs b/csharp/src/Google.Protobuf/JsonFormatter.cs
index 563b834e..573ca766 100644
--- a/csharp/src/Google.Protobuf/JsonFormatter.cs
+++ b/csharp/src/Google.Protobuf/JsonFormatter.cs
@@ -224,6 +224,31 @@ 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)
@@ -525,7 +550,7 @@ namespace Google.Protobuf
private void WriteFieldMask(StringBuilder builder, IMessage value)
{
IList paths = (IList) value.Descriptor.Fields[FieldMask.PathsFieldNumber].Accessor.GetValue(value);
- WriteString(builder, string.Join(",", paths.Cast<string>().Select(ToCamelCase)));
+ WriteString(builder, string.Join(",", paths.Cast<string>().Select(ToCamelCaseForFieldMask)));
}
private void WriteAny(StringBuilder builder, IMessage value)