json.pm 625 B

123456789101112131415161718192021222324252627282930313233
  1. package Trog::Renderer::json;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures state};
  6. use JSON::MaybeXS;
  7. =head1 Trog::Renderer::json
  8. Render JSON. Rather than be templated, we just run the input thru the encoder.
  9. =cut
  10. sub render (%options) {
  11. my $code = delete $options{code} // 200;
  12. my $headers = delete $options{headers} // {};
  13. my %h = (
  14. 'Content-type' => "application/json",
  15. %$headers,
  16. );
  17. delete $options{contenttype};
  18. delete $options{template};
  19. my $body = encode_json( $options{data} );
  20. return [ $code, [%h], [$body] ];
  21. }
  22. 1;