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