json.pm 561 B

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