1 package MooseX::Storage::Format::JSON;
13 my ( $class, $json, @args ) = @_;
14 utf8::encode($json) if utf8::is_utf8($json);
15 $class->unpack( JSON::Any->new->jsonToObj($json), @args );
19 my ( $self, @args ) = @_;
20 my $json = JSON::Any->new(canonical => 1)->objToJson( $self->pack(@args) );
21 utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json); # if it's valid utf8 mark it as such
35 MooseX::Storage::Format::JSON - A JSON serialization role
43 with Storage('format' => 'JSON');
45 has 'x' => (is => 'rw', isa => 'Int');
46 has 'y' => (is => 'rw', isa => 'Int');
50 my $p = Point->new(x => 10, y => 10);
52 ## methods to freeze/thaw into
53 ## a specified serialization format
54 ## (in this case JSON)
56 # pack the class into a JSON string
57 $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
59 # unpack the JSON string into a class
60 my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');
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
88 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
90 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
92 Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
94 =head1 COPYRIGHT AND LICENSE
96 Copyright 2007-2008 by Infinity Interactive, Inc.
98 L<http://www.iinteractive.com>
100 This library is free software; you can redistribute it and/or modify
101 it under the same terms as Perl itself.