docs
[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 requires 'pack';
8 requires 'unpack';
9
10 sub thaw {
11     my ( $class, $json ) = @_;
12     $class->unpack( JSON::Any->jsonToObj($json) );
13 }
14
15 sub freeze {
16     my $self = shift;
17     JSON::Any->objToJson( $self->pack() );
18 }
19
20 1;
21
22 __END__
23
24 =pod
25
26 =head1 NAME
27
28 MooseX::Storage::Format::JSON
29
30 =head1 SYNOPSIS
31
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
55 =head1 DESCRIPTION
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