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