OLD | NEW |
1 #region Copyright notice and license | 1 #region Copyright notice and license |
2 // Protocol Buffers - Google's data interchange format | 2 // Protocol Buffers - Google's data interchange format |
3 // Copyright 2016 Google Inc. All rights reserved. | 3 // Copyright 2016 Google Inc. All rights reserved. |
4 // https://developers.google.com/protocol-buffers/ | 4 // https://developers.google.com/protocol-buffers/ |
5 // | 5 // |
6 // Redistribution and use in source and binary forms, with or without | 6 // Redistribution and use in source and binary forms, with or without |
7 // modification, are permitted provided that the following conditions are | 7 // modification, are permitted provided that the following conditions are |
8 // met: | 8 // met: |
9 // | 9 // |
10 // * Redistributions of source code must retain the above copyright | 10 // * Redistributions of source code must retain the above copyright |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 /// <summary> | 45 /// <summary> |
46 /// Converts a timestamp specified in seconds/nanoseconds to a string. | 46 /// Converts a timestamp specified in seconds/nanoseconds to a string. |
47 /// </summary> | 47 /// </summary> |
48 /// <remarks> | 48 /// <remarks> |
49 /// If the value is a normalized duration in the range described in <c>f
ield_mask.proto</c>, | 49 /// If the value is a normalized duration in the range described in <c>f
ield_mask.proto</c>, |
50 /// <paramref name="diagnosticOnly"/> is ignored. Otherwise, if the para
meter is <c>true</c>, | 50 /// <paramref name="diagnosticOnly"/> is ignored. Otherwise, if the para
meter is <c>true</c>, |
51 /// a JSON object with a warning is returned; if it is <c>false</c>, an
<see cref="InvalidOperationException"/> is thrown. | 51 /// a JSON object with a warning is returned; if it is <c>false</c>, an
<see cref="InvalidOperationException"/> is thrown. |
52 /// </remarks> | 52 /// </remarks> |
53 /// <param name="paths">Paths in the field mask</param> | 53 /// <param name="paths">Paths in the field mask</param> |
54 /// <param name="diagnosticOnly">Determines the handling of non-normaliz
ed values</param> | 54 /// <param name="diagnosticOnly">Determines the handling of non-normaliz
ed values</param> |
55 /// <exception cref="InvalidOperationException">The represented duration
is invalid, and <paramref name="diagnosticOnly"/> is <c>false</c>.</exception> | 55 /// <exception cref="InvalidOperationException">The represented field ma
sk is invalid, and <paramref name="diagnosticOnly"/> is <c>false</c>.</exception
> |
56 internal static string ToJson(IList<string> paths, bool diagnosticOnly) | 56 internal static string ToJson(IList<string> paths, bool diagnosticOnly) |
57 { | 57 { |
58 var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p)); | 58 var firstInvalid = paths.FirstOrDefault(p => !ValidatePath(p)); |
59 if (firstInvalid == null) | 59 if (firstInvalid == null) |
60 { | 60 { |
61 var writer = new StringWriter(); | 61 var writer = new StringWriter(); |
62 JsonFormatter.WriteString(writer, string.Join(",", paths.Select(
JsonFormatter.ToCamelCase))); | 62 #if DOTNET35 |
| 63 var query = paths.Select(JsonFormatter.ToJsonName); |
| 64 JsonFormatter.WriteString(writer, string.Join(",", query.ToArray
())); |
| 65 #else |
| 66 JsonFormatter.WriteString(writer, string.Join(",", paths.Select(
JsonFormatter.ToJsonName))); |
| 67 #endif |
63 return writer.ToString(); | 68 return writer.ToString(); |
64 } | 69 } |
65 else | 70 else |
66 { | 71 { |
67 if (diagnosticOnly) | 72 if (diagnosticOnly) |
68 { | 73 { |
69 var writer = new StringWriter(); | 74 var writer = new StringWriter(); |
70 writer.Write("{ \"@warning\": \"Invalid FieldMask\", \"paths
\": "); | 75 writer.Write("{ \"@warning\": \"Invalid FieldMask\", \"paths
\": "); |
71 JsonFormatter.Default.WriteList(writer, (IList)paths); | 76 JsonFormatter.Default.WriteList(writer, (IList)paths); |
72 writer.Write(" }"); | 77 writer.Write(" }"); |
73 return writer.ToString(); | 78 return writer.ToString(); |
74 } | 79 } |
75 else | 80 else |
76 { | 81 { |
77 throw new InvalidOperationException($"Invalid field mask to
be converted to JSON: {firstInvalid}"); | 82 throw new InvalidOperationException($"Invalid field mask to
be converted to JSON: {firstInvalid}"); |
78 } | 83 } |
79 } | 84 } |
80 } | 85 } |
81 | 86 |
82 /// <summary> | 87 /// <summary> |
83 /// Camel-case converter with added strictness for field mask formatting
. | 88 /// Checks whether the given path is valid for a field mask. |
84 /// </summary> | 89 /// </summary> |
85 /// <exception cref="InvalidOperationException">The field mask is invali
d for JSON representation</exception> | 90 /// <returns>true if the path is valid; false otherwise</returns> |
86 private static bool ValidatePath(string input) | 91 private static bool ValidatePath(string input) |
87 { | 92 { |
88 for (int i = 0; i < input.Length; i++) | 93 for (int i = 0; i < input.Length; i++) |
89 { | 94 { |
90 char c = input[i]; | 95 char c = input[i]; |
91 if (c >= 'A' && c <= 'Z') | 96 if (c >= 'A' && c <= 'Z') |
92 { | 97 { |
93 return false; | 98 return false; |
94 } | 99 } |
95 if (c == '_' && i < input.Length - 1) | 100 if (c == '_' && i < input.Length - 1) |
(...skipping 18 matching lines...) Expand all Loading... |
114 /// diagnose problems - the regular JSON formatter will still throw an e
xception for non-normalized | 119 /// diagnose problems - the regular JSON formatter will still throw an e
xception for non-normalized |
115 /// values. | 120 /// values. |
116 /// </remarks> | 121 /// </remarks> |
117 /// <returns>A string representation of this value.</returns> | 122 /// <returns>A string representation of this value.</returns> |
118 public string ToDiagnosticString() | 123 public string ToDiagnosticString() |
119 { | 124 { |
120 return ToJson(Paths, true); | 125 return ToJson(Paths, true); |
121 } | 126 } |
122 } | 127 } |
123 } | 128 } |
OLD | NEW |