private static void PrintFieldValue(FieldDescriptor field, object value, TextGenerator generator) { switch (field.FieldType) { case FieldType.Int32: case FieldType.Int64: case FieldType.SInt32: case FieldType.SInt64: case FieldType.SFixed32: case FieldType.SFixed64: case FieldType.Float: case FieldType.Double: case FieldType.UInt32: case FieldType.UInt64: case FieldType.Fixed32: case FieldType.Fixed64: // The simple Object.ToString converts using the current culture. // We want to always use the invariant culture so it's predictable. generator.Print(((IConvertible)value).ToString(CultureInfo.InvariantCulture)); break; case FieldType.Bool: // Explicitly use the Java true/false generator.Print((bool)value ? "true" : "false"); break; case FieldType.String: generator.Print("\""); generator.Print(EscapeText((string)value)); generator.Print("\""); break; case FieldType.Bytes: { generator.Print("\""); generator.Print(EscapeBytes((ByteString)value)); generator.Print("\""); break; } case FieldType.Enum: { if (value is IEnumLite && !(value is EnumValueDescriptor)) { throw new NotSupportedException("Lite enumerations are not supported."); } generator.Print(((EnumValueDescriptor)value).Name); break; } case FieldType.Message: case FieldType.Group: if (value is IMessageLite && !(value is IMessage)) { throw new NotSupportedException("Lite messages are not supported."); } Print((IMessage)value, generator); break; } }
private static void PrintSingleField(FieldDescriptor field, Object value, TextGenerator generator) { if (field.IsExtension) { generator.Print("["); // We special-case MessageSet elements for compatibility with proto1. if (field.ContainingType.Options.MessageSetWireFormat && field.FieldType == FieldType.Message && field.IsOptional // object equality (TODO(jonskeet): Work out what this comment means!) && field.ExtensionScope == field.MessageType) { generator.Print(field.MessageType.FullName); } else { generator.Print(field.FullName); } generator.Print("]"); } else { if (field.FieldType == FieldType.Group) { // Groups must be serialized with their original capitalization. generator.Print(field.MessageType.Name); } else { generator.Print(field.Name); } } if (field.MappedType == MappedType.Message) { generator.Print(" {\n"); generator.Indent(); } else { generator.Print(": "); } PrintFieldValue(field, value, generator); if (field.MappedType == MappedType.Message) { generator.Outdent(); generator.Print("}"); } generator.Print("\n"); }
private static void PrintUnknownFields(UnknownFieldSet unknownFields, TextGenerator generator) { foreach (KeyValuePair<int, UnknownField> entry in unknownFields.FieldDictionary) { String prefix = entry.Key.ToString() + ": "; UnknownField field = entry.Value; foreach (ulong value in field.VarintList) { generator.Print(prefix); generator.Print(value.ToString()); generator.Print("\n"); } foreach (uint value in field.Fixed32List) { generator.Print(prefix); generator.Print(string.Format("0x{0:x8}", value)); generator.Print("\n"); } foreach (ulong value in field.Fixed64List) { generator.Print(prefix); generator.Print(string.Format("0x{0:x16}", value)); generator.Print("\n"); } foreach (ByteString value in field.LengthDelimitedList) { generator.Print(entry.Key.ToString()); generator.Print(": \""); generator.Print(EscapeBytes(value)); generator.Print("\"\n"); } foreach (UnknownFieldSet value in field.GroupList) { generator.Print(entry.Key.ToString()); generator.Print(" {\n"); generator.Indent(); PrintUnknownFields(value, generator); generator.Outdent(); generator.Print("}\n"); } } }
private static void PrintFieldValue(FieldDescriptor field, object value, TextGenerator generator) { switch (field.FieldType) { // The Float and Double types must specify the "r" format to preserve their precision, otherwise, // the double to/from string will trim the precision to 6 places. As with other numeric formats // below, always use the invariant culture so it's predictable. case FieldType.Float: generator.Print(((float)value).ToString("r", FrameworkPortability.InvariantCulture)); break; case FieldType.Double: generator.Print(((double)value).ToString("r", FrameworkPortability.InvariantCulture)); break; case FieldType.Int32: case FieldType.Int64: case FieldType.SInt32: case FieldType.SInt64: case FieldType.SFixed32: case FieldType.SFixed64: case FieldType.UInt32: case FieldType.UInt64: case FieldType.Fixed32: case FieldType.Fixed64: // The simple Object.ToString converts using the current culture. // We want to always use the invariant culture so it's predictable. generator.Print(((IConvertible)value).ToString(FrameworkPortability.InvariantCulture)); break; case FieldType.Bool: // Explicitly use the Java true/false generator.Print((bool) value ? "true" : "false"); break; case FieldType.String: generator.Print("\""); generator.Print(EscapeText((string) value)); generator.Print("\""); break; case FieldType.Bytes: { generator.Print("\""); generator.Print(EscapeBytes((ByteString) value)); generator.Print("\""); break; } case FieldType.Enum: { if (value is IEnumLite && !(value is EnumValueDescriptor)) { throw new NotSupportedException("Lite enumerations are not supported."); } generator.Print(((EnumValueDescriptor) value).Name); break; } case FieldType.Message: case FieldType.Group: if (value is IMessageLite && !(value is IMessage)) { throw new NotSupportedException("Lite messages are not supported."); } Print((IMessage) value, generator); break; } }
private static void PrintUnknownFields(UnknownFieldSet unknownFields, TextGenerator generator) { foreach (KeyValuePair <int, UnknownField> entry in unknownFields.FieldDictionary) { String prefix = entry.Key.ToString() + ": "; UnknownField field = entry.Value; foreach (ulong value in field.VarintList) { generator.Print(prefix); generator.Print(value.ToString()); generator.Print("\n"); } foreach (uint value in field.Fixed32List) { generator.Print(prefix); generator.Print(string.Format("0x{0:x8}", value)); generator.Print("\n"); } foreach (ulong value in field.Fixed64List) { generator.Print(prefix); generator.Print(string.Format("0x{0:x16}", value)); generator.Print("\n"); } foreach (ByteString value in field.LengthDelimitedList) { generator.Print(entry.Key.ToString()); generator.Print(": \""); generator.Print(EscapeBytes(value)); generator.Print("\"\n"); } foreach (UnknownFieldSet value in field.GroupList) { generator.Print(entry.Key.ToString()); generator.Print(" {\n"); generator.Indent(); PrintUnknownFields(value, generator); generator.Outdent(); generator.Print("}\n"); } } }
private static void PrintFieldValue(FieldDescriptor field, object value, TextGenerator generator) { switch (field.FieldType) { // The Float and Double types must specify the "r" format to preserve their precision, otherwise, // the double to/from string will trim the precision to 6 places. As with other numeric formats // below, always use the invariant culture so it's predictable. case FieldType.Float: generator.Print(((float)value).ToString("r", FrameworkPortability.InvariantCulture)); break; case FieldType.Double: generator.Print(((double)value).ToString("r", FrameworkPortability.InvariantCulture)); break; case FieldType.Int32: case FieldType.Int64: case FieldType.SInt32: case FieldType.SInt64: case FieldType.SFixed32: case FieldType.SFixed64: case FieldType.UInt32: case FieldType.UInt64: case FieldType.Fixed32: case FieldType.Fixed64: // The simple Object.ToString converts using the current culture. // We want to always use the invariant culture so it's predictable. generator.Print(((IConvertible)value).ToString(FrameworkPortability.InvariantCulture)); break; case FieldType.Bool: // Explicitly use the Java true/false generator.Print((bool)value ? "true" : "false"); break; case FieldType.String: generator.Print("\""); generator.Print(EscapeText((string)value)); generator.Print("\""); break; case FieldType.Bytes: { generator.Print("\""); generator.Print(EscapeBytes((ByteString)value)); generator.Print("\""); break; } case FieldType.Enum: { if (value is IEnumLite && !(value is EnumValueDescriptor)) { throw new NotSupportedException("Lite enumerations are not supported."); } generator.Print(((EnumValueDescriptor)value).Name); break; } case FieldType.Message: case FieldType.Group: if (value is IMessageLite && !(value is IMessage)) { throw new NotSupportedException("Lite messages are not supported."); } Print((IMessage)value, generator); break; } }
private static void PrintFieldValue(FieldDescriptor field, object value, TextGenerator generator) { switch (field.FieldType) { case FieldType.Int32: case FieldType.Int64: case FieldType.SInt32: case FieldType.SInt64: case FieldType.SFixed32: case FieldType.SFixed64: case FieldType.Float: case FieldType.Double: case FieldType.UInt32: case FieldType.UInt64: case FieldType.Fixed32: case FieldType.Fixed64: // The simple Object.ToString converts using the current culture. // We want to always use the invariant culture so it's predictable. generator.Print(((IConvertible) value).ToString(CultureInfo.InvariantCulture)); break; case FieldType.Bool: // Explicitly use the Java true/false generator.Print((bool) value ? "true" : "false"); break; case FieldType.String: generator.Print("\""); generator.Print(EscapeText((string) value)); generator.Print("\""); break; case FieldType.Bytes: { generator.Print("\""); generator.Print(EscapeBytes((ByteString) value)); generator.Print("\""); break; } case FieldType.Enum: { generator.Print(((EnumValueDescriptor) value).Name); break; } case FieldType.Message: case FieldType.Group: Print((IMessage) value, generator); break; } }