Exemple #1
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>
 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('"');
         writer.Write(((ByteString)value).ToBase64());
         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());
     }
 }
 /// <summary>
 /// Parses the given string value as the wire representation of a
 /// <typeparamref name="TEnum"/> value, throwing an exception if the value cannot be parsed.
 /// </summary>
 /// <typeparam name="TEnum">The enum type to convert <paramref name="value"/> to.</typeparam>
 /// <param name="value">The text value to parse.</param>
 /// <returns>The parsed value.</returns>
 /// <exception cref="ArgumentException"><paramref name="value"/> is not a valid wire value for
 /// <typeparamref name="TEnum"/>.</exception>
 public static TEnum Parse <TEnum>(string value) where TEnum : struct, Enum =>
 OriginalEnumValueHelper <TEnum> .Parse(value);
 /// <summary>
 /// Converts the given enum value into the wire representation as a string.
 /// </summary>
 /// <typeparam name="TEnum">The enum type to convert to a string.</typeparam>
 /// <param name="value">The value to convert.</param>
 /// <returns>The string representation of the enum value.</returns>
 /// <exception cref="ArgumentException">The enum value is not defined, so it has no known
 /// wire representation.</exception>
 public static string Format <TEnum>(TEnum value) where TEnum : struct, Enum =>
 OriginalEnumValueHelper <TEnum> .Format(value);
 /// <summary>
 /// Attempts to parse the given string value as the wire representation of
 /// a <typeparamref name="TEnum"/> value.
 /// </summary>
 /// <typeparam name="TEnum">The enum type to convert <paramref name="value"/> to.</typeparam>
 /// <param name="value">The text value to parse.</param>
 /// <param name="result">The output parameter to store the result in. This will be the
 /// parsed value of <typeparamref name="TEnum"/> if the return value is true, or the
 /// default value otherwise.</param>
 /// <returns>true if the value was successfully parsed; false otherwise</returns>
 public static bool TryParse <TEnum>(string value, out TEnum result) where TEnum : struct, Enum =>
 OriginalEnumValueHelper <TEnum> .TryParse(value, out result);