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