line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MooseX::Storage::Format::JSONpm; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$MooseX::Storage::Format::JSONpm::VERSION = '0.093093'; |
4
|
|
|
|
|
|
|
} |
5
|
1
|
|
|
1
|
|
438662
|
use MooseX::Role::Parameterized; |
|
1
|
|
|
|
|
93259
|
|
|
1
|
|
|
|
|
8
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: a format role for MooseX::Storage using JSON.pm |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
57414
|
use namespace::autoclean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
70
|
use JSON; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
parameter json_opts => ( |
14
|
|
|
|
|
|
|
isa => 'HashRef', |
15
|
|
|
|
|
|
|
default => sub { return { } }, |
16
|
|
|
|
|
|
|
initializer => sub { |
17
|
|
|
|
|
|
|
my ($self, $value, $set) = @_; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
%$value = (ascii => 1, %$value); |
20
|
|
|
|
|
|
|
$set->($value); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
role { |
25
|
|
|
|
|
|
|
my $p = shift; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
requires 'pack'; |
28
|
|
|
|
|
|
|
requires 'unpack'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
method freeze => sub { |
32
|
3
|
|
|
3
|
|
18537
|
my ($self, @args) = @_; |
33
|
|
|
|
|
|
|
|
34
|
3
|
|
|
|
|
19
|
my $json = to_json($self->pack(@args), $p->json_opts); |
35
|
3
|
|
|
|
|
12217
|
return $json; |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
method thaw => sub { |
40
|
0
|
|
|
0
|
|
|
my ($class, $json, @args) = @_; |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
$class->unpack( from_json($json, $p->json_opts), @args); |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
1; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
__END__ |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=pod |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 NAME |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
MooseX::Storage::Format::JSONpm - a format role for MooseX::Storage using JSON.pm |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 VERSION |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
version 0.093093 |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SYNOPSIS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
package Point; |
64
|
|
|
|
|
|
|
use Moose; |
65
|
|
|
|
|
|
|
use MooseX::Storage; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
with Storage(format => 'JSONpm'); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
has 'x' => (is => 'rw', isa => 'Int'); |
70
|
|
|
|
|
|
|
has 'y' => (is => 'rw', isa => 'Int'); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
1; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $p = Point->new(x => 10, y => 10); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# pack the class into a JSON string |
77
|
|
|
|
|
|
|
my $json = $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 } |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# unpack the JSON string into an object |
80
|
|
|
|
|
|
|
my $p2 = Point->thaw($json); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
...in other words, it can be used as a drop-in replacement for |
83
|
|
|
|
|
|
|
MooseX::Storage::Format::JSON. However, it can also be parameterized: |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
package Point; |
86
|
|
|
|
|
|
|
use Moose; |
87
|
|
|
|
|
|
|
use MooseX::Storage; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
with Storage(format => [ JSONpm => { json_opts => { pretty => 1 } } ]); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
At present, C<json_opts> is the only parameter, and is used when calling the |
92
|
|
|
|
|
|
|
C<to_json> and C<from_json> routines provided by the L<JSON|JSON> library. |
93
|
|
|
|
|
|
|
Default values are merged into the given hashref (with explict values taking |
94
|
|
|
|
|
|
|
priority). The defaults are as follows: |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
{ ascii => 1 } |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 METHODS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head2 freeze |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
my $json = $obj->freeze; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head2 thaw |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $obj = Class->thaw($json) |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 THANKS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Thanks to Stevan Little, Chris Prather, and Yuval Kogman, from whom I cribbed |
111
|
|
|
|
|
|
|
this code -- from MooseX::Storage::Format::JSON. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 AUTHOR |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Ricardo SIGNES <rjbs@cpan.org> |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Ricardo SIGNES. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
122
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=cut |