package MooseX::Storage::Format::JSON;
use Moose::Role;
no warnings 'once';
use JSON::Any;
use utf8 ();
our $VERSION = '0.32';
our $AUTHORITY = 'cpan:STEVAN';
requires 'pack';
requires 'unpack'
::encode($json) if utf8::is_utf8($json);
$class->unpack( JSON::Any->new->jsonToObj($json), @args );
}
sub freeze {
my ( $self, @args ) = @_;
my $json = JSON::Any->new->objToJson( $self->p
json) if !utf8::is_utf8($json) and utf8::valid($json); # if it's valid utf8 mark it as such
return $json;
}
no Moose::Role;
1;
__END__
=pod
=head1 NAME
MooseX::Storage::Format::JSON - A JSON
ad1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => '
ase JSON)
# pack the class into a JSON string
$p->freeze({ format => 'JSON' }); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
# pack the class into a JSON string using parameterized JSONpm r
> [ JSONpm => { json_opts => { pretty => 1 } } ] });
# unpack the JSON string into a class
my $p2 = Point->thaw(
'{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }',
{ format => 'JSON' }
ally use them (see the
SYNOPSIS for more info)
=head1 SUPPORTED FORMATS
=over 4
=item I<JSON>
=item I<JSONpm>
=item I<YAML>
=item I<Storable>
=back
=head1 SUPPORTED I/O
=over 4
=item I<File>
e_json {
my ($class, $json) = @_;
eval { require JSON::Any; JSON::Any->import };
confess "Could not load JSON module because : $@" if $@;
utf8::encode($json) if utf8::is_utf8($json);
my $data = eval { JSON::Any->jsonToObj($json) };
if ($@) {
confess "There was an error when attempting to peek at JSON: $@";
}
return $data;
}
sub _inflate_yaml {
my ($
unfreeze/unpack it.
The C<$data> can be either a perl HASH ref or some kind of serialized
data (JSON, YAML, etc.).
The C<%options> are as follows:
=over 4
=item I<format>
If this is left blank,
perly numify the numbers
# before and after them being futzed with, because some of
# the JSON engines are stupid/annoying/frustrating
'Int' => { expand => sub { $_[0] + 0 }, collapse
head1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'AtomicFile');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa
methods to load/store a class
## on the file system
$p->store('my_point.json');
my $p2 = Point->load('my_point.json');
=head1 METHODS
=over 4
=item B<load ($filename)>
=item B<store ($file
ad1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => '
YAML string
$p->freeze();
# ----
# __CLASS__: "Point"
# x: 10
# y: 10
# unpack the JSON string into a class
my $p2 = Point->thaw(<<YAML);
----
__CLASS__: "Point"
x: 10
y: 10
head1 SYNOPSIS
package Point;
use Moose;
use MooseX::Storage;
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'In
methods to load/store a class
## on the file system
$p->store('my_point.json');
my $p2 = Point->load('my_point.json');
=head1 METHODS
=over 4
=item B<load ($filename)>
=item B<store ($file
Point;
use Moose;
use MooseX::Storage;
our $VERSION = '0.01';
with Storage('format' => 'JSON', 'io' => 'File');
has 'x' => (is => 'rw', isa => 'Int');
has 'y' => (is => 'rw', isa => 'In
zation format
## (in this case JSON)
# pack the class into a JSON string
$p->freeze(); # { "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }
# unpack the JSON string into a class
my $p2 = P
methods to load/store a class
## on the file system
$p->store('my_point.json');
my $p2 = Point->load('my_point.json');
=head1 DESCRIPTION
MooseX::Storage is a serialization framework for Moo