2 package MooseX::Storage::Format::JSON;
10 our $VERSION = '0.30';
11 our $AUTHORITY = 'cpan:STEVAN';
17 my ( $class, $json, @args ) = @_;
18 utf8::encode($json) if utf8::is_utf8($json);
19 $class->unpack( JSON::Any->jsonToObj($json), @args );
23 my ( $self, @args ) = @_;
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
39 MooseX::Storage::Format::JSON - A JSON serialization role
47 with Storage('format' => 'JSON');
49 has 'x' => (is => 'rw', isa => 'Int');
50 has 'y' => (is => 'rw', isa => 'Int');
54 my $p = Point->new(x => 10, y => 10);
56 ## methods to freeze/thaw into
57 ## a specified serialization format
58 ## (in this case JSON)
60 # pack the class into a JSON string
61 $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
63 # unpack the JSON string into a class
64 my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');
86 All complex software has bugs lurking in it, and this module is no
87 exception. If you find a bug please either email me, or add the bug
92 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
94 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
96 Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
98 =head1 COPYRIGHT AND LICENSE
100 Copyright 2007-2008 by Infinity Interactive, Inc.
102 L<http://www.iinteractive.com>
104 This library is free software; you can redistribute it and/or modify
105 it under the same terms as Perl itself.