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