1 package MooseX::Storage::Format::JSON;
10 our $AUTHORITY = 'cpan:STEVAN';
16 my ( $class, $json, @args ) = @_;
17 utf8::encode($json) if utf8::is_utf8($json);
18 $class->unpack( JSON::Any->new->jsonToObj($json), @args );
22 my ( $self, @args ) = @_;
23 my $json = JSON::Any->new(canonical => 1)->objToJson( $self->pack(@args) );
24 utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json); # if it's valid utf8 mark it as such
38 MooseX::Storage::Format::JSON - A JSON serialization role
46 with Storage('format' => 'JSON');
48 has 'x' => (is => 'rw', isa => 'Int');
49 has 'y' => (is => 'rw', isa => 'Int');
53 my $p = Point->new(x => 10, y => 10);
55 ## methods to freeze/thaw into
56 ## a specified serialization format
57 ## (in this case JSON)
59 # pack the class into a JSON string
60 $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
62 # unpack the JSON string into a class
63 my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');
85 All complex software has bugs lurking in it, and this module is no
86 exception. If you find a bug please either email me, or add the bug
91 Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
93 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
95 Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
97 =head1 COPYRIGHT AND LICENSE
99 Copyright 2007-2008 by Infinity Interactive, Inc.
101 L<http://www.iinteractive.com>
103 This library is free software; you can redistribute it and/or modify
104 it under the same terms as Perl itself.