HttpHandler.json.cs 943 B

12345678910111213141516171819202122232425262728293031323334353637
  1. using BeetleX;
  2. using BeetleX.Light.Memory;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading.Tasks;
  9. namespace PlatformBenchmarks
  10. {
  11. public partial class HttpHandler
  12. {
  13. private static ReadOnlySpan<byte> _jsonPreamble =>
  14. "HTTP/1.1 200 OK\r\n"u8 +
  15. "Server: B\r\n"u8 +
  16. "Content-Type: application/json\r\n"u8 +
  17. "Content-Length: 27\r\n"u8;
  18. public void Json(IStreamWriter stream)
  19. {
  20. stream.Write(_jsonPreamble);
  21. GMTDate.Default.Write(stream);
  22. var jsonWriter = GetJsonWriter(stream);
  23. using (var unflush = stream.UnFlush())
  24. {
  25. jsonWriter.WriteStartObject();
  26. jsonWriter.WriteString("message", "Hello, World!");
  27. jsonWriter.WriteEndObject();
  28. jsonWriter.Flush();
  29. }
  30. }
  31. }
  32. }