1 package MooseX::Storage::Format::JSON;
12 my ( $class, $json, @args ) = @_;
13 utf8::encode($json) if utf8::is_utf8($json);
14 $class->unpack( JSON::Any->new->jsonToObj($json), @args );
18 my ( $self, @args ) = @_;
19 my $json = JSON::Any->new(canonical => 1)->objToJson( $self->pack(@args) );
20 utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json); # if it's valid utf8 mark it as such
34 MooseX::Storage::Format::JSON - A JSON serialization role
42 with Storage('format' => 'JSON');
44 has 'x' => (is => 'rw', isa => 'Int');
45 has 'y' => (is => 'rw', isa => 'Int');
49 my $p = Point->new(x => 10, y => 10);
51 ## methods to freeze/thaw into
52 ## a specified serialization format
53 ## (in this case JSON)
55 # pack the class into a JSON string
56 $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
58 # unpack the JSON string into a class
59 my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');
81 All complex software has bugs lurking in it, and this module is no
82 exception. If you find a bug please either email me, or add the bug
87 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
89 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
91 Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
93 =head1 COPYRIGHT AND LICENSE
95 Copyright 2007-2008 by Infinity Interactive, Inc.
97 L<http://www.iinteractive.com>
99 This library is free software; you can redistribute it and/or modify
100 it under the same terms as Perl itself.