| 1 | package Zuzu::Module::JSON; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.007002'; | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Zuzu::Error; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Zuzu::Value::BinaryString; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Zuzu::Util::NativeHelpers qw( | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | native_class | ||||||||||||||||||||||||||||||||||||||||||||||
| 13 | native_function | ||||||||||||||||||||||||||||||||||||||||||||||
| 14 | native_object | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | perl_to_zuzu | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | zuzu_bool | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | zuzu_to_perl | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub _assert_binary_string { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assert_binary_string: called CC=7 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my ( $value, $label ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | return $value | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::BinaryString")
builtin::blessed($value)
$value->isa("Zuzu::Value::BinaryString")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::BinaryString")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | if blessed($value) and $value->isa('Zuzu::Value::BinaryString'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my $type = blessed($value) && $value->can('type_name') | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/3
builtin::blessed($value) and $value->can("type_name")
MC/DC0%0/2
builtin::blessed($value) and $value->can("type_name")
builtin::blessed($value)
$value->can("type_name")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->can("type_name")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | ? $value->type_name | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | : !defined($value) ? 'Null' : ref($value) ? ref($value) : 'String'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | die Zuzu::Error->new_runtime( | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | message => "TypeException: $label expects BinaryString, got $type", | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | file => '<std/data/json>', | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | line => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | sub _escape_non_ascii_for_json_backend { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_non_ascii_for_json_backend: called CC=2 SCAR=12.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | my ( $text ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 4 | $text =~ s{([^\x00-\x7F])}{ | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my $cp = ord($1); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | if ( $cp <= 0xFFFF ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | sprintf '\\u%04X', $cp; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $cp -= 0x10000; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | sprintf '\\u%04X\\u%04X', | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0xD800 + ( $cp >> 10 ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0xDC00 + ( $cp & 0x3FF ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 50 | }gex; | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | return $text; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub _decode_json_to_zuzu { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_decode_json_to_zuzu: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 4 | my ( $self, $json ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 4 | my $perl = $self->slots->{_encoder}->decode($json); | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | return perl_to_zuzu( | ||||||||||||||||||||||||||||||||||||||||||||||
| 60 | $perl, | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 15 | ▶ | is_boolean => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 15 | ▶ | blessed( $_[0] ) and $_[0]->DOES('JSON::PP::Boolean'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | package Zuzu::Module::JSON::Codec; | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | require JSON::MultiValueOrdered; | |||||||||||||||||||||||||||||||||||||||||||||
| 70 | 3 | our @ISA = 'JSON::MultiValueOrdered'; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | sub _new_hash { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_new_hash: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 4 | ▶ | if ( $_[0]{pairlists} ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | tie my %h, 'Tie::Hash::MultiValueOrdered'; | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | return \%h; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 4 | return {}; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | sub _encode_object { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_encode_object: called CC=11 SCAR=34.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | my $object = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 4 | my $indent; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | if (exists $self->{_indent}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $indent = $self->{_indent}; | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | $self->{_indent} .= "\t"; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 4 | my @pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 4 | ▶ | my $space = defined $indent ? q( ) : q(); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | my $tied = tied(%$object); | |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | if ($tied and $tied->DOES('Tie::Hash::MultiValueOrdered')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
$tied and $tied->DOES("Tie::Hash::MultiValueOrdered")
MC/DC0%0/2
$tied and $tied->DOES("Tie::Hash::MultiValueOrdered")
$tied
$tied->DOES("Tie::Hash::MultiValueOrdered")
Truth table33%1/3
A and B
A= $tiedB= $tied->DOES("Tie::Hash::MultiValueOrdered")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | my @list = $tied->pairs; | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | ▶ | for ( my $i = 0; $i < @list; $i += 2 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | push @pairs, sprintf( | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | '%s:%s%s', | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | $self->_encode_string($list[$i]), | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | $space, | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | $self->_encode_values($list[$i + 1]), | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | elsif ( $self->{canonical} ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 4 | for my $k ( sort keys %$object ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 106 | push @pairs, sprintf( | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | '%s:%s%s', | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | $self->_encode_string($k), | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | $space, | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 12 | $self->_encode_values($object->{$k}), | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | while ( my ($k, $v) = each %$object ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | push @pairs, sprintf( | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | '%s:%s%s', | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | $self->_encode_string($k), | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | $space, | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | $self->_encode_values($v), | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | ▶ | if ( defined $indent ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $self->{_indent} =~ s/^.//; | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | return "{}" unless @pairs; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | return "\{\n$indent\t" . join(",\n$indent\t", @pairs) . "\n$indent\}"; | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 4 | return '{' . join(',', @pairs) . '}'; | |||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 3 | ▶ | sub _path_tiny_from_object { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_path_tiny_from_object: called CC=6 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | my ( $path_obj, $method_name ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object") and exists $path_obj->slots->{"_path_tiny"}
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object")
MC/DC0%0/3
builtin::blessed($path_obj) and $path_obj->isa("Zuzu::Value::Object") and exists $path_obj->slots->{"_path_tiny"}
builtin::blessed($path_obj)
$path_obj->isa("Zuzu::Value::Object")
exists $path_obj->slots->{"_path_tiny"}
Truth table25%1/4
A and B and C
A= builtin::blessed($path_obj)B= $path_obj->isa("Zuzu::Value::Object")C= exists $path_obj->slots->{"_path_tiny"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | blessed($path_obj) | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | and $path_obj->isa('Zuzu::Value::Object') | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | and exists $path_obj->slots->{_path_tiny} | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | return $path_obj->slots->{_path_tiny}; | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | ▶ | elsif ( ref($path_obj) eq 'HASH' and exists $path_obj->{_path_tiny} ) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $path_obj eq "HASH" and exists $path_obj->{"_path_tiny"}
MC/DC0%0/2
ref $path_obj eq "HASH" and exists $path_obj->{"_path_tiny"}
ref $path_obj eq "HASH"
exists $path_obj->{"_path_tiny"}
Truth table0%0/3
A and B
A= ref $path_obj eq "HASH"B= exists $path_obj->{"_path_tiny"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | return $path_obj->{_path_tiny}; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | die Zuzu::Error->new_runtime( | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | message => "TypeException: $method_name expects Path as first argument", | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | file => '<std/data/json>', | ||||||||||||||||||||||||||||||||||||||||||||||
| 153 | line => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 18 | ▶ | sub IMPORT { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
IMPORT: called CC=1 SCAR=0.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 18 | my ( $class, $runtime ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 18 | my $json_class = native_class( | |||||||||||||||||||||||||||||||||||||||||||||
| 161 | name => 'JSON', | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 9 | ▶ | $json_class->native_constructor( sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=32.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 9 | my ( $rt, $klass, $positional, $named ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 9 | ▶ | my %config = %{ $named // {} }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$named // {}
MC/DC0%0/1
$named // {}
$named
Truth table50%1/2
A
A= $named
| |||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 9 | ▶ | if ( @{ $positional // [] } ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$positional // []
MC/DC0%0/1
$positional // []
$positional
Truth table50%1/2
A
A= $positional
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | my $first = $positional->[0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ( ref($first) ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my $first_hash = zuzu_to_perl( $first ); | |||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ( ref($first_hash) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | for my $key ( CORE::keys %{ $first_hash } ) { | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | $config{$key} = $first_hash->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||
| 179 | my $encoder = Zuzu::Module::JSON::Codec->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | canonical => zuzu_bool( $config{canonical}, 0 ) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | pairlists => zuzu_bool( $config{pairlists}, 0 ) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | pretty => zuzu_bool( $config{pretty}, 0 ) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 9 | ▶ | utf8 => zuzu_bool( $config{utf8}, 0 ) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 184 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 9 | return native_object( | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | class => $klass, | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | slots => { _encoder => $encoder }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | const => { _encoder => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 18 | } ); | |||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | my $encode = native_function( | ||||||||||||||||||||||||||||||||||||||||||||||
| 194 | name => 'encode', | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 3 | ▶ | native => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 3 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 197 | 3 | ▶ | my $value = @args ? $args[0] : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | my $perl = zuzu_to_perl( | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | $value, | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | ▶ | boolean_mapper => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 3 | ▶ | return $_[0] ? JSON::Tiny::Subclassable->true : JSON::Tiny::Subclassable->false; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 204 | 3 | return $self->slots->{_encoder}->encode( $perl ); | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 18 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 18 | $json_class->methods->{encode} = $encode; | |||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||
| 209 | $json_class->methods->{encode_binarystring} = native_function( | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | name => 'encode_binarystring', | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | native => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | return Zuzu::Value::BinaryString->from_utf8_string( | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | $encode->{_native}->( $self, @args ) | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 18 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | my $decode = native_function( | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | name => 'decode', | ||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | native => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | ▶ | my $json = @args ? $args[0] : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | ▶ | $json = defined($json) ? "$json" : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # The JSON::Tiny backend mishandles non-ASCII characters in | ||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # character strings; escape them like decode_binarystring does. | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 3 | $json = _escape_non_ascii_for_json_backend( $json ); | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 3 | return _decode_json_to_zuzu( $self, $json ); | |||||||||||||||||||||||||||||||||||||||||||||
| 229 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 18 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | 18 | $json_class->methods->{decode} = $decode; | |||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | $json_class->methods->{decode_binarystring} = native_function( | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | name => 'decode_binarystring', | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | ▶ | native => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | my $raw = _assert_binary_string( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 238 | @args ? $args[0] : undef, | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 'JSON.decode_binarystring', | ||||||||||||||||||||||||||||||||||||||||||||||
| 240 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | my $json = _escape_non_ascii_for_json_backend( | |||||||||||||||||||||||||||||||||||||||||||||
| 242 | $raw->to_utf8_string | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | return _decode_json_to_zuzu( $self, $json ); | |||||||||||||||||||||||||||||||||||||||||||||
| 245 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 18 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||||||
| 248 | $json_class->methods->{load} = native_function( | ||||||||||||||||||||||||||||||||||||||||||||||
| 249 | name => 'load', | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 1 | ▶ | native => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 1 | my ( $self, $path_obj ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | $runtime->assert_capability( 'fs', "JSON.load is denied by runtime policy" ); | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | my $path_tiny = _path_tiny_from_object( $path_obj, 'JSON.load' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | my $raw = Zuzu::Value::BinaryString->new( | |||||||||||||||||||||||||||||||||||||||||||||
| 255 | bytes => $path_tiny->slurp_raw, | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 1 | return $json_class->methods->{decode_binarystring}->{_native}->( | |||||||||||||||||||||||||||||||||||||||||||||
| 258 | $self, | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | $raw, | ||||||||||||||||||||||||||||||||||||||||||||||
| 260 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 18 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | $json_class->methods->{dump} = native_function( | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | name => 'dump', | ||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 1 | ▶ | native => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 1 | my ( $self, $path_obj, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 268 | 1 | $runtime->assert_capability( 'fs', "JSON.dump is denied by runtime policy" ); | |||||||||||||||||||||||||||||||||||||||||||||
| 269 | 1 | my $path_tiny = _path_tiny_from_object( $path_obj, 'JSON.dump' ); | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | ▶ | my $value = @args ? $args[0] : undef; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | my $perl = zuzu_to_perl( | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | $value, | ||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | boolean_mapper => sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | ▶ | return $_[0] ? JSON::Tiny::Subclassable->true : JSON::Tiny::Subclassable->false; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | my $json = $self->slots->{_encoder}->encode( $perl ); | |||||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | $path_tiny->spew_raw( | |||||||||||||||||||||||||||||||||||||||||||||
| 279 | Zuzu::Value::BinaryString->from_utf8_string($json)->bytes | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 1 | return $path_obj; | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 18 | ); | |||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||
| 285 | return { | ||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 18 | JSON => $json_class, | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||
| 292 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||
| 294 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||
| 296 | Zuzu::Module::JSON - std/data/json bindings for ZuzuScript. | ||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||
| 298 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||
| 300 | Implements the C<std/data/json> module. The importer provides a single class | ||||||||||||||||||||||||||||||||||||||||||||||
| 301 | named C<JSON>. If C<JSON::XS> is installed, it will be used as the | ||||||||||||||||||||||||||||||||||||||||||||||
| 302 | underlying backend; otherwise C<JSON::PP> is used. | ||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||
| 304 | =head1 CLASSES | ||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||
| 306 | =head2 JSON | ||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||
| 308 | Constructor options: | ||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||
| 310 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||
| 312 | =item * C<utf8> (default true) | ||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||
| 314 | =item * C<pretty> (default false) | ||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||
| 316 | =item * C<canonical> (default false) | ||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||
| 318 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||
| 320 | Instance methods: | ||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||
| 322 | =over | ||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||
| 324 | =item * C<encode(value)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||
| 326 | Encodes a ZuzuScript value as JSON text. | ||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||
| 328 | =item * C<decode(json)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||
| 330 | Decodes JSON text and returns the corresponding ZuzuScript value. | ||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | =item * C<load(path)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | Reads JSON text from a C<std/io> C<Path> and decodes it. | ||||||||||||||||||||||||||||||||||||||||||||||
| 335 | Throws if given anything other than C<Path>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||
| 337 | =item * C<dump(path, value)> | ||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||
| 339 | Encodes C<value> and writes JSON text to a C<std/io> C<Path>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 340 | Throws if given anything other than C<Path>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||
| 342 | =back | ||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||
| 344 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||
| 346 | B<< Zuzu::Module::JSON >> is copyright Toby Inkster. | ||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||
| 348 | It is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 349 | the terms of either the Artistic License 1.0 or the GNU General Public | ||||||||||||||||||||||||||||||||||||||||||||||
| 350 | License version 2. | ||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||
| 352 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||