Example #1
0
        /// <summary>
        /// Writes the leaf value provided to the json writer.
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="value">The value.</param>
        /// <param name="convertUnsupportedToString">When <c>true</c> if the object type
        /// is not natively supported by this method its internally serialized to a string then written to the response.</param>
        private void WriteLeafValue(Utf8JsonWriter writer, object value, bool convertUnsupportedToString = false)
        {
            if (value == null)
            {
                writer.WriteNullValue();
                return;
            }

            if (value.GetType().IsEnum)
            {
                value = _nameFormatter.FormatEnumValueName(value.ToString());
            }

            switch (value)
            {
            case string str:
                this.WritePreEncodedStringValue(writer, str);
                break;

            case double dob:
                writer.WriteNumberValue(dob);
                break;

            case float f:
                writer.WriteNumberValue(f);
                break;

            case decimal d:
                writer.WriteNumberValue(d);
                break;

            case long l:
                writer.WriteNumberValue(l);
                break;

            case ulong ul:
                writer.WriteNumberValue(ul);
                break;

            case int i:
                writer.WriteNumberValue(i);
                break;

            case uint ui:
                writer.WriteNumberValue(ui);
                break;

            case byte b:
                writer.WriteNumberValue(b);
                break;

            case sbyte sb:
                writer.WriteNumberValue(sb);
                break;

            case bool boo:
                writer.WriteBooleanValue(boo);
                break;

            case DateTime dt:
                this.WritePreEncodedStringValue(writer, dt.ToRfc3339String());
                break;

            case DateTimeOffset dto:
                this.WritePreEncodedStringValue(writer, dto.ToRfc3339String());
                break;

            default:
                if (convertUnsupportedToString)
                {
                    var output = JsonSerializer.Serialize(value, _serializerSettings);
                    writer.WriteStringValue(output);
                }
                else
                {
                    throw new ArgumentOutOfRangeException(
                              $"Unknown type. Default writer is unable to write " +
                              $"type '{value.GetType().FriendlyName()}' to the output stream.");
                }

                break;
            }
        }
Example #2
0
 /// <summary>
 /// When overridden in a derived class, converts the specified name according to the policy.
 /// </summary>
 /// <param name="name">The name to convert.</param>
 /// <returns>The converted name.</returns>
 public override string ConvertName(string name)
 {
     return(_namingFormatter.FormatEnumValueName(name));
 }