more tweaks for 0.01
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Format / JSON.pm
1
2 package MooseX::Storage::Format::JSON;
3 use Moose::Role;
4
5 use JSON::Any;
6
7 our $VERSION = '0.01';
8
9 requires 'pack';
10 requires 'unpack';
11
12 sub thaw {
13     my ( $class, $json ) = @_;
14     $class->unpack( JSON::Any->jsonToObj($json) );
15 }
16
17 sub freeze {
18     my $self = shift;
19     JSON::Any->objToJson( $self->pack() );
20 }
21
22 1;
23
24 __END__
25
26 =pod
27
28 =head1 NAME
29
30 MooseX::Storage::Format::JSON
31
32 =head1 SYNOPSIS
33
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
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
77 All complex software has bugs lurking in it, and this module is no 
78 exception. If you find a bug please either email me, or add the bug
79 to cpan-RT.
80
81 =head1 AUTHOR
82
83 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
84
85 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
86
87 =head1 COPYRIGHT AND LICENSE
88
89 Copyright 2007 by Infinity Interactive, Inc.
90
91 L<http://www.iinteractive.com>
92
93 This library is free software; you can redistribute it and/or modify
94 it under the same terms as Perl itself.
95
96 =cut
97
98