private static void PrintFieldValue(object value, TextGenerator generator) { if (value == null) { } else if (value is bool) { generator.Print((bool)value ? "true" : "false"); } else if (value is ByteString) { generator.Print("\""); generator.Print(EscapeBytes((ByteString)value)); generator.Print("\""); } else if (value is string) { generator.Print("\""); generator.Print(EscapeText((string)value)); generator.Print("\""); } else if (value is Enum) { var name = OriginalEnumValueHelper.GetOriginalName(value); generator.Print(name); } else if (value is float || value is double) { string text = ((IFormattable)value).ToString("r", CultureInfo.InvariantCulture); if (text == "NaN" || text == "Infinity" || text == "-Infinity") { generator.Print("\""); generator.Print(text); generator.Print("\""); } else { generator.Print(text); } } else if (value is Single) { string text = ((IConvertible)value).ToString(CultureInfo.InvariantCulture); generator.Print(text); } else if (value is IFormattable) { generator.Print(((IFormattable)value).ToString("d", CultureInfo.InvariantCulture)); } else if (value is IMessage) { Print((IMessage)value, generator); } else { throw new ArgumentException("Unable to format value of type " + value.GetType()); } }
/// <summary> /// Writes a single value to the given writer as text. 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) { WriteNull(writer); } else if (value is bool) { writer.Write((bool)value ? "true" : "false"); } else if (value is ByteString) { // Nothing in Base64 needs escaping writer.Write('"'); WriteByteString(writer, (ByteString)value); writer.Write('"'); } else if (value is string) { WriteString(writer, (string)value); } else if (value is IDictionary) { WriteDictionary(writer, (IDictionary)value); } else if (value is IList) { WriteList(writer, (IList)value); } else if (value is int || value is uint) { IFormattable formattable = (IFormattable)value; writer.Write(formattable.ToString("d", CultureInfo.InvariantCulture)); } else if (value is long || value is ulong) { writer.Write('"'); IFormattable formattable = (IFormattable)value; writer.Write(formattable.ToString("d", CultureInfo.InvariantCulture)); writer.Write('"'); } else if (value is System.Enum) { if (settings.FormatEnumsAsIntegers) { WriteValue(writer, (int)value); } else { string name = OriginalEnumValueHelper.GetOriginalName(value); if (name != null) { WriteString(writer, name); } else { WriteValue(writer, (int)value); } } } else if (value is float || value is double) { string text = ((IFormattable)value).ToString("r", CultureInfo.InvariantCulture); if (text == "NaN" || text == "Infinity" || text == "-Infinity") { writer.Write('"'); writer.Write(text); writer.Write('"'); } else { writer.Write(text); } } else if (value is IMessage) { Format((IMessage)value, writer); } else { throw new ArgumentException("Unable to format value of type " + value.GetType()); } }