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