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