NAME

Kelp::Module::JSON - Simple JSON module for a Kelp application

SYNOPSIS

package MyApp;
use Kelp::Base 'Kelp';

sub some_route {
    my $self = shift;

    # manually render a json configured to UTF-8
    $self->res->set_charset('UTF-8');
    $self->res->render_binary(
        $self->json->encode({ yes => 1 })
    );
}

DESCRIPTION

Standard JSON encoder/decoder. Chooses the best backend through JSON::MaybeXS.

REGISTERED METHODS

This module registers only one method into the application: json. It also registers itself for later use by "get_encoder" in Kelp under the name json.

The module will try to use backends in this order: Cpanel::JSON::XS, JSON::XS, JSON::PP.

CAVEATS

You should probably not use utf8, and just encode the value into a proper charset by hand. You may not always want to have encoded strings anyway, for example some interfaces may encode the values themselves.

Kelp will use an internal copy of JSON encoder / decoder with all the same options but without utf8, reserved for internal use. Modifying json options at runtime will not cause the request / response encoding to change.