| 1 | |||
| 2 | package JSON::PP::Monkey; | ||
| 3 | $JSON::PP::Monkey::VERSION = '0.1.0'; | ||
| 4 | # ABSTRACT: JSON::PP with encoding fallbacks | ||
| 5 | use 5.10.1; | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use JSON::PP 2.94; | ||
| 10 | use parent qw(JSON::PP); | ||
| 11 | |||
| 12 | use Carp (); | ||
| 13 | use Scalar::Util qw(blessed refaddr reftype); | ||
| 14 | |||
| 15 | BEGIN { *P_ALLOW_UNKNOWN = *JSON::PP::P_ALLOW_UNKNOWN } | ||
| 16 | BEGIN { *_is_bignum = *JSON::PP::_is_bignum } | ||
| 17 | BEGIN { *_looks_like_number = *JSON::PP::_looks_like_number } | ||
| 18 | |||
| 19 | sub add_fallback { | ||
| 20 | my ($self, $case, $cb) = @_; | ||
| 21 | push @{$self->{fallbacks}{$case}}, $cb; | ||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub remove_fallback { | ||
| 26 | my ($self, $case, $cb) = @_; | ||
| 27 | |||
| 28 | if ($cb) { | ||
| 29 | $self->{fallbacks}{$case} = [grep { $cb ne $_ } @{$self->{fallbacks}{$case}}]; | ||
| 30 | delete $self->{fallbacks}{$case} unless @{$self->{fallbacks}{$case}}; | ||
| 31 | } | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _emit_fallback_to_json { | ||
| 37 | my ($self, $case, $value) = @_; | ||
| 38 | |||
| 39 | if (my $s = $self->{fallbacks}{$case}) { | ||
| 40 | for my $cb (@$s) { | ||
| 41 | if (my ($r) = $self->$cb($value, $case)) { | ||
| 42 | |||
| 43 | if ( defined $r and ref( $r ) ) { | ||
| 44 | if ( refaddr( $value ) eq refaddr( $r ) ) { | ||
| 45 | encode_error( sprintf( | ||
| 46 | "'%s' fallback (%s) returned same object as was passed instead of a new one", | ||
| 47 | $case, $cb | ||
| 48 | ) ); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | return $self->object_to_json($r); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | return 'null' if $self->_get_collapse_to_null($case); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | # $json->_collapse_to_null($case, $enable); | ||
| 61 | sub _collapse_to_null { $_[0]->{"collapse_$_[1]"} = @_ > 2 ? !!$_[2] : !!1; $_[0] } | ||
| 62 | sub _get_collapse_to_null { $_[0]->{"collapse_$_[1]"} // !!1 } | ||
| 63 | |||
| 64 | sub collapse_blessed { shift->_collapse_to_null( 'blessed', @_ ) } | ||
| 65 | sub get_collapse_blessed { $_[0]->_get_collapse_to_null('blessed') } | ||
| 66 | |||
| 67 | sub collapse_unknown { shift->_collapse_to_null( 'unknown', @_ ) } | ||
| 68 | sub get_collapse_unknown { $_[0]->_get_collapse_to_null('unknown') } | ||
| 69 | |||
| 70 | # Helpful fallbacks | ||
| 71 | |||
| 72 | sub _convert_bignum { | ||
| 73 | return unless _is_bignum($_[1]); | ||
| 74 | return "$_[1]"; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _convert_as_nonblessed { # Based on JSON::PP::blessed_to_json | ||
| 78 | my $obj = $_[1]; | ||
| 79 | my $reftype = reftype($obj) || ''; | ||
| 80 | if ($reftype eq 'HASH') { | ||
| 81 | return {%$obj}; | ||
| 82 | } | ||
| 83 | elsif ($reftype eq 'ARRAY') { | ||
| 84 | return [@$obj]; | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | return undef; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _convert_with_to_json { | ||
| 92 | return unless $_[1]->can('TO_JSON'); | ||
| 93 | return $_[1]->TO_JSON; | ||
| 94 | } | ||
| 95 | |||
| 96 | # Methods 'convert_bignum' and 'convert_as_nonblessed' | ||
| 97 | |||
| 98 | BEGIN { | ||
| 99 | my @hidden_properties = qw( allow_bignum as_nonblessed ); | ||
| 100 | for my $name (@hidden_properties) { | ||
| 101 | no strict 'refs'; | ||
| 102 | *{$name} = *{"get_$name"} = sub { ... }; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | sub convert_blessed { | ||
| 107 | my $self = shift->SUPER::convert_blessed(@_); | ||
| 108 | my $meth = $self->SUPER::get_convert_blessed ? 'add_fallback' : 'remove_fallback'; | ||
| 109 | $self->$meth('blessed', '_convert_with_to_json'); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub get_convert_bignum { shift->SUPER::get_allow_bignum } | ||
| 113 | |||
| 114 | sub convert_bignum { | ||
| 115 | my $self = shift->SUPER::allow_bignum(@_); | ||
| 116 | my $meth = $self->SUPER::get_allow_bignum ? 'add_fallback' : 'remove_fallback'; | ||
| 117 | $self->$meth('blessed', '_convert_bignum'); | ||
| 118 | } | ||
| 119 | |||
| 120 | sub get_convert_as_nonblessed { shift->SUPER::get_as_nonblessed } | ||
| 121 | |||
| 122 | sub convert_as_nonblessed { | ||
| 123 | my $self = shift->SUPER::as_nonblessed(@_); | ||
| 124 | my $meth = $self->SUPER::get_as_nonblessed ? 'add_fallback' : 'remove_fallback'; | ||
| 125 | $self->$meth('blessed', '_convert_as_nonblessed'); | ||
| 126 | } | ||
| 127 | |||
| 128 | ### | ||
| 129 | |||
| 130 | { | ||
| 131 | sub object_to_json { | ||
| 132 | my ($self, $obj) = @_; | ||
| 133 | my $type = ref($obj); | ||
| 134 | |||
| 135 | if($type eq 'HASH'){ | ||
| 136 | return $self->hash_to_json($obj); | ||
| 137 | } | ||
| 138 | elsif($type eq 'ARRAY'){ | ||
| 139 | return $self->array_to_json($obj); | ||
| 140 | } | ||
| 141 | elsif ($type) { # blessed object? | ||
| 142 | if (blessed($obj)) { | ||
| 143 | |||
| 144 | return $self->value_to_json($obj) if ( $obj->isa('JSON::PP::Boolean') ); | ||
| 145 | |||
| 146 | my $allow_blessed = $self->get_allow_blessed; | ||
| 147 | if ($allow_blessed) { | ||
| 148 | if (my ($r) = $self->_emit_fallback_to_json('blessed', $obj)) { | ||
| 149 | return $r; | ||
| 150 | } | ||
| 151 | else { | ||
| 152 | encode_error( sprintf("encountered object '%s', but no fallback " | ||
| 153 | . "for 'blessed' was matched", $obj ) | ||
| 154 | ); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | encode_error( sprintf("encountered object '%s', but allow_blessed " | ||
| 158 | . "setting is not enabled", $obj) | ||
| 159 | ); | ||
| 160 | } | ||
| 161 | else { | ||
| 162 | return $self->value_to_json($obj); | ||
| 163 | } | ||
| 164 | } | ||
| 165 | else{ | ||
| 166 | return $self->value_to_json($obj); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | sub value_to_json { | ||
| 171 | my ($self, $value) = @_; | ||
| 172 | |||
| 173 | return 'null' if(!defined $value); | ||
| 174 | |||
| 175 | my $type = ref($value); | ||
| 176 | |||
| 177 | if (!$type) { | ||
| 178 | if (_looks_like_number($value)) { | ||
| 179 | return $value; | ||
| 180 | } | ||
| 181 | return $self->string_to_json($value); | ||
| 182 | } | ||
| 183 | elsif( blessed($value) and $value->isa('JSON::PP::Boolean') ){ | ||
| 184 | return $$value == 1 ? 'true' : 'false'; | ||
| 185 | } | ||
| 186 | else { | ||
| 187 | if ((overload::StrVal($value) =~ /=(\w+)/)[0]) { | ||
| 188 | return $self->value_to_json("$value"); | ||
| 189 | } | ||
| 190 | |||
| 191 | if ($type eq 'SCALAR' and defined $$value) { | ||
| 192 | return 'true' if $$value eq '1'; | ||
| 193 | return 'false' if $$value eq '0'; | ||
| 194 | } | ||
| 195 | |||
| 196 | if ( $self->{PROPS}->[ P_ALLOW_UNKNOWN ] ) { | ||
| 197 | if (my ($r) = $self->_emit_fallback_to_json('unknown', $value)) { | ||
| 198 | return $r; | ||
| 199 | } | ||
| 200 | else { | ||
| 201 | encode_error( sprintf("encountered reference '%s', but no fallback " | ||
| 202 | . "for 'unknown' was matched", $value ) | ||
| 203 | ); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | { | ||
| 207 | if ( $type eq 'SCALAR' or $type eq 'REF' ) { | ||
| 208 | encode_error("cannot encode reference to scalar"); | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | encode_error("encountered $value, but JSON can only represent references to arrays or hashes"); | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | BEGIN { *encode_error = *JSON::PP::encode_error } | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; | ||
| 222 | |||
| 223 | #pod =encoding utf8 | ||
| 224 | #pod | ||
| 225 | #pod =head1 SYNOPSIS | ||
| 226 | #pod | ||
| 227 | #pod use JSON::PP::Monkey; | ||
| 228 | #pod | ||
| 229 | #pod my $json = JSON::PP::Monkey->new->utf8->pretty | ||
| 230 | #pod ->allow_blessed->add_fallback('blessed', sub { +{ __BLESSED_ => "$_[1]" } }) | ||
| 231 | #pod ->allow_unknown->add_fallback('unknown', sub { +{ __UNKNOWN_ => "$_[1]" } }) | ||
| 232 | #pod | ||
| 233 | #pod $json->encode({ active => \1, io => \*STDOUT, foo => bless({}, 'foo')}); | ||
| 234 | #pod # { | ||
| 235 | #pod # "foo" : { | ||
| 236 | #pod # "__BLESSED_" : "foo=HASH(0x7fda11bc0fc8)" | ||
| 237 | #pod # }, | ||
| 238 | #pod # "active" : true, | ||
| 239 | #pod # "io" : { | ||
| 240 | #pod # "__UNKNOWN_" : "GLOB(0x7fda11029518)" | ||
| 241 | #pod # } | ||
| 242 | #pod # } | ||
| 243 | #pod | ||
| 244 | #pod =head1 DESCRIPTION | ||
| 245 | #pod | ||
| 246 | #pod This is an experiment with a JSON encoder that can | ||
| 247 | #pod apply fallback conversions to blessed objects and unknowns. | ||
| 248 | #pod | ||
| 249 | #pod The primary reason it has been created was to allow | ||
| 250 | #pod dumping arbitrary Perl data into JSON. | ||
| 251 | #pod | ||
| 252 | #pod =head1 CAVEATS | ||
| 253 | #pod | ||
| 254 | #pod =head2 REVISED API | ||
| 255 | #pod | ||
| 256 | #pod Unlike C<JSON::PP>, C<JSON::XS>, C<Cpanel::JSON::XS>, L</"allow_blessed"> | ||
| 257 | #pod must be enabled before blessed objects can be converted to JSON | ||
| 258 | #pod by invoking C<TO_JSON> or stringifying bignums. | ||
| 259 | #pod | ||
| 260 | #pod # { "x": <JSON encoding of $foo->TO_JSON> } | ||
| 261 | #pod JSON::PP::Monkey->new->allow_blessed->convert_blessed->encode({x => $foo}); | ||
| 262 | #pod | ||
| 263 | #pod # dies - allow_blessed is not enabled | ||
| 264 | #pod JSON::PP::Monkey->new->convert_blessed->encode({x => $foo}); | ||
| 265 | #pod | ||
| 266 | #pod # { "x": "999" } | ||
| 267 | #pod JSON::PP::Monkey->new->allow_blessed->convert_bignum->encode({x => Math::BigInt->new('999')}); | ||
| 268 | #pod | ||
| 269 | #pod # dies - allow_blessed is not enabled | ||
| 270 | #pod JSON::PP::Monkey->new->convert_bignum->encode({x => $foo}); | ||
| 271 | #pod | ||
| 272 | #pod Another difference is that the fallback conversion of objects into C<'null'> | ||
| 273 | #pod must be disabled explicitly (with L</"collapse_blessed">) if it is unwanted. So | ||
| 274 | #pod | ||
| 275 | #pod JSON::PP::Monkey->new->allow_blessed->collapse_blessed(0)->convert_blessed; | ||
| 276 | #pod | ||
| 277 | #pod is the equivalent of | ||
| 278 | #pod | ||
| 279 | #pod JSON::PP->new->convert_blessed; | ||
| 280 | #pod | ||
| 281 | #pod in the sense they will convert objects with C<TO_JSON> methods into JSON | ||
| 282 | #pod and bail on everything else. | ||
| 283 | #pod | ||
| 284 | #pod The behavior of L</"allow_unknown"> and L</"collapse_unknown"> is analogous. | ||
| 285 | #pod | ||
| 286 | #pod These API changes have been made to provide a more consistent behavior. | ||
| 287 | #pod | ||
| 288 | #pod =over 4 | ||
| 289 | #pod | ||
| 290 | #pod =item * | ||
| 291 | #pod | ||
| 292 | #pod if objects will be encoded, C<allow_blessed> must be enabled before any | ||
| 293 | #pod fallback (installed with L</"add_fallback">, L</"convert_blessed">, or | ||
| 294 | #pod L</"convert_bignum">) can be applied. | ||
| 295 | #pod | ||
| 296 | #pod =item * | ||
| 297 | #pod | ||
| 298 | #pod if objects will be encoded but the fallback conversion into C<"null"> | ||
| 299 | #pod is unwanted, it should be disabled with L</"collapse_blessed">. | ||
| 300 | #pod | ||
| 301 | #pod =item * | ||
| 302 | #pod | ||
| 303 | #pod if unknowns will be encoded, C<allow_unknown> must be enabled before any | ||
| 304 | #pod fallback can be applied | ||
| 305 | #pod | ||
| 306 | #pod =item * | ||
| 307 | #pod | ||
| 308 | #pod if unknowns will be encoded but the fallback conversion into C<"null"> | ||
| 309 | #pod is unwanted, it should be disabled with L</"collapse_unknown">. | ||
| 310 | #pod | ||
| 311 | #pod =back | ||
| 312 | #pod | ||
| 313 | #pod =head2 FALLBACK ORDER | ||
| 314 | #pod | ||
| 315 | #pod Notice that the order of fallbacks is important: | ||
| 316 | #pod | ||
| 317 | #pod $json = JSON::PP->new->utf8->allow_blessed->convert_bignum->convert_blessed; | ||
| 318 | #pod | ||
| 319 | #pod will apply stringification to bignums before trying to check for C<TO_JSON> | ||
| 320 | #pod methods. While | ||
| 321 | #pod | ||
| 322 | #pod $json = JSON::PP->new->utf8->allow_blessed->convert_blessed->convert_bignum; | ||
| 323 | #pod | ||
| 324 | #pod will check for C<TO_JSON> methods (and apply them) before considering the | ||
| 325 | #pod stringification of bignums. | ||
| 326 | #pod | ||
| 327 | #pod =head1 METHODS | ||
| 328 | #pod | ||
| 329 | #pod =head2 allow_blessed | ||
| 330 | #pod | ||
| 331 | #pod $json = $json->allow_blessed; | ||
| 332 | #pod $json = $json->allow_blessed($enable); | ||
| 333 | #pod | ||
| 334 | #pod If enabled, allows to encode blessed references (or objects) | ||
| 335 | #pod via the current C<'blessed'> fallbacks or into C<'null'> | ||
| 336 | #pod (if L</"collapse_blessed"> is enabled). | ||
| 337 | #pod | ||
| 338 | #pod Defaults to disabled. | ||
| 339 | #pod | ||
| 340 | #pod =head2 allow_unknown | ||
| 341 | #pod | ||
| 342 | #pod $json = $json->allow_unknown; | ||
| 343 | #pod $json = $json->allow_unknown($enable); | ||
| 344 | #pod | ||
| 345 | #pod If enabled, allows to encode unknown references | ||
| 346 | #pod via the current C<'unknown'> fallbacks or into C<'null'> | ||
| 347 | #pod (if L</"collapse_unknown"> is enabled). | ||
| 348 | #pod | ||
| 349 | #pod Defaults to disabled. | ||
| 350 | #pod | ||
| 351 | #pod =head2 collapse_blessed | ||
| 352 | #pod | ||
| 353 | #pod $json = $json->collapse_blessed; | ||
| 354 | #pod $json = $json->collapse_blessed($enable); | ||
| 355 | #pod | ||
| 356 | #pod If L</"allow_blessed"> is enabled, an object is encoded into | ||
| 357 | #pod C<'null'> if no C<'blessed'> fallback applied. | ||
| 358 | #pod | ||
| 359 | #pod Defaults to enabled. Only has effect if C<"allow_blessed"> is enabled. | ||
| 360 | #pod | ||
| 361 | #pod =head2 collapse_unknown | ||
| 362 | #pod | ||
| 363 | #pod $json = $json->collapse_unknown; | ||
| 364 | #pod $json = $json->collapse_unknown($enable); | ||
| 365 | #pod | ||
| 366 | #pod If L</"allow_unknown"> is enabled, an unknown is encoded into | ||
| 367 | #pod C<'null'> if no C<'unknown'> fallback applied. | ||
| 368 | #pod | ||
| 369 | #pod Defaults to enabled. Only has effect if C<"allow_unknown"> is enabled. | ||
| 370 | #pod | ||
| 371 | #pod =head2 convert_blessed | ||
| 372 | #pod | ||
| 373 | #pod $json = $json->convert_blessed; | ||
| 374 | #pod | ||
| 375 | #pod Add a C<"blessed"> fallback which applies to objects which | ||
| 376 | #pod have a C<"TO_JSON"> method. Equivalent to | ||
| 377 | #pod | ||
| 378 | #pod $json = $json->add_fallback('blessed', sub { | ||
| 379 | #pod return unless $_[1]->can('TO_JSON'); | ||
| 380 | #pod return $_[1]->TO_JSON; | ||
| 381 | #pod }); | ||
| 382 | #pod | ||
| 383 | #pod Only has effect if C<"allow_blessed"> is enabled. | ||
| 384 | #pod | ||
| 385 | #pod =head2 convert_bignum | ||
| 386 | #pod | ||
| 387 | #pod $json = $json->convert_bignum; | ||
| 388 | #pod | ||
| 389 | #pod Add a C<"blessed"> fallback which applies to objects which | ||
| 390 | #pod are L<Math::BigInt> or L<Math::BigFloat>. Equivalent to | ||
| 391 | #pod | ||
| 392 | #pod $json = $json->add_fallback('blessed', sub { | ||
| 393 | #pod return unless $_[1]->isa('Math::BigInt') || $_[1]->isa('Math::BigFloat'); | ||
| 394 | #pod return "$_[1]" | ||
| 395 | #pod }); | ||
| 396 | #pod | ||
| 397 | #pod Only has effect if C<"allow_blessed"> is enabled. | ||
| 398 | #pod | ||
| 399 | #pod =head2 add_fallback | ||
| 400 | #pod | ||
| 401 | #pod $json = $json->add_fallback('blessed', $cb); | ||
| 402 | #pod $json = $json->add_fallback('unknown', $cb); | ||
| 403 | #pod | ||
| 404 | #pod Add fallback conversions to be applied if: | ||
| 405 | #pod | ||
| 406 | #pod a blessed ref is found and "allow_blessed" is enabled | ||
| 407 | #pod an unknown is found and "allow_unknown" is enabled | ||
| 408 | #pod | ||
| 409 | #pod C<$case> should be one of C<'blessed'> or C<'unknown'>. | ||
| 410 | #pod | ||
| 411 | #pod C<$cb> is a subroutine which expects two arguments | ||
| 412 | #pod | ||
| 413 | #pod sub { | ||
| 414 | #pod my ($json, $item, $case) = (shift, shift); | ||
| 415 | #pod ... | ||
| 416 | #pod } | ||
| 417 | #pod | ||
| 418 | #pod Fallback subroutines are evaluated in list context. | ||
| 419 | #pod Their return is interpreted as below. | ||
| 420 | #pod | ||
| 421 | #pod =over 4 | ||
| 422 | #pod | ||
| 423 | #pod =item * | ||
| 424 | #pod | ||
| 425 | #pod a non-empty list means the first element converted to JSON | ||
| 426 | #pod will be the encoding result | ||
| 427 | #pod | ||
| 428 | #pod =item * | ||
| 429 | #pod | ||
| 430 | #pod an empty list means the fallback did not match, | ||
| 431 | #pod and the next one should be tried | ||
| 432 | #pod | ||
| 433 | #pod =back | ||
| 434 | #pod | ||
| 435 | #pod =head1 remove_fallback | ||
| 436 | #pod | ||
| 437 | #pod $json = $json->remove_fallback($case, $cb); | ||
| 438 | #pod | ||
| 439 | #pod =cut | ||
| 440 | |||
| 441 | __END__ |