File Coverage

blib/lib/MooseX/Storage/Format/JSON.pm
Criterion Covered Total %
statement 16 16 100.0
branch 3 4 75.0
condition 1 3 33.3
subroutine 5 5 100.0
pod 2 2 100.0
total 27 30 90.0


line stmt bran cond sub pod time code
1             package MooseX::Storage::Format::JSON;
2             # ABSTRACT: A JSON serialization role
3              
4             our $VERSION = '0.53';
5              
6 10     10   5572 use Moose::Role;
  10         22  
  10         75  
7 10     10   45984 use JSON::MaybeXS 1.001000;
  10         286  
  10         627  
8 10     10   56 use namespace::autoclean;
  10         37  
  10         103  
9              
10             requires 'pack';
11             requires 'unpack';
12              
13             sub thaw {
14 12     12 1 15196 my ( $class, $json, @args ) = @_;
15              
16             # TODO ugh! this is surely wrong and should be fixed.
17 12 100       63 utf8::encode($json) if utf8::is_utf8($json);
18              
19 12         106 $class->unpack( JSON::MaybeXS->new({ utf8 => 1 })->decode( $json), @args );
20             }
21              
22             sub freeze {
23 15     15 1 23960 my ( $self, @args ) = @_;
24              
25 15         190 my $json = JSON::MaybeXS->new({ utf8 => 1, canonical => 1 })->encode($self->pack(@args));
26              
27             # if it's valid utf8 mark it as such
28             # TODO ugh! this is surely wrong and should be fixed.
29 15 50 33     1901 utf8::decode($json) if !utf8::is_utf8($json) and utf8::valid($json);
30              
31 15         108 return $json;
32             }
33              
34             1;
35              
36             __END__
37              
38             =pod
39              
40             =encoding UTF-8
41              
42             =head1 NAME
43              
44             MooseX::Storage::Format::JSON - A JSON serialization role
45              
46             =head1 VERSION
47              
48             version 0.53
49              
50             =head1 SYNOPSIS
51              
52             package Point;
53             use Moose;
54             use MooseX::Storage;
55              
56             with Storage('format' => 'JSON');
57              
58             has 'x' => (is => 'rw', isa => 'Int');
59             has 'y' => (is => 'rw', isa => 'Int');
60              
61             1;
62              
63             my $p = Point->new(x => 10, y => 10);
64              
65             ## methods to freeze/thaw into
66             ## a specified serialization format
67             ## (in this case JSON)
68              
69             # pack the class into a JSON string
70             $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
71              
72             # unpack the JSON string into a class
73             my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');
74              
75             =head1 METHODS
76              
77             =over 4
78              
79             =item B<freeze>
80              
81             =item B<thaw ($json)>
82              
83             =back
84              
85             =head1 SUPPORT
86              
87             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Storage>
88             (or L<bug-MooseX-Storage@rt.cpan.org|mailto:bug-MooseX-Storage@rt.cpan.org>).
89              
90             There is also a mailing list available for users of this distribution, at
91             L<http://lists.perl.org/list/moose.html>.
92              
93             There is also an irc channel available for users of this distribution, at
94             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
95              
96             =head1 AUTHORS
97              
98             =over 4
99              
100             =item *
101              
102             Chris Prather <chris.prather@iinteractive.com>
103              
104             =item *
105              
106             Stevan Little <stevan.little@iinteractive.com>
107              
108             =item *
109              
110             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
111              
112             =back
113              
114             =head1 COPYRIGHT AND LICENSE
115              
116             This software is copyright (c) 2007 by Infinity Interactive, Inc.
117              
118             This is free software; you can redistribute it and/or modify it under
119             the same terms as the Perl 5 programming language system itself.
120              
121             =cut