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