JSON::ON - javascript object notation object notator
This module serializes and deserializes blessed references with JSON.
use JSON::ON; my $stuff = {whatever => What::Ever->new}; my $j = JSON::ON->new; my $enc = $j->encode($stuff); # elsewhere... my $j = JSON::ON->new; my $dec = $j->decode($enc); $dec->{whatever}->amethod;
The encode() method installs a local UNIVERSAL::TO_JSON() which simply un-blesses HASH, ARRAY, and SCALAR references. Similarly, the decoding has a hook which simply blesses them back into their class. This leaves edge cases for inside-out objects and code references, but ...
A special token is embedded in the JSON whenever an object appears (it is ugly, but highly unlikely to appear in a regular JSON document.)
This is intended more as an opaque transport mechanism than as human-readable JSON.
my $j = JSON::ON->new(%args);
Optional. The JSON object.
Optional. A callback for handling modules found in decode(). The default is to warn if a module has no VERSION method (which is a good indicator that it is not loaded.) Setting this to undef() will ignore all modules.
module_handler => sub { foreach my $module (@_) { unless($module->can("VERSION")) { (my $pm = $module) =~ s{::}{/}g; eval {require "$pm.pm"}; die $@ if($@ and $@ !~ m/^Can't locate $pm in \@INC /); } } },
$j->encoder->($data);
$j->decoder->($data);
Convenience method. Returns $j->decoder, $j->encoder subrefs.
my ($dec, $enc) = $j->subs;
my $enc = $j->encode($stuff);
my $dec = $j->decode($enc);
Eric Wilhelm @ <ewilhelm at cpan dot org>
http://scratchcomputing.com/
If you found this module on CPAN, please report any bugs or feature requests through the web interface at http://rt.cpan.org. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
If you pulled this development version from my /svn/, please contact me directly.
Copyright (C) 2010-2013 Eric L. Wilhelm, All Rights Reserved.
Absolutely, positively NO WARRANTY, neither express or implied, is offered with this software. You use this software at your own risk. In case of loss, no person or entity owes you anything whatsoever. You have been warned.
To install JSON::ON, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::ON
CPAN shell
perl -MCPAN -e shell install JSON::ON
For more information on module installation, please visit the detailed CPAN module installation guide.