File Coverage

lib/MooseX/Storage/Format/JSONpm.pm
Criterion Covered Total %
statement 12 14 85.7
branch n/a
condition n/a
subroutine 4 5 80.0
pod n/a
total 16 19 84.2


line stmt bran cond sub pod time code
1             package MooseX::Storage::Format::JSONpm 0.093094;
2 1     1   45665 use MooseX::Role::Parameterized;
  1         79336  
  1         4  
3             # ABSTRACT: a format role for MooseX::Storage using JSON.pm
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod package Point;
8             #pod use Moose;
9             #pod use MooseX::Storage;
10             #pod
11             #pod with Storage(format => 'JSONpm');
12             #pod
13             #pod has 'x' => (is => 'rw', isa => 'Int');
14             #pod has 'y' => (is => 'rw', isa => 'Int');
15             #pod
16             #pod 1;
17             #pod
18             #pod my $p = Point->new(x => 10, y => 10);
19             #pod
20             #pod # pack the class into a JSON string
21             #pod my $json = $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
22             #pod
23             #pod # unpack the JSON string into an object
24             #pod my $p2 = Point->thaw($json);
25             #pod
26             #pod ...in other words, it can be used as a drop-in replacement for
27             #pod MooseX::Storage::Format::JSON. However, it can also be parameterized:
28             #pod
29             #pod package Point;
30             #pod use Moose;
31             #pod use MooseX::Storage;
32             #pod
33             #pod with Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ]);
34             #pod
35             #pod At present, C<json_opts> is the only parameter, and is used when calling the
36             #pod C<to_json> and C<from_json> routines provided by the L<JSON|JSON> library.
37             #pod Default values are merged into the given hashref (with explict values taking
38             #pod priority). The defaults are as follows:
39             #pod
40             #pod { ascii => 1 }
41             #pod
42             #pod =cut
43              
44 1     1   38921 use namespace::autoclean;
  1         3  
  1         9  
45              
46 1     1   843 use JSON;
  1         9638  
  1         5  
47              
48             parameter json_opts => (
49             isa => 'HashRef',
50             default => sub { return { } },
51             initializer => sub {
52             my ($self, $value, $set) = @_;
53              
54             %$value = (ascii => 1, %$value);
55             $set->($value);
56             }
57             );
58              
59             role {
60             my $p = shift;
61              
62             requires 'pack';
63             requires 'unpack';
64              
65             #pod =method freeze
66             #pod
67             #pod my $json = $obj->freeze;
68             #pod
69             #pod =cut
70              
71             method freeze => sub {
72 3     3   13574 my ($self, @args) = @_;
73              
74 3         11 my $json = to_json($self->pack(@args), $p->json_opts);
75 3         8118 return $json;
76             };
77              
78             #pod =method thaw
79             #pod
80             #pod my $obj = Class->thaw($json)
81             #pod
82             #pod =cut
83              
84             method thaw => sub {
85 0     0     my ($class, $json, @args) = @_;
86              
87 0           $class->unpack( from_json($json, $p->json_opts), @args);
88             };
89              
90             };
91              
92             1;
93              
94             #pod =head1 THANKS
95             #pod
96             #pod Thanks to Stevan Little, Chris Prather, and Yuval Kogman, from whom I cribbed
97             #pod this code -- from MooseX::Storage::Format::JSON.
98              
99             __END__
100              
101             =pod
102              
103             =encoding UTF-8
104              
105             =head1 NAME
106              
107             MooseX::Storage::Format::JSONpm - a format role for MooseX::Storage using JSON.pm
108              
109             =head1 VERSION
110              
111             version 0.093094
112              
113             =head1 SYNOPSIS
114              
115             package Point;
116             use Moose;
117             use MooseX::Storage;
118              
119             with Storage(format => 'JSONpm');
120              
121             has 'x' => (is => 'rw', isa => 'Int');
122             has 'y' => (is => 'rw', isa => 'Int');
123              
124             1;
125              
126             my $p = Point->new(x => 10, y => 10);
127              
128             # pack the class into a JSON string
129             my $json = $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
130              
131             # unpack the JSON string into an object
132             my $p2 = Point->thaw($json);
133              
134             ...in other words, it can be used as a drop-in replacement for
135             MooseX::Storage::Format::JSON. However, it can also be parameterized:
136              
137             package Point;
138             use Moose;
139             use MooseX::Storage;
140              
141             with Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ]);
142              
143             At present, C<json_opts> is the only parameter, and is used when calling the
144             C<to_json> and C<from_json> routines provided by the L<JSON|JSON> library.
145             Default values are merged into the given hashref (with explict values taking
146             priority). The defaults are as follows:
147              
148             { ascii => 1 }
149              
150             =head1 PERL VERSION
151              
152             This library should run on perls released even a long time ago. It should work
153             on any version of perl released in the last five years.
154              
155             Although it may work on older versions of perl, no guarantee is made that the
156             minimum required version will not be increased. The version may be increased
157             for any reason, and there is no promise that patches will be accepted to lower
158             the minimum required perl.
159              
160             =head1 METHODS
161              
162             =head2 freeze
163              
164             my $json = $obj->freeze;
165              
166             =head2 thaw
167              
168             my $obj = Class->thaw($json)
169              
170             =head1 THANKS
171              
172             Thanks to Stevan Little, Chris Prather, and Yuval Kogman, from whom I cribbed
173             this code -- from MooseX::Storage::Format::JSON.
174              
175             =head1 AUTHOR
176              
177             Ricardo SIGNES <cpan@semiotic.systems>
178              
179             =head1 CONTRIBUTOR
180              
181             =for stopwords Ricardo Signes
182              
183             Ricardo Signes <rjbs@semiotic.systems>
184              
185             =head1 COPYRIGHT AND LICENSE
186              
187             This software is copyright (c) 2022 by Ricardo SIGNES.
188              
189             This is free software; you can redistribute it and/or modify it under
190             the same terms as the Perl 5 programming language system itself.
191              
192             =cut