JsonModule.cs 704 B

123456789101112131415161718192021222324252627282930
  1. namespace Benchmarks
  2. {
  3. using Carter;
  4. using System.Threading.Tasks;
  5. using Utf8Json;
  6. public class JsonModule : CarterModule
  7. {
  8. private const int _bufferSize = 27;
  9. public JsonModule() : base("json")
  10. {
  11. Get("/", (req, res) =>
  12. {
  13. res.StatusCode = 200;
  14. res.ContentType = "application/json";
  15. res.ContentLength = _bufferSize;
  16. var msg = new JsonMessage { message = "Hello, World!" };
  17. return JsonSerializer.SerializeAsync(res.Body, msg);
  18. });
  19. }
  20. public struct JsonMessage
  21. {
  22. public string message;
  23. }
  24. }
  25. }