JQ-Lite

 view release on metacpan or  search on metacpan

lib/JQ/Lite/Util/Parsing.pm  view on Meta::CPAN

use Encode qw(encode is_utf8);
use JQ::Lite::Expression ();

our $JSON_DECODER     = _build_json_decoder();
our $FROMJSON_DECODER = _build_json_decoder();
our $TOJSON_ENCODER   = JSON::PP->new->utf8->allow_nonref;

sub _build_json_decoder {
    my $decoder = JSON::PP->new->utf8->allow_nonref;

    if ($decoder->can('boolean_values')) {
        $decoder->boolean_values(JSON::PP::false, JSON::PP::true);
    }

    return $decoder;
}

sub _encode_json {
    my ($value) = @_;
    return $TOJSON_ENCODER->encode($value);
}

t/test_function.t  view on Meta::CPAN

    'test() maps over arrays and returns JSON booleans'
);

my @mixed_values = $jq->run_query($json, '.mixed | test("\\\\d")');
is_deeply(
    $mixed_values[0],
    [JSON::PP::true, JSON::PP::false, JSON::PP::true, JSON::PP::false],
    'test() coerces scalars and treats non-strings as false'
);

my @boolean_values = $jq->run_query($json, '.bools | test("^true$")');
is_deeply(
    $boolean_values[0],
    [JSON::PP::true, JSON::PP::false],
    'test() coerces booleans to true/false strings'
);

my @nested_values = $jq->run_query($json, '.nested | test("a")');
is_deeply(
    $nested_values[0],
    [[JSON::PP::false], [JSON::PP::true]],
    'test() maps recursively over nested arrays'
);



( run in 2.843 seconds using v1.01-cache-2.11-cpan-98e64b0badf )