Eric Wilhelm > JSON-ON-v0.0.1 > JSON::ON

Download:
JSON-ON-v0.0.1.tar.gz

Dependencies

Annotate this POD

Website

CPAN RT

Open  0
View/Report Bugs
Module Version: v0.0.1   Source   Latest Release: JSON-ON-v0.0.3

NAME ^

JSON::ON - javascript object notation object notator

SYNOPSIS ^

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;

Making Sausage

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 ...

Implementation

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.

Constructor ^

new

  my $j = JSON::ON->new(%args);
j

Optional. The JSON object.

module_handler

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 /);
      }
    }
  },

Methods ^

encoder

  $j->encoder->($data);

decoder

  $j->decoder->($data);

subs

Convenience method. Returns $j->decoder, $j->encoder subrefs.

  my ($dec, $enc) = $j->subs;

encode

  my $enc = $j->encode($stuff);

decode

  my $dec = $j->decode($enc);

AUTHOR ^

Eric Wilhelm @ <ewilhelm at cpan dot org>

http://scratchcomputing.com/

BUGS ^

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 ^

Copyright (C) 2010-2013 Eric L. Wilhelm, All Rights Reserved.

NO WARRANTY ^

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.

LICENSE ^

syntax highlighting: