0.07 adding in the Storable role
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Format / JSON.pm
CommitLineData
a23e18d7 1
4d1850a6 2package MooseX::Storage::Format::JSON;
a23e18d7 3use Moose::Role;
4
34dcaa5d 5no warnings 'once';
6
b5384d08 7use JSON::Any;
a23e18d7 8
69b45b7d 9our $VERSION = '0.02';
10our $AUTHORITY = 'cpan:STEVAN';
7b428d1f 11
4d1850a6 12requires 'pack';
13requires 'unpack';
a23e18d7 14
15sub thaw {
98ae09f0 16 my ( $class, $json, @args ) = @_;
34dcaa5d 17 local $JSON::UnMapping = 1;
98ae09f0 18 $class->unpack( JSON::Any->jsonToObj($json), @args );
a23e18d7 19}
20
21sub freeze {
98ae09f0 22 my ( $self, @args ) = @_;
34dcaa5d 23 local $JSON::UnMapping = 1;
98ae09f0 24 JSON::Any->objToJson( $self->pack(@args) );
a23e18d7 25}
26
271;
28
29__END__
30
31=pod
32
ec9c1923 33=head1 NAME
34
35MooseX::Storage::Format::JSON
36
37=head1 SYNOPSIS
38
1390c23d 39 package Point;
40 use Moose;
41 use MooseX::Storage;
42
43 with Storage('format' => 'JSON');
44
45 has 'x' => (is => 'rw', isa => 'Int');
46 has 'y' => (is => 'rw', isa => 'Int');
47
48 1;
49
50 my $p = Point->new(x => 10, y => 10);
51
52 ## methods to freeze/thaw into
53 ## a specified serialization format
54 ## (in this case JSON)
55
56 # pack the class into a JSON string
57 $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
58
59 # unpack the JSON string into a class
60 my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');
61
ec9c1923 62=head1 METHODS
63
64=over 4
65
66=item B<freeze>
67
68=item B<thaw ($json)>
69
70=back
71
72=head2 Introspection
73
74=over 4
75
76=item B<meta>
77
78=back
79
80=head1 BUGS
81
82All complex software has bugs lurking in it, and this module is no
83exception. If you find a bug please either email me, or add the bug
84to cpan-RT.
85
86=head1 AUTHOR
87
88Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
89
90Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
91
92=head1 COPYRIGHT AND LICENSE
93
94Copyright 2007 by Infinity Interactive, Inc.
95
96L<http://www.iinteractive.com>
97
98This library is free software; you can redistribute it and/or modify
99it under the same terms as Perl itself.
100
a23e18d7 101=cut
102
ec9c1923 103