TextJsonSerializer.cs 853 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using System.IO;
  3. using System.Text.Json;
  4. using Microsoft.Azure.Cosmos;
  5. namespace CosmosDB3Test
  6. {
  7. public class TextJsonSerializer : CosmosSerializer
  8. {
  9. private JsonSerializerOptions options = new JsonSerializerOptions()
  10. {
  11. IgnoreNullValues = true,
  12. PropertyNameCaseInsensitive = true
  13. };
  14. public override T FromStream<T>(Stream stream)
  15. {
  16. using (stream)
  17. {
  18. return JsonSerializer.DeserializeAsync<T>(stream, this.options).GetAwaiter().GetResult();
  19. }
  20. }
  21. public override Stream ToStream<T>(T input)
  22. {
  23. MemoryStream stream = new MemoryStream();
  24. JsonSerializer.SerializeAsync(stream, input, this.options).GetAwaiter().GetResult();
  25. return stream;
  26. }
  27. }
  28. }