private static async Task <BinaryData> FromSerializableAsync <T>(
     T data,
     ObjectSerializer serializer,
     bool async)
 {
     Argument.AssertNotNull(serializer, nameof(serializer));
     using var memoryStream = new MemoryStream();
     if (async)
     {
         await serializer.SerializeAsync(memoryStream, data, typeof(T)).ConfigureAwait(false);
     }
     else
     {
         serializer.Serialize(memoryStream, data, typeof(T));
     }
     return(new BinaryData(memoryStream.ToArray()));
 }
 private static async Task <BinaryData> SerializeInternalAsync <T>(
     T data,
     ObjectSerializer serializer,
     bool async,
     CancellationToken cancellationToken)
 {
     Argument.AssertNotNull(serializer, nameof(serializer));
     using var memoryStream = new MemoryStream();
     if (async)
     {
         await serializer.SerializeAsync(memoryStream, data, typeof(T), cancellationToken).ConfigureAwait(false);
     }
     else
     {
         serializer.Serialize(memoryStream, data, typeof(T), cancellationToken);
     }
     return(new BinaryData((ReadOnlyMemory <byte>)memoryStream.ToArray()));
 }
 private string Serialize <T>(T value)
 {
     using MemoryStream memoryStream = new MemoryStream();
     _serializer.Serialize(memoryStream, value, typeof(T), default);
     return(Encoding.UTF8.GetString(memoryStream.ToArray()));
 }
 private string Serialize <T>(T value, CancellationToken cancellationToken)
 {
     using MemoryStream memoryStream = new MemoryStream();
     _serializer.Serialize(memoryStream, value, typeof(T), cancellationToken);
     return(Encoding.UTF8.GetString(memoryStream.ToArray()));
 }