| 1 | package Venus::Json; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.018; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | use overload ( | ||||||||||||||||||||||||||||
| 9 | 2 | '""' => 'explain', | |||||||||||||||||||||||||||
| 10 | '~~' => 'explain', | ||||||||||||||||||||||||||||
| 11 | fallback => 1, | ||||||||||||||||||||||||||||
| 12 | 2 | ▶ | ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Venus::Class 'attr', 'base', 'with'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | base 'Venus::Kind::Utility'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | with 'Venus::Role::Valuable'; | |||||||||||||||||||||||||||
| 19 | 2 | with 'Venus::Role::Buildable'; | |||||||||||||||||||||||||||
| 20 | 2 | with 'Venus::Role::Accessible'; | |||||||||||||||||||||||||||
| 21 | 2 | with 'Venus::Role::Explainable'; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # ATTRIBUTES | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 2 | attr 'decoder'; | |||||||||||||||||||||||||||
| 26 | 2 | attr 'encoder'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # BUILDERS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub build_arg { | ||||||||||||||||||||||||||||
| 31 | 4 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
build_arg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | return { | ||||||||||||||||||||||||||||
| 34 | 4 | value => $data | |||||||||||||||||||||||||||
| 35 | }; | ||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | sub build_args { | ||||||||||||||||||||||||||||
| 39 | 8 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
build_args: called CC=3 SCAR=13.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 8 | ▶ | if (keys %$data == 1 && exists $data->{value}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
keys %$data == 1 and exists $data->{"value"}
MC/DC0%0/2
keys %$data == 1 and exists $data->{"value"}
keys %$data == 1
exists $data->{"value"}
Truth table33%1/3
A and B
A= keys %$data == 1B= exists $data->{"value"}
| |||||||||||||||||||||||||||||
| 42 | 8 | return $data; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | return { | ||||||||||||||||||||||||||||
| 45 | 0 | value => $data | |||||||||||||||||||||||||||
| 46 | }; | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub build_nil { | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
build_nil: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | return { | ||||||||||||||||||||||||||||
| 53 | 0 | value => $data | |||||||||||||||||||||||||||
| 54 | }; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub build_self { | ||||||||||||||||||||||||||||
| 58 | 8 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
build_self: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 8 | return $self->config; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # METHODS | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub config { | ||||||||||||||||||||||||||||
| 66 | 8 | ▶ | my ($self, $package) = @_; | ||||||||||||||||||||||||||
|
Subroutine
config: called CC=7 SCAR=25.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 8 | ▶ | $package ||= $self->package | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$package ||= $self->package
MC/DC0%0/2
$package ||= $self->package
$package
$self->package
Truth table33%1/3
A ||= B
A= $packageB= $self->package
| |||||||||||||||||||||||||||||
| 69 | or $self->error({throw => 'error_on_config'}); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 8 | $package = $package->new | |||||||||||||||||||||||||||
| 72 | ->canonical | ||||||||||||||||||||||||||||
| 73 | ->allow_nonref | ||||||||||||||||||||||||||||
| 74 | ->allow_unknown | ||||||||||||||||||||||||||||
| 75 | ->allow_blessed | ||||||||||||||||||||||||||||
| 76 | ->convert_blessed | ||||||||||||||||||||||||||||
| 77 | ->pretty; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 8 | ▶ | if ($package->can('escape_slash')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 8 | $package->escape_slash; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Cpanel::JSON::XS | ||||||||||||||||||||||||||||
| 84 | 8 | ▶ | if ($package->isa('Cpanel::JSON::XS')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | $self->decoder(sub { | ||||||||||||||||||||||||||||
| 86 | 1 | ▶ | my ($text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 1 | $package->decode($text); | |||||||||||||||||||||||||||
| 88 | 8 | }); | |||||||||||||||||||||||||||
| 89 | $self->encoder(sub { | ||||||||||||||||||||||||||||
| 90 | 8 | ▶ | my ($data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 8 | $package->encode($data); | |||||||||||||||||||||||||||
| 92 | 8 | }); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # JSON::XS | ||||||||||||||||||||||||||||
| 96 | 8 | ▶ | if ($package->isa('JSON::XS')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | $self->decoder(sub { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my ($text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 99 | 0 | $package->decode($text); | |||||||||||||||||||||||||||
| 100 | 0 | }); | |||||||||||||||||||||||||||
| 101 | $self->encoder(sub { | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | my ($data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | 0 | $package->encode($data); | |||||||||||||||||||||||||||
| 104 | 0 | }); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # JSON::PP | ||||||||||||||||||||||||||||
| 108 | 8 | ▶ | if ($package->isa('JSON::PP')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | $self->decoder(sub { | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | my ($text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | 0 | $package->decode($text); | |||||||||||||||||||||||||||
| 112 | 0 | }); | |||||||||||||||||||||||||||
| 113 | $self->encoder(sub { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my ($data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 115 | 0 | $package->encode($data); | |||||||||||||||||||||||||||
| 116 | 0 | }); | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 8 | return $self; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub decode { | ||||||||||||||||||||||||||||
| 123 | 1 | ▶ | my ($self, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | # double-traversing the data structure due to lack of serialization hooks | ||||||||||||||||||||||||||||
| 126 | 1 | return $self->set(FROM_BOOL($self->decoder->($data))); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub encode { | ||||||||||||||||||||||||||||
| 130 | 8 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # double-traversing the data structure due to lack of serialization hooks | ||||||||||||||||||||||||||||
| 133 | 8 | return $self->encoder->(TO_BOOL($self->get)); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub explain { | ||||||||||||||||||||||||||||
| 137 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
explain: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | return $self->encode; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | sub package { | ||||||||||||||||||||||||||||
| 143 | 13 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
package: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 13 | state $engine; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 13 | ▶ | return $engine if defined $engine; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 2 | my %packages = ( | |||||||||||||||||||||||||||
| 150 | 'JSON::XS' => '3.0', | ||||||||||||||||||||||||||||
| 151 | 'JSON::PP' => '2.27105', | ||||||||||||||||||||||||||||
| 152 | 'Cpanel::JSON::XS' => '4.09', | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | 2 | for my $package ( | |||||||||||||||||||||||||||
| 155 | grep defined, | ||||||||||||||||||||||||||||
| 156 | $ENV{VENUS_JSON_PACKAGE}, | ||||||||||||||||||||||||||||
| 157 | qw(Cpanel::JSON::XS JSON::XS JSON::PP) | ||||||||||||||||||||||||||||
| 158 | ) | ||||||||||||||||||||||||||||
| 159 | { | ||||||||||||||||||||||||||||
| 160 | 2 | my $criteria = "require $package; $package->VERSION($packages{$package})"; | |||||||||||||||||||||||||||
| 161 | 2 | ▶ | if (do {local $@; eval "$criteria"; $@}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | next; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 2 | $engine = $package; | |||||||||||||||||||||||||||
| 166 | 2 | last; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 2 | return $engine; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub FROM_BOOL { | ||||||||||||||||||||||||||||
| 174 | 5 | ▶ | my ($value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
FROM_BOOL: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 5 | require Venus::Boolean; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 5 | ▶ | if (ref($value) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1 | for my $key (keys %$value) { | |||||||||||||||||||||||||||
| 180 | 2 | $value->{$key} = FROM_BOOL($value->{$key}); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | 1 | return $value; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 4 | ▶ | if (ref($value) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 1 | for my $key (keys @$value) { | |||||||||||||||||||||||||||
| 187 | 2 | $value->[$key] = FROM_BOOL($value->[$key]); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 1 | return $value; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 3 | return Venus::Boolean::TO_BOOL(Venus::Boolean::FROM_BOOL($value)); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | sub TO_BOOL { | ||||||||||||||||||||||||||||
| 196 | 30 | ▶ | my ($value) = @_; | ||||||||||||||||||||||||||
|
Subroutine
TO_BOOL: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 30 | require Venus::Boolean; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 30 | ▶ | if (ref($value) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 4 | $value = { | |||||||||||||||||||||||||||
| 202 | %$value | ||||||||||||||||||||||||||||
| 203 | }; | ||||||||||||||||||||||||||||
| 204 | 4 | for my $key (keys %$value) { | |||||||||||||||||||||||||||
| 205 | 12 | $value->{$key} = TO_BOOL($value->{$key}); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | 4 | return $value; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 26 | ▶ | if (ref($value) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 6 | $value = [ | |||||||||||||||||||||||||||
| 212 | @$value | ||||||||||||||||||||||||||||
| 213 | ]; | ||||||||||||||||||||||||||||
| 214 | 6 | for my $key (keys @$value) { | |||||||||||||||||||||||||||
| 215 | 10 | $value->[$key] = TO_BOOL($value->[$key]); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 6 | return $value; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 20 | return Venus::Boolean::TO_BOOL_JPO($value); | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | # ERRORS | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | sub error_on_config { | ||||||||||||||||||||||||||||
| 226 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
error_on_config: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | return { | ||||||||||||||||||||||||||||
| 229 | 1 | name => 'on.config', | |||||||||||||||||||||||||||
| 230 | message => 'No suitable JSON package', | ||||||||||||||||||||||||||||
| 231 | raise => true, | ||||||||||||||||||||||||||||
| 232 | }; | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 2 | 1; | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | =head1 NAME | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | Venus::Json - Json Class | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | =cut | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head1 ABSTRACT | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Json Class for Perl 5 | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | package main; | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | use Venus::Json; | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | my $json = Venus::Json->new( | ||||||||||||||||||||||||||||
| 258 | value => { name => ['Ready', 'Robot'], version => 0.12, stable => !!1, } | ||||||||||||||||||||||||||||
| 259 | ); | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | # $json->encode; | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | This package provides methods for reading and writing L<JSON|https://json.org> | ||||||||||||||||||||||||||||
| 268 | data. B<Note:> This package requires that a suitable JSON library is installed, | ||||||||||||||||||||||||||||
| 269 | currently either C<JSON::XS> C<3.0+>, C<JSON::PP> C<2.27105+>, or | ||||||||||||||||||||||||||||
| 270 | C<Cpanel::JSON::XS> C<4.09+>. You can use the C<VENUS_JSON_PACKAGE> environment | ||||||||||||||||||||||||||||
| 271 | variable to include or prioritize your preferred JSON library. | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | This package has the following attributes: | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =head2 decoder | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | decoder(CodeRef) | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | This attribute is read-write, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =cut | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =head2 encoder | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | encoder(CodeRef) | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | This attribute is read-write, accepts C<(CodeRef)> values, and is optional. | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =cut | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | =head1 INHERITS | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | This package inherits behaviors from: | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | L<Venus::Kind::Utility> | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =cut | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | =head1 INTEGRATES | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | This package integrates behaviors from: | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | L<Venus::Role::Accessible> | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | L<Venus::Role::Buildable> | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | L<Venus::Role::Explainable> | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | L<Venus::Role::Valuable> | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =cut | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head1 METHODS | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | This package provides the following methods: | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =cut | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | =head2 decode | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | decode(string $json) (any) | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | The decode method decodes the JSON string, sets the object value, and returns | ||||||||||||||||||||||||||||
| 330 | the decoded value. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | =over 4 | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =item decode example 1 | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | # given: synopsis; | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | my $decode = $json->decode('{"codename":["Ready","Robot"],"stable":true}'); | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | # { codename => ["Ready", "Robot"], stable => 1 } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =back | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =cut | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head2 encode | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | encode() (string) | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | The encode method encodes the objects value as a JSON string and returns the | ||||||||||||||||||||||||||||
| 353 | encoded string. | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | I<Since C<0.01>> | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =over 4 | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =item encode example 1 | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | # given: synopsis; | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | my $encode = $json->encode; | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | # '{ "name": ["Ready", "Robot"], "stable": true, "version": 0.12 }' | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | =back | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =head1 ERRORS | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | This package may raise the following errors: | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | =cut | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =over 4 | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =item error: C<error_on_config> | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | This package may raise an error_on_config exception. | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | B<example 1> | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | # given: synopsis; | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | my $input = { | ||||||||||||||||||||||||||||
| 388 | throw => 'error_on_config', | ||||||||||||||||||||||||||||
| 389 | }; | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | my $error = $json->catch('error', $input); | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | # my $name = $error->name; | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # "on_config" | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # my $message = $error->message; | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | # "No suitable JSON package" | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =back | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Awncorp, C<awncorp@cpan.org> | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =cut | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>. | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | This program is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 414 | the terms of the Apache license version 2.0. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =cut | ||||||||||||||||||||||||||||