JsonHandler.cs 960 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System.Text.Json;
  2. using GenHTTP.Api.Content;
  3. using GenHTTP.Api.Protocol;
  4. using GenHTTP.Modules.Conversion.Serializers.Json;
  5. namespace Benchmarks.Tests;
  6. public sealed class JsonResult
  7. {
  8. public string Message { get; set; }
  9. }
  10. public sealed class JsonHandler : IHandler
  11. {
  12. private static readonly FlexibleContentType _ContentType = new(ContentType.ApplicationJson, "utf-8");
  13. private static readonly JsonSerializerOptions _Options = new();
  14. public ValueTask PrepareAsync() => new();
  15. public ValueTask<IResponse> HandleAsync(IRequest request)
  16. {
  17. var result = new JsonResult()
  18. {
  19. Message = "Hello, World!"
  20. };
  21. var response = request.Respond()
  22. .Content(new JsonContent(result, _Options))
  23. .Type(_ContentType)
  24. .Build();
  25. return new(response);
  26. }
  27. }