line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package JSON::HPack; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
80628
|
use common::sense; |
|
3
|
|
|
|
|
29
|
|
|
3
|
|
|
|
|
17
|
|
4
|
3
|
|
|
3
|
|
171
|
use constant FIRST => 0; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
231
|
|
5
|
3
|
|
|
3
|
|
3808
|
use JSON::Any; |
|
3
|
|
|
|
|
83172
|
|
|
3
|
|
|
|
|
23
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = q(0.0.3); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
JSON-HPack - JSON Homogeneous Collections Compressor |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use JSON::HPack; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
JSON::HPack->pack( [ |
19
|
|
|
|
|
|
|
{ |
20
|
|
|
|
|
|
|
name => 'Larry Wall', |
21
|
|
|
|
|
|
|
nick => 'timtowtdi' |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
] ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# - OR - |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
JSON::HPack->dump( [ |
28
|
|
|
|
|
|
|
{ |
29
|
|
|
|
|
|
|
name => 'Larry Wall', |
30
|
|
|
|
|
|
|
nick => 'timtowtdi' |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
] ) |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# To Unpack |
35
|
|
|
|
|
|
|
JSON::HPack->unpack( |
36
|
|
|
|
|
|
|
[ 2, 'name', 'nick', 'Larry Wall', 'timtowdi' ] |
37
|
|
|
|
|
|
|
) |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# - OR use JSON string directly |
40
|
|
|
|
|
|
|
JSON::HPack->load( $json_string ) |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DESCRIPTION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
JSON HPack perl implementation is based on other implementations available on Github L |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Usually a database result set, stored as list of objects where all of them contains the same amount |
47
|
|
|
|
|
|
|
of keys with identical name. This is a basic homogeneous collection example: |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
[{"a":"A","b":"B"},{"a":"C","b":"D"},{"a":"E","b":"F"}] |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
We all have exchange over the network one or more homogenous collections at least once. JSON::HPack is able to |
52
|
|
|
|
|
|
|
pack the example into: |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
[2,"a","b","A","B","C","D","E","F"] |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
and unpack it into original collection at light speed. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head2 C |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$packed_structure = JSON::HPack->pack( $unpacked_structure ); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 C |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$unpacked_structure = JSON::HPack->unpack( $packed_structure ); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head2 C |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$packed_json = JSON::HPack->dump( $unpacked_structure ); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head2 C |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$unpacked_structure = JSON::HPack->load( $packed_json ); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head1 BUGS |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
Please report them. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub pack { |
92
|
2
|
|
|
2
|
1
|
2015
|
my ( $class, $aoh ) = @_; |
93
|
|
|
|
|
|
|
|
94
|
2
|
|
|
|
|
3
|
my %first = %{ $aoh->[FIRST] }; |
|
2
|
|
|
|
|
10
|
|
95
|
2
|
|
|
|
|
5
|
my $key_size = scalar( keys( %first ) ); |
96
|
2
|
|
|
|
|
8
|
my @keys = keys( %first ); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
[ |
99
|
6
|
|
|
|
|
9
|
$key_size, |
100
|
|
|
|
|
|
|
@keys, |
101
|
|
|
|
|
|
|
map { |
102
|
2
|
|
|
|
|
7
|
my $this = $_; |
103
|
18
|
|
|
|
|
38
|
map { |
104
|
6
|
|
|
|
|
9
|
$this->{$_} |
105
|
|
|
|
|
|
|
} @keys |
106
|
2
|
|
|
|
|
7
|
} @{ $aoh }[ 0 .. ( scalar( @$aoh ) - 1 ) ] |
107
|
|
|
|
|
|
|
]; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub unpack { |
112
|
1
|
|
|
1
|
1
|
40
|
my ( $class, $pa ) = @_; |
113
|
|
|
|
|
|
|
|
114
|
1
|
|
|
|
|
18
|
my ( $results, @keys ) = ( |
115
|
|
|
|
|
|
|
[ ], |
116
|
1
|
|
|
|
|
6
|
@{ $pa }[ 1 .. $pa->[ FIRST ] ] |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
|
119
|
1
|
|
|
|
|
4
|
my ( $start, $length ) = ( scalar( @keys ) ) x 2; |
120
|
|
|
|
|
|
|
|
121
|
1
|
|
|
|
|
7
|
LOOP: while( ( $start + 1 + $length ) <= @$pa ) { |
122
|
4
|
|
|
|
|
11
|
my @values = @{ $pa }[ $start + 1 .. ( $start + $length ) ]; |
|
4
|
|
|
|
|
10
|
|
123
|
|
|
|
|
|
|
|
124
|
12
|
|
|
|
|
27
|
my %hash = ( |
125
|
|
|
|
|
|
|
map { |
126
|
4
|
|
|
|
|
8
|
$keys[ $_ ] => $values[ $_ ] |
127
|
|
|
|
|
|
|
} ( 0 .. ( $length - 1 ) ) |
128
|
|
|
|
|
|
|
); |
129
|
|
|
|
|
|
|
|
130
|
4
|
|
|
|
|
15
|
push( @$results, { %hash } ); |
131
|
|
|
|
|
|
|
|
132
|
4
|
|
|
|
|
15
|
$start += $length; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
1
|
|
|
|
|
4
|
$results; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub load { |
141
|
0
|
|
|
0
|
1
|
|
my ( $class, $string ) = @_; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
$class->unpack( |
144
|
|
|
|
|
|
|
JSON::Any |
145
|
|
|
|
|
|
|
->new |
146
|
|
|
|
|
|
|
->jsonToObj( $string ) |
147
|
|
|
|
|
|
|
); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub dump { |
151
|
0
|
|
|
0
|
1
|
|
my ( $class, $struct ) = @_; |
152
|
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
JSON::Any->new |
154
|
|
|
|
|
|
|
->objToJson( |
155
|
|
|
|
|
|
|
$class->pack( $struct ) |
156
|
|
|
|
|
|
|
); |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |
164
|
|
|
|
|
|
|
__END__ |