JQ-Lite
view release on metacpan or search on metacpan
lib/JQ/Lite/Util/Parsing.pm view on Meta::CPAN
package JQ::Lite::Util;
use strict;
use warnings;
use JSON::PP ();
use Scalar::Util qw(looks_like_number);
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);
}
sub _decode_json {
my ($text) = @_;
if (defined $text && is_utf8($text, 1)) {
$text = encode('UTF-8', $text);
}
return $JSON_DECODER->decode($text);
}
sub _are_brackets_balanced {
my ($text) = @_;
return 1 unless defined $text && length $text;
my %pairs = (
'(' => ')',
'[' => ']',
'{' => '}',
);
my %closing = reverse %pairs;
my @stack;
my $string;
my $escape = 0;
for my $char (split //, $text) {
if (defined $string) {
if ($escape) {
$escape = 0;
next;
}
if ($char eq '\\') {
$escape = 1;
next;
}
if ($char eq $string) {
undef $string;
}
next;
}
if ($char eq "'" || $char eq '"') {
$string = $char;
next;
}
( run in 3.305 seconds using v1.01-cache-2.11-cpan-acf6aa7dc9e )