public static void WriteDynamicValue(
     this Utf8JsonWriter writer,
     object?value)
 {
     if (value is null)
     {
         writer.WriteNullValue();
     }
     else if (value is IJsonSerializable serializable)
     {
         writer.WriteSerializableValue(serializable);
     }
     else if (value is IEnumerable <KeyValuePair <string, string?> > sdic)
     {
         writer.WriteDictionaryValue(sdic);
     }
     else if (value is IEnumerable <KeyValuePair <string, object?> > dic)
     {
         writer.WriteDictionaryValue(dic);
     }
     else if (value is string str)
     {
         writer.WriteStringValue(str);
     }
     else if (value is bool b)
     {
         writer.WriteBooleanValue(b);
     }
     else if (value is int i)
     {
         writer.WriteNumberValue(i);
     }
     else if (value is long l)
     {
         writer.WriteNumberValue(l);
     }
     else if (value is double d)
     {
         writer.WriteNumberValue(d);
     }
     else if (value is DateTime dt)
     {
         writer.WriteStringValue(dt);
     }
     else if (value is DateTimeOffset dto)
     {
         writer.WriteStringValue(dto);
     }
     else if (value is IFormattable formattable)
     {
         writer.WriteStringValue(formattable.ToString(null, CultureInfo.InvariantCulture));
     }
     else
     {
         JsonSerializer.Serialize(writer, value);
     }
 }
Esempio n. 2
0
 public static void WriteDictionary(
     this Utf8JsonWriter writer,
     string propertyName,
     IEnumerable <KeyValuePair <string, string?> >?dic)
 {
     writer.WritePropertyName(propertyName);
     writer.WriteDictionaryValue(dic);
 }
Esempio n. 3
0
 private async Task SerializeHeaderAsync(
     Stream stream,
     IReadOnlyDictionary <string, object?> header,
     CancellationToken cancellationToken = default)
 {
     await using var writer = new Utf8JsonWriter(stream);
     writer.WriteDictionaryValue(header);
     await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
 }
Esempio n. 4
0
        private async Task SerializeHeaderAsync(Stream stream, IDiagnosticLogger?logger, CancellationToken cancellationToken = default)
        {
            var writer = new Utf8JsonWriter(stream);

#if NET461 || NETSTANDARD2_0
            using (writer)
#else
            await using (writer.ConfigureAwait(false))
#endif
            {
                writer.WriteDictionaryValue(Header, logger);
                await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
            }
        }
Esempio n. 5
0
        private static async Task SerializeHeaderAsync(
            Stream stream,
            IReadOnlyDictionary <string, object?> header,
            CancellationToken cancellationToken = default)
        {
            var writer = new Utf8JsonWriter(stream);

#if NET461 || NETSTANDARD2_0
            using (writer)
#else
            await using (writer.ConfigureAwait(false))
#endif
            {
                writer.WriteDictionaryValue(header);
                await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
            }
        }