Amon2-Plugin-Web-CpanelJSON

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

{
   "abstract" : "Cpanel::JSON::XS plugin",
   "author" : [
      "kfly8 <kfly@cpan.org>"
   ],
   "dynamic_config" : 0,
   "generated_by" : "Minilla/v3.1.18",
   "license" : [
      "perl_5"
   ],
   "meta-spec" : {
      "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",

META.json  view on Meta::CPAN

            "Test::CPAN::Meta" : "0",
            "Test::MinimumVersion::Fast" : "0.04",
            "Test::PAUSE::Permissions" : "0.07",
            "Test::Pod" : "1.41",
            "Test::Spellunker" : "v0.2.7"
         }
      },
      "runtime" : {
         "requires" : {
            "Amon2" : "0",
            "Cpanel::JSON::XS" : "0",
            "HTTP::SecureHeaders" : "0",
            "perl" : "5.010000"
         }
      },
      "test" : {
         "requires" : {
            "Module::Build::Tiny" : "0.035",
            "Test::More" : "0.98",
            "Test::Requires" : "0.06"
         }

META.yml  view on Meta::CPAN

---
abstract: 'Cpanel::JSON::XS plugin'
author:
  - 'kfly8 <kfly@cpan.org>'
build_requires:
  Module::Build::Tiny: '0.035'
  Test::More: '0.98'
  Test::Requires: '0.06'
configure_requires:
  Module::Build::Tiny: '0.035'
dynamic_config: 0
generated_by: 'Minilla/v3.1.18, CPAN::Meta::Converter version 2.150010'

META.yml  view on Meta::CPAN

    - eg
    - examples
    - author
    - builder
provides:
  Amon2::Plugin::Web::CpanelJSON:
    file: lib/Amon2/Plugin/Web/CpanelJSON.pm
    version: '0.01'
requires:
  Amon2: '0'
  Cpanel::JSON::XS: '0'
  HTTP::SecureHeaders: '0'
  perl: '5.010000'
resources:
  bugtracker: https://github.com/kfly8/p5-Amon2-Plugin-Web-CpanelJSON/issues
  homepage: https://github.com/kfly8/p5-Amon2-Plugin-Web-CpanelJSON
  repository: https://github.com/kfly8/p5-Amon2-Plugin-Web-CpanelJSON.git
version: '0.01'
x_authority: cpan:KFLY
x_serialization_backend: 'CPAN::Meta::YAML version 0.018'
x_static_install: 1

README.md  view on Meta::CPAN

[![Actions Status](https://github.com/kfly8/p5-Amon2-Plugin-Web-CpanelJSON/actions/workflows/test.yml/badge.svg)](https://github.com/kfly8/p5-Amon2-Plugin-Web-CpanelJSON/actions) [![Coverage Status](http://codecov.io/github/kfly8/p5-Amon2-Plugin-Web-...
# NAME

Amon2::Plugin::Web::CpanelJSON - Cpanel::JSON::XS plugin

# SYNOPSIS

```perl
use Amon2::Lite;
use Cpanel::JSON::XS::Type;
use HTTP::Status qw(:constants);

__PACKAGE__->load_plugins(qw/Web::CpanelJSON/);

use constant HelloWorld => {
    message => JSON_TYPE_STRING,
};

get '/' => sub {
    my $c = shift;

README.md  view on Meta::CPAN

};

__PACKAGE__->to_app();
```

# DESCRIPTION

This is a JSON plugin for Amon2.
The differences from Amon2::Plugin::Web::JSON are as follows.

\* Cpanel::JSON::XS::Type is available

\* HTTP status code can be specified

\* Flexible Configurations

# METHODS

- `$c->render_json($data, $json_spec, $status=200);`

    Generate JSON `$data` and `$json_spec` and returns instance of [Plack::Response](https://metacpan.org/pod/Plack%3A%3AResponse).
    `$json_spec` is a structure for JSON encoding defined in [Cpanel::JSON::XS::Type](https://metacpan.org/pod/Cpanel%3A%3AJSON%3A%3AXS%3A%3AType).

# CONFIGURATION

- json

    Parameters of [Cpanel::JSON::XS](https://metacpan.org/pod/Cpanel%3A%3AJSON%3A%3AXS). Default is as follows:

    ```perl
    ascii => !!1,
    ```

    Any parameters can be set:

    ```perl
     __PACKAGE__->load_plugins(
        'Web::CpanelJSON' => {

cpanfile  view on Meta::CPAN

requires 'perl', '5.010000';
requires 'Cpanel::JSON::XS';
requires 'Amon2';
requires 'HTTP::SecureHeaders';

on test => sub {
    requires 'Test::More', '0.98';
    requires 'Test::Requires', '0.06';
    requires 'Module::Build::Tiny', '0.035';
};

lib/Amon2/Plugin/Web/CpanelJSON.pm  view on Meta::CPAN

package Amon2::Plugin::Web::CpanelJSON;
use strict;
use warnings;

use Amon2::Util ();
use Cpanel::JSON::XS ();
use Scalar::Util qw(blessed);
use HTTP::SecureHeaders;

our $VERSION = "0.01";

my %DEFAULT_CONFIG = (
    name => 'render_json',

    # for security
    # refs https://cheatsheetseries.owasp.org/cheatsheets/REST_Security_Cheat_Sheet.html#security-headers

lib/Amon2/Plugin/Web/CpanelJSON.pm  view on Meta::CPAN

            $res
        };

        return $res;
    }
}

sub _generate_json_encoder {
    my $conf = shift;

    my $json = Cpanel::JSON::XS->new;
    if (my $json_args = $conf->{json}) {
        for my $key (keys %{$json_args}) {
            $json->$key($json_args->{$key})
        }
    }

    my $escape_filter = $conf->{json_escape_filter} || {};
    my $escape_target = '';
    for my $key (keys %{$escape_filter}) {
        if ($escape_filter->{$key}) {

lib/Amon2/Plugin/Web/CpanelJSON.pm  view on Meta::CPAN

    return $res;
}

1;
__END__

=encoding utf-8

=head1 NAME

Amon2::Plugin::Web::CpanelJSON - Cpanel::JSON::XS plugin

=head1 SYNOPSIS

    use Amon2::Lite;
    use Cpanel::JSON::XS::Type;
    use HTTP::Status qw(:constants);

    __PACKAGE__->load_plugins(qw/Web::CpanelJSON/);

    use constant HelloWorld => {
        message => JSON_TYPE_STRING,
    };

    get '/' => sub {
        my $c = shift;
        return $c->render_json(+{ message => 'HELLO!' }, HelloWorld, HTTP_OK);
    };

    __PACKAGE__->to_app();

=head1 DESCRIPTION

This is a JSON plugin for Amon2.
The differences from Amon2::Plugin::Web::JSON are as follows.

* Cpanel::JSON::XS::Type is available

* HTTP status code can be specified

* Flexible Configurations

=head1 METHODS

=over 4

=item C<< $c->render_json($data, $json_spec, $status=200); >>

Generate JSON C<< $data >> and C<< $json_spec >> and returns instance of L<Plack::Response>.
C<< $json_spec >> is a structure for JSON encoding defined in L<Cpanel::JSON::XS::Type>.

=back

=head1 CONFIGURATION

=over 4

=item json

Parameters of L<Cpanel::JSON::XS>. Default is as follows:

    ascii => !!1,

Any parameters can be set:

     __PACKAGE__->load_plugins(
        'Web::CpanelJSON' => {
            json => {
                ascii     => 0,
                utf8      => 1,

t/01_render_json.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Cpanel::JSON::XS::Type;

{
    package MyApp::Web;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins('Web::CpanelJSON');
    sub encoding { 'utf-8' }
}

my $app = MyApp::Web->to_app;

t/10-config/json.t  view on Meta::CPAN

use strict;
use warnings;
use utf8;
use Test::More;

use Cpanel::JSON::XS qw(decode_json);
use Cpanel::JSON::XS::Type;
use Encode qw(encode_utf8 decode_utf8);

{
    package MyApp::Web::Default;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins(
        'Web::CpanelJSON',
    );
    sub encoding { 'utf-8' }
}

t/10-config/json.t  view on Meta::CPAN

    };
};

subtest 'none' => sub {
    my $src = { message => 'あ'};

    subtest 'ascii is off' => sub {
        my $res = $c_none->render_json($src);
        is $res->content, '{"message":"あ"}';
        is utf8::is_utf8($res->content), !!1;
        my $json = Cpanel::JSON::XS->new->utf8(0);
        is_deeply $json->decode($res->content), $src;
    };
};

done_testing;

t/10-config/json_escape_filter.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Cpanel::JSON::XS qw(decode_json);

{
    package MyApp::Web::Off;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins('Web::CpanelJSON', { json_escape_filter => undef });
    sub encoding { 'utf-8' }
}

{
    package MyApp::Web::PartialOff;

t/10-config/name.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Plack::Test;
use HTTP::Request::Common;
use Cpanel::JSON::XS::Type;

{
    package MyApp::Web;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins('Web::CpanelJSON', { name => 'my_json' });
    sub encoding { 'utf-8' }
}

{
    package MyApp::Web::ConflictName;

t/10-config/status_code_field.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Cpanel::JSON::XS qw(decode_json);
use Cpanel::JSON::XS::Type;

{
    package MyApp::Web::Default;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins(
        'Web::CpanelJSON',
    );
    sub encoding { 'utf-8' }
}

t/10-config/unbless_object.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Cpanel::JSON::XS qw(decode_json);
use Cpanel::JSON::XS::Type;

{
    package MyApp::Web::Default;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins(
        'Web::CpanelJSON',
    );
    sub encoding { 'utf-8' }
}

t/30_integration/JSON-UnblessObject/unbless_object.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Test::Requires 'JSON::UnblessObject';

use Cpanel::JSON::XS qw(decode_json);
use Cpanel::JSON::XS::Type;

use JSON::UnblessObject;

{
    package MyApp::Web;
    use parent qw(Amon2 Amon2::Web);
    __PACKAGE__->load_plugins(
        'Web::CpanelJSON', {
            json => {
                canonical => 1,

t/99_synopsis.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;
use Test::Requires qw(
    Amon2::Lite
);

{
    package MyApp;
    use Amon2::Lite;
    use Cpanel::JSON::XS::Type;
    use HTTP::Status qw(:constants);

    __PACKAGE__->load_plugins(qw/Web::CpanelJSON/);

    use constant HelloWorld => {
        message => JSON_TYPE_STRING,
    };

    get '/' => sub {
        my $c = shift;



( run in 0.434 second using v1.01-cache-2.11-cpan-4d50c553e7e )