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