ADD: added new version of protobuf
This commit is contained in:
@@ -62,13 +62,9 @@ namespace Google.Protobuf
|
||||
internal const string AnyTypeUrlField = "@type";
|
||||
internal const string AnyDiagnosticValueField = "@value";
|
||||
internal const string AnyWellKnownTypeValueField = "value";
|
||||
private const string TypeUrlPrefix = "type.googleapis.com";
|
||||
private const string NameValueSeparator = ": ";
|
||||
private const string ValueSeparator = ", ";
|
||||
private const string MultilineValueSeparator = ",";
|
||||
private const char ObjectOpenBracket = '{';
|
||||
private const char ObjectCloseBracket = '}';
|
||||
private const char ListBracketOpen = '[';
|
||||
private const char ListBracketClose = ']';
|
||||
private const string PropertySeparator = ", ";
|
||||
|
||||
/// <summary>
|
||||
/// Returns a formatter using the default settings.
|
||||
@@ -145,26 +141,11 @@ namespace Google.Protobuf
|
||||
/// Formats the specified message as JSON.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to format.</param>
|
||||
/// <remarks>This method delegates to <c>Format(IMessage, int)</c> with <c>indentationLevel = 0</c>.</remarks>
|
||||
/// <returns>The formatted message.</returns>
|
||||
public string Format(IMessage message) => Format(message, indentationLevel: 0);
|
||||
|
||||
/// <summary>
|
||||
/// Formats the specified message as JSON.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to format.</param>
|
||||
/// <param name="indentationLevel">Indentation level to start at.</param>
|
||||
/// <remarks>To keep consistent indentation when embedding a message inside another JSON string, set <paramref name="indentationLevel"/>. E.g:
|
||||
/// <code>
|
||||
/// var response = $@"{{
|
||||
/// ""data"": { Format(message, indentationLevel: 1) }
|
||||
/// }}"</code>
|
||||
/// </remarks>
|
||||
/// <returns>The formatted message.</returns>
|
||||
public string Format(IMessage message, int indentationLevel)
|
||||
public string Format(IMessage message)
|
||||
{
|
||||
var writer = new StringWriter();
|
||||
Format(message, writer, indentationLevel);
|
||||
Format(message, writer);
|
||||
return writer.ToString();
|
||||
}
|
||||
|
||||
@@ -173,29 +154,19 @@ namespace Google.Protobuf
|
||||
/// </summary>
|
||||
/// <param name="message">The message to format.</param>
|
||||
/// <param name="writer">The TextWriter to write the formatted message to.</param>
|
||||
/// <remarks>This method delegates to <c>Format(IMessage, TextWriter, int)</c> with <c>indentationLevel = 0</c>.</remarks>
|
||||
/// <returns>The formatted message.</returns>
|
||||
public void Format(IMessage message, TextWriter writer) => Format(message, writer, indentationLevel: 0);
|
||||
|
||||
/// <summary>
|
||||
/// Formats the specified message as JSON. When <see cref="Settings.Indentation"/> is not null, start indenting at the specified <paramref name="indentationLevel"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">The message to format.</param>
|
||||
/// <param name="writer">The TextWriter to write the formatted message to.</param>
|
||||
/// <param name="indentationLevel">Indentation level to start at.</param>
|
||||
/// <remarks>To keep consistent indentation when embedding a message inside another JSON string, set <paramref name="indentationLevel"/>.</remarks>
|
||||
public void Format(IMessage message, TextWriter writer, int indentationLevel)
|
||||
public void Format(IMessage message, TextWriter writer)
|
||||
{
|
||||
ProtoPreconditions.CheckNotNull(message, nameof(message));
|
||||
ProtoPreconditions.CheckNotNull(writer, nameof(writer));
|
||||
|
||||
if (message.Descriptor.IsWellKnownType)
|
||||
{
|
||||
WriteWellKnownTypeValue(writer, message.Descriptor, message, indentationLevel);
|
||||
WriteWellKnownTypeValue(writer, message.Descriptor, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteMessage(writer, message, indentationLevel);
|
||||
WriteMessage(writer, message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,7 +193,7 @@ namespace Google.Protobuf
|
||||
return diagnosticFormatter.Format(message);
|
||||
}
|
||||
|
||||
private void WriteMessage(TextWriter writer, IMessage message, int indentationLevel)
|
||||
private void WriteMessage(TextWriter writer, IMessage message)
|
||||
{
|
||||
if (message == null)
|
||||
{
|
||||
@@ -231,19 +202,19 @@ namespace Google.Protobuf
|
||||
}
|
||||
if (DiagnosticOnly)
|
||||
{
|
||||
if (message is ICustomDiagnosticMessage customDiagnosticMessage)
|
||||
ICustomDiagnosticMessage customDiagnosticMessage = message as ICustomDiagnosticMessage;
|
||||
if (customDiagnosticMessage != null)
|
||||
{
|
||||
writer.Write(customDiagnosticMessage.ToDiagnosticString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
WriteBracketOpen(writer, ObjectOpenBracket);
|
||||
bool writtenFields = WriteMessageFields(writer, message, false, indentationLevel + 1);
|
||||
WriteBracketClose(writer, ObjectCloseBracket, writtenFields, indentationLevel);
|
||||
writer.Write("{ ");
|
||||
bool writtenFields = WriteMessageFields(writer, message, false);
|
||||
writer.Write(writtenFields ? " }" : "}");
|
||||
}
|
||||
|
||||
private bool WriteMessageFields(TextWriter writer, IMessage message, bool assumeFirstFieldWritten, int indentationLevel)
|
||||
private bool WriteMessageFields(TextWriter writer, IMessage message, bool assumeFirstFieldWritten)
|
||||
{
|
||||
var fields = message.Descriptor.Fields;
|
||||
bool first = !assumeFirstFieldWritten;
|
||||
@@ -257,8 +228,10 @@ namespace Google.Protobuf
|
||||
continue;
|
||||
}
|
||||
|
||||
MaybeWriteValueSeparator(writer, first);
|
||||
MaybeWriteValueWhitespace(writer, indentationLevel);
|
||||
if (!first)
|
||||
{
|
||||
writer.Write(PropertySeparator);
|
||||
}
|
||||
|
||||
if (settings.PreserveProtoFieldNames)
|
||||
{
|
||||
@@ -269,23 +242,13 @@ namespace Google.Protobuf
|
||||
WriteString(writer, accessor.Descriptor.JsonName);
|
||||
}
|
||||
writer.Write(NameValueSeparator);
|
||||
WriteValue(writer, value, indentationLevel);
|
||||
WriteValue(writer, value);
|
||||
|
||||
first = false;
|
||||
}
|
||||
return !first;
|
||||
}
|
||||
|
||||
private void MaybeWriteValueSeparator(TextWriter writer, bool first)
|
||||
{
|
||||
if (first)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
writer.Write(settings.Indentation == null ? ValueSeparator : MultilineValueSeparator);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether or not a field value should be serialized according to the field,
|
||||
/// its value in the message, and the settings of this formatter.
|
||||
@@ -357,20 +320,39 @@ namespace Google.Protobuf
|
||||
IList list = (IList) value;
|
||||
return list.Count == 0;
|
||||
}
|
||||
return descriptor.FieldType switch
|
||||
switch (descriptor.FieldType)
|
||||
{
|
||||
FieldType.Bool => (bool) value == false,
|
||||
FieldType.Bytes => (ByteString) value == ByteString.Empty,
|
||||
FieldType.String => (string) value == "",
|
||||
FieldType.Double => (double) value == 0.0,
|
||||
FieldType.SInt32 or FieldType.Int32 or FieldType.SFixed32 or FieldType.Enum => (int) value == 0,
|
||||
FieldType.Fixed32 or FieldType.UInt32 => (uint) value == 0,
|
||||
FieldType.Fixed64 or FieldType.UInt64 => (ulong) value == 0,
|
||||
FieldType.SFixed64 or FieldType.Int64 or FieldType.SInt64 => (long) value == 0,
|
||||
FieldType.Float => (float) value == 0f,
|
||||
FieldType.Message or FieldType.Group => value == null,
|
||||
_ => throw new ArgumentException("Invalid field type"),
|
||||
};
|
||||
case FieldType.Bool:
|
||||
return (bool) value == false;
|
||||
case FieldType.Bytes:
|
||||
return (ByteString) value == ByteString.Empty;
|
||||
case FieldType.String:
|
||||
return (string) value == "";
|
||||
case FieldType.Double:
|
||||
return (double) value == 0.0;
|
||||
case FieldType.SInt32:
|
||||
case FieldType.Int32:
|
||||
case FieldType.SFixed32:
|
||||
case FieldType.Enum:
|
||||
return (int) value == 0;
|
||||
case FieldType.Fixed32:
|
||||
case FieldType.UInt32:
|
||||
return (uint) value == 0;
|
||||
case FieldType.Fixed64:
|
||||
case FieldType.UInt64:
|
||||
return (ulong) value == 0;
|
||||
case FieldType.SFixed64:
|
||||
case FieldType.Int64:
|
||||
case FieldType.SInt64:
|
||||
return (long) value == 0;
|
||||
case FieldType.Float:
|
||||
return (float) value == 0f;
|
||||
case FieldType.Message:
|
||||
case FieldType.Group: // Never expect to get this, but...
|
||||
return value == null;
|
||||
default:
|
||||
throw new ArgumentException("Invalid field type");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -381,46 +363,34 @@ namespace Google.Protobuf
|
||||
/// </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>
|
||||
/// <remarks>Delegates to <c>WriteValue(TextWriter, object, int)</c> with <c>indentationLevel = 0</c>.</remarks>
|
||||
public void WriteValue(TextWriter writer, object value) => WriteValue(writer, value, 0);
|
||||
|
||||
/// <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>
|
||||
/// <param name="indentationLevel">The current indentationLevel. Not used when <see cref="Settings.Indentation"/> is null.</param>
|
||||
public void WriteValue(TextWriter writer, object value, int indentationLevel)
|
||||
public void WriteValue(TextWriter writer, object value)
|
||||
{
|
||||
if (value == null || value is NullValue)
|
||||
{
|
||||
WriteNull(writer);
|
||||
}
|
||||
else if (value is bool b)
|
||||
else if (value is bool)
|
||||
{
|
||||
writer.Write(b ? "true" : "false");
|
||||
writer.Write((bool)value ? "true" : "false");
|
||||
}
|
||||
else if (value is ByteString byteString)
|
||||
else if (value is ByteString)
|
||||
{
|
||||
// Nothing in Base64 needs escaping
|
||||
writer.Write('"');
|
||||
writer.Write(byteString.ToBase64());
|
||||
writer.Write(((ByteString)value).ToBase64());
|
||||
writer.Write('"');
|
||||
}
|
||||
else if (value is string str)
|
||||
else if (value is string)
|
||||
{
|
||||
WriteString(writer, str);
|
||||
WriteString(writer, (string)value);
|
||||
}
|
||||
else if (value is IDictionary dictionary)
|
||||
else if (value is IDictionary)
|
||||
{
|
||||
WriteDictionary(writer, dictionary, indentationLevel);
|
||||
WriteDictionary(writer, (IDictionary)value);
|
||||
}
|
||||
else if (value is IList list)
|
||||
else if (value is IList)
|
||||
{
|
||||
WriteList(writer, list, indentationLevel);
|
||||
WriteList(writer, (IList)value);
|
||||
}
|
||||
else if (value is int || value is uint)
|
||||
{
|
||||
@@ -467,9 +437,9 @@ namespace Google.Protobuf
|
||||
writer.Write(text);
|
||||
}
|
||||
}
|
||||
else if (value is IMessage message)
|
||||
else if (value is IMessage)
|
||||
{
|
||||
Format(message, writer, indentationLevel);
|
||||
Format((IMessage)value, writer);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -483,7 +453,7 @@ namespace Google.Protobuf
|
||||
/// values are using the embedded well-known types, in order to allow for dynamic messages
|
||||
/// in the future.
|
||||
/// </summary>
|
||||
private void WriteWellKnownTypeValue(TextWriter writer, MessageDescriptor descriptor, object value, int indentationLevel)
|
||||
private void WriteWellKnownTypeValue(TextWriter writer, MessageDescriptor descriptor, object value)
|
||||
{
|
||||
// Currently, we can never actually get here, because null values are always handled by the caller. But if we *could*,
|
||||
// this would do the right thing.
|
||||
@@ -499,8 +469,9 @@ namespace Google.Protobuf
|
||||
// WriteValue will do the right thing.)
|
||||
if (descriptor.IsWrapperType)
|
||||
{
|
||||
if (value is IMessage message)
|
||||
if (value is IMessage)
|
||||
{
|
||||
var message = (IMessage) value;
|
||||
value = message.Descriptor.Fields[WrappersReflection.WrapperValueFieldNumber].Accessor.GetValue(message);
|
||||
}
|
||||
WriteValue(writer, value);
|
||||
@@ -523,26 +494,26 @@ namespace Google.Protobuf
|
||||
}
|
||||
if (descriptor.FullName == Struct.Descriptor.FullName)
|
||||
{
|
||||
WriteStruct(writer, (IMessage)value, indentationLevel);
|
||||
WriteStruct(writer, (IMessage)value);
|
||||
return;
|
||||
}
|
||||
if (descriptor.FullName == ListValue.Descriptor.FullName)
|
||||
{
|
||||
var fieldAccessor = descriptor.Fields[ListValue.ValuesFieldNumber].Accessor;
|
||||
WriteList(writer, (IList)fieldAccessor.GetValue((IMessage)value), indentationLevel);
|
||||
WriteList(writer, (IList)fieldAccessor.GetValue((IMessage)value));
|
||||
return;
|
||||
}
|
||||
if (descriptor.FullName == Value.Descriptor.FullName)
|
||||
{
|
||||
WriteStructFieldValue(writer, (IMessage)value, indentationLevel);
|
||||
WriteStructFieldValue(writer, (IMessage)value);
|
||||
return;
|
||||
}
|
||||
if (descriptor.FullName == Any.Descriptor.FullName)
|
||||
{
|
||||
WriteAny(writer, (IMessage)value, indentationLevel);
|
||||
WriteAny(writer, (IMessage)value);
|
||||
return;
|
||||
}
|
||||
WriteMessage(writer, (IMessage)value, indentationLevel);
|
||||
WriteMessage(writer, (IMessage)value);
|
||||
}
|
||||
|
||||
private void WriteTimestamp(TextWriter writer, IMessage value)
|
||||
@@ -570,7 +541,7 @@ namespace Google.Protobuf
|
||||
writer.Write(FieldMask.ToJson(paths, DiagnosticOnly));
|
||||
}
|
||||
|
||||
private void WriteAny(TextWriter writer, IMessage value, int indentationLevel)
|
||||
private void WriteAny(TextWriter writer, IMessage value)
|
||||
{
|
||||
if (DiagnosticOnly)
|
||||
{
|
||||
@@ -587,23 +558,23 @@ namespace Google.Protobuf
|
||||
throw new InvalidOperationException($"Type registry has no descriptor for type name '{typeName}'");
|
||||
}
|
||||
IMessage message = descriptor.Parser.ParseFrom(data);
|
||||
WriteBracketOpen(writer, ObjectOpenBracket);
|
||||
writer.Write("{ ");
|
||||
WriteString(writer, AnyTypeUrlField);
|
||||
writer.Write(NameValueSeparator);
|
||||
WriteString(writer, typeUrl);
|
||||
|
||||
if (descriptor.IsWellKnownType)
|
||||
{
|
||||
writer.Write(ValueSeparator);
|
||||
writer.Write(PropertySeparator);
|
||||
WriteString(writer, AnyWellKnownTypeValueField);
|
||||
writer.Write(NameValueSeparator);
|
||||
WriteWellKnownTypeValue(writer, descriptor, message, indentationLevel);
|
||||
WriteWellKnownTypeValue(writer, descriptor, message);
|
||||
}
|
||||
else
|
||||
{
|
||||
WriteMessageFields(writer, message, true, indentationLevel);
|
||||
WriteMessageFields(writer, message, true);
|
||||
}
|
||||
WriteBracketClose(writer, ObjectCloseBracket, true, indentationLevel);
|
||||
writer.Write(" }");
|
||||
}
|
||||
|
||||
private void WriteDiagnosticOnlyAny(TextWriter writer, IMessage value)
|
||||
@@ -614,7 +585,7 @@ namespace Google.Protobuf
|
||||
WriteString(writer, AnyTypeUrlField);
|
||||
writer.Write(NameValueSeparator);
|
||||
WriteString(writer, typeUrl);
|
||||
writer.Write(ValueSeparator);
|
||||
writer.Write(PropertySeparator);
|
||||
WriteString(writer, AnyDiagnosticValueField);
|
||||
writer.Write(NameValueSeparator);
|
||||
writer.Write('"');
|
||||
@@ -623,9 +594,9 @@ namespace Google.Protobuf
|
||||
writer.Write(" }");
|
||||
}
|
||||
|
||||
private void WriteStruct(TextWriter writer, IMessage message, int indentationLevel)
|
||||
private void WriteStruct(TextWriter writer, IMessage message)
|
||||
{
|
||||
WriteBracketOpen(writer, ObjectOpenBracket);
|
||||
writer.Write("{ ");
|
||||
IDictionary fields = (IDictionary) message.Descriptor.Fields[Struct.FieldsFieldNumber].Accessor.GetValue(message);
|
||||
bool first = true;
|
||||
foreach (DictionaryEntry entry in fields)
|
||||
@@ -637,17 +608,19 @@ namespace Google.Protobuf
|
||||
throw new InvalidOperationException("Struct fields cannot have an empty key or a null value.");
|
||||
}
|
||||
|
||||
MaybeWriteValueSeparator(writer, first);
|
||||
MaybeWriteValueWhitespace(writer, indentationLevel + 1);
|
||||
if (!first)
|
||||
{
|
||||
writer.Write(PropertySeparator);
|
||||
}
|
||||
WriteString(writer, key);
|
||||
writer.Write(NameValueSeparator);
|
||||
WriteStructFieldValue(writer, value, indentationLevel + 1);
|
||||
WriteStructFieldValue(writer, value);
|
||||
first = false;
|
||||
}
|
||||
WriteBracketClose(writer, ObjectCloseBracket, !first, indentationLevel);
|
||||
writer.Write(first ? "}" : " }");
|
||||
}
|
||||
|
||||
private void WriteStructFieldValue(TextWriter writer, IMessage message, int indentationLevel)
|
||||
private void WriteStructFieldValue(TextWriter writer, IMessage message)
|
||||
{
|
||||
var specifiedField = message.Descriptor.Oneofs[0].Accessor.GetCaseFieldDescriptor(message);
|
||||
if (specifiedField == null)
|
||||
@@ -668,7 +641,7 @@ namespace Google.Protobuf
|
||||
case Value.ListValueFieldNumber:
|
||||
// Structs and ListValues are nested messages, and already well-known types.
|
||||
var nestedMessage = (IMessage) specifiedField.Accessor.GetValue(message);
|
||||
WriteWellKnownTypeValue(writer, nestedMessage.Descriptor, nestedMessage, indentationLevel);
|
||||
WriteWellKnownTypeValue(writer, nestedMessage.Descriptor, nestedMessage);
|
||||
return;
|
||||
case Value.NullValueFieldNumber:
|
||||
WriteNull(writer);
|
||||
@@ -678,40 +651,43 @@ namespace Google.Protobuf
|
||||
}
|
||||
}
|
||||
|
||||
internal void WriteList(TextWriter writer, IList list, int indentationLevel = 0)
|
||||
internal void WriteList(TextWriter writer, IList list)
|
||||
{
|
||||
WriteBracketOpen(writer, ListBracketOpen);
|
||||
|
||||
writer.Write("[ ");
|
||||
bool first = true;
|
||||
foreach (var value in list)
|
||||
{
|
||||
MaybeWriteValueSeparator(writer, first);
|
||||
MaybeWriteValueWhitespace(writer, indentationLevel + 1);
|
||||
WriteValue(writer, value, indentationLevel + 1);
|
||||
if (!first)
|
||||
{
|
||||
writer.Write(PropertySeparator);
|
||||
}
|
||||
WriteValue(writer, value);
|
||||
first = false;
|
||||
}
|
||||
|
||||
WriteBracketClose(writer, ListBracketClose, !first, indentationLevel);
|
||||
writer.Write(first ? "]" : " ]");
|
||||
}
|
||||
|
||||
internal void WriteDictionary(TextWriter writer, IDictionary dictionary, int indentationLevel = 0)
|
||||
internal void WriteDictionary(TextWriter writer, IDictionary dictionary)
|
||||
{
|
||||
WriteBracketOpen(writer, ObjectOpenBracket);
|
||||
|
||||
writer.Write("{ ");
|
||||
bool first = true;
|
||||
// This will box each pair. Could use IDictionaryEnumerator, but that's ugly in terms of disposal.
|
||||
foreach (DictionaryEntry pair in dictionary)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
writer.Write(PropertySeparator);
|
||||
}
|
||||
string keyText;
|
||||
if (pair.Key is string s)
|
||||
if (pair.Key is string)
|
||||
{
|
||||
keyText = s;
|
||||
keyText = (string) pair.Key;
|
||||
}
|
||||
else if (pair.Key is bool b)
|
||||
else if (pair.Key is bool)
|
||||
{
|
||||
keyText = b ? "true" : "false";
|
||||
keyText = (bool) pair.Key ? "true" : "false";
|
||||
}
|
||||
else if (pair.Key is int || pair.Key is uint || pair.Key is long || pair.Key is ulong)
|
||||
else if (pair.Key is int || pair.Key is uint | pair.Key is long || pair.Key is ulong)
|
||||
{
|
||||
keyText = ((IFormattable) pair.Key).ToString("d", CultureInfo.InvariantCulture);
|
||||
}
|
||||
@@ -723,16 +699,12 @@ namespace Google.Protobuf
|
||||
}
|
||||
throw new ArgumentException("Unhandled dictionary key type: " + pair.Key.GetType());
|
||||
}
|
||||
|
||||
MaybeWriteValueSeparator(writer, first);
|
||||
MaybeWriteValueWhitespace(writer, indentationLevel + 1);
|
||||
WriteString(writer, keyText);
|
||||
writer.Write(NameValueSeparator);
|
||||
WriteValue(writer, pair.Value);
|
||||
first = false;
|
||||
}
|
||||
|
||||
WriteBracketClose(writer, ObjectCloseBracket, !first, indentationLevel);
|
||||
writer.Write(first ? "}" : " }");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -816,49 +788,6 @@ namespace Google.Protobuf
|
||||
writer.Write(Hex[(c >> 0) & 0xf]);
|
||||
}
|
||||
|
||||
private void WriteBracketOpen(TextWriter writer, char openChar)
|
||||
{
|
||||
writer.Write(openChar);
|
||||
if (settings.Indentation == null)
|
||||
{
|
||||
writer.Write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteBracketClose(TextWriter writer, char closeChar, bool hasFields, int indentationLevel)
|
||||
{
|
||||
if (hasFields)
|
||||
{
|
||||
if (settings.Indentation != null)
|
||||
{
|
||||
writer.WriteLine();
|
||||
WriteIndentation(writer, indentationLevel);
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.Write(" ");
|
||||
}
|
||||
}
|
||||
|
||||
writer.Write(closeChar);
|
||||
}
|
||||
|
||||
private void MaybeWriteValueWhitespace(TextWriter writer, int indentationLevel)
|
||||
{
|
||||
if (settings.Indentation != null) {
|
||||
writer.WriteLine();
|
||||
WriteIndentation(writer, indentationLevel);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteIndentation(TextWriter writer, int indentationLevel)
|
||||
{
|
||||
for (int i = 0; i < indentationLevel; i++)
|
||||
{
|
||||
writer.Write(settings.Indentation);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Settings controlling JSON formatting.
|
||||
/// </summary>
|
||||
@@ -899,10 +828,6 @@ namespace Google.Protobuf
|
||||
/// </summary>
|
||||
public bool PreserveProtoFieldNames { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Indentation string, used for formatting. Setting null disables indentation.
|
||||
/// </summary>
|
||||
public string Indentation { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Settings"/> object with the specified formatting of default values
|
||||
@@ -930,54 +855,40 @@ namespace Google.Protobuf
|
||||
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages. TypeRegistry.Empty will be used if it is null.</param>
|
||||
/// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
|
||||
/// <param name="preserveProtoFieldNames"><c>true</c> to preserve proto field names; <c>false</c> to convert them to lowerCamelCase.</param>
|
||||
/// <param name="indentation">The indentation string to use for multi-line formatting. <c>null</c> to disable multi-line format.</param>
|
||||
private Settings(bool formatDefaultValues,
|
||||
TypeRegistry typeRegistry,
|
||||
bool formatEnumsAsIntegers,
|
||||
bool preserveProtoFieldNames,
|
||||
string indentation = null)
|
||||
bool preserveProtoFieldNames)
|
||||
{
|
||||
FormatDefaultValues = formatDefaultValues;
|
||||
TypeRegistry = typeRegistry ?? TypeRegistry.Empty;
|
||||
FormatEnumsAsIntegers = formatEnumsAsIntegers;
|
||||
PreserveProtoFieldNames = preserveProtoFieldNames;
|
||||
Indentation = indentation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Settings"/> object with the specified formatting of default values and the current settings.
|
||||
/// </summary>
|
||||
/// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
|
||||
public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, PreserveProtoFieldNames, Indentation);
|
||||
public Settings WithFormatDefaultValues(bool formatDefaultValues) => new Settings(formatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, PreserveProtoFieldNames);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Settings"/> object with the specified type registry and the current settings.
|
||||
/// </summary>
|
||||
/// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
|
||||
public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers, PreserveProtoFieldNames, Indentation);
|
||||
public Settings WithTypeRegistry(TypeRegistry typeRegistry) => new Settings(FormatDefaultValues, typeRegistry, FormatEnumsAsIntegers, PreserveProtoFieldNames);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Settings"/> object with the specified enums formatting option and the current settings.
|
||||
/// </summary>
|
||||
/// <param name="formatEnumsAsIntegers"><c>true</c> to format the enums as integers; <c>false</c> to format enums as enum names.</param>
|
||||
public Settings WithFormatEnumsAsIntegers(bool formatEnumsAsIntegers) => new Settings(FormatDefaultValues, TypeRegistry, formatEnumsAsIntegers, PreserveProtoFieldNames, Indentation);
|
||||
public Settings WithFormatEnumsAsIntegers(bool formatEnumsAsIntegers) => new Settings(FormatDefaultValues, TypeRegistry, formatEnumsAsIntegers, PreserveProtoFieldNames);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Settings"/> object with the specified field name formatting option and the current settings.
|
||||
/// </summary>
|
||||
/// <param name="preserveProtoFieldNames"><c>true</c> to preserve proto field names; <c>false</c> to convert them to lowerCamelCase.</param>
|
||||
public Settings WithPreserveProtoFieldNames(bool preserveProtoFieldNames) => new Settings(FormatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, preserveProtoFieldNames, Indentation);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Settings"/> object with the specified indentation and the current settings.
|
||||
/// </summary>
|
||||
/// <param name="indentation">The string to output for each level of indentation (nesting). The default is two spaces per level. Use null to disable indentation entirely.</param>
|
||||
/// <remarks>A non-null value for <see cref="Indentation"/> will insert additional line-breaks to the JSON output.
|
||||
/// Each line will contain either a single value, or braces. The default line-break is determined by <see cref="Environment.NewLine"/>,
|
||||
/// which is <c>"\n"</c> on Unix platforms, and <c>"\r\n"</c> on Windows. If <see cref="JsonFormatter"/> seems to produce empty lines,
|
||||
/// you need to pass a <see cref="TextWriter"/> that uses a <c>"\n"</c> newline. See <see cref="JsonFormatter.Format(Google.Protobuf.IMessage, TextWriter)"/>.
|
||||
/// </remarks>
|
||||
public Settings WithIndentation(string indentation = " ") => new Settings(FormatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, PreserveProtoFieldNames, indentation);
|
||||
public Settings WithPreserveProtoFieldNames(bool preserveProtoFieldNames) => new Settings(FormatDefaultValues, TypeRegistry, FormatEnumsAsIntegers, preserveProtoFieldNames);
|
||||
}
|
||||
|
||||
// Effectively a cache of mapping from enum values to the original name as specified in the proto file,
|
||||
@@ -1005,8 +916,9 @@ namespace Google.Protobuf
|
||||
}
|
||||
}
|
||||
|
||||
string originalName;
|
||||
// If this returns false, originalName will be null, which is what we want.
|
||||
nameMapping.TryGetValue(value, out string originalName);
|
||||
nameMapping.TryGetValue(value, out originalName);
|
||||
return originalName;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user