Filename | /opt/flows/lib/lib/perl5/Search/Elasticsearch/Role/Serializer/JSON.pm |
Statements | Executed 17 statements in 972µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 3.27ms | 7.01ms | BEGIN@8 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 17µs | 357µs | BEGIN@3 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 13µs | 54µs | BEGIN@7 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 12µs | 264µs | BEGIN@6 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 9µs | 291µs | BEGIN@9 | Search::Elasticsearch::Role::Serializer::JSON::
1 | 1 | 1 | 7µs | 44µs | __ANON__[:3] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:24] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:25] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:46] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:47] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:59] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:62] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:65] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:82] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | __ANON__[:85] | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | _set_canonical | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | decode | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | encode | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | encode_bulk | Search::Elasticsearch::Role::Serializer::JSON::
0 | 0 | 0 | 0s | 0s | encode_pretty | Search::Elasticsearch::Role::Serializer::JSON::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Search::Elasticsearch::Role::Serializer::JSON; | ||||
2 | 1 | 600ns | $Search::Elasticsearch::Role::Serializer::JSON::VERSION = '2.02'; | ||
3 | 4 | 60µs | 3 | 734µs | # spent 44µs (7+37) within Search::Elasticsearch::Role::Serializer::JSON::__ANON__[/opt/flows/lib/lib/perl5/Search/Elasticsearch/Role/Serializer/JSON.pm:3] which was called:
# once (7µs+37µs) by import::into at line 34 of Import/Into.pm
# spent 357µs (17+340) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@3 which was called:
# once (17µs+340µs) by Module::Runtime::require_module at line 3 # spent 357µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@3
# spent 340µs making 1 call to Moo::Role::import
# spent 37µs making 1 call to strictures::import |
4 | 1 | 2µs | 1 | 11µs | requires 'JSON'; # spent 11µs making 1 call to Moo::Role::requires |
5 | |||||
6 | 2 | 42µs | 2 | 516µs | # spent 264µs (12+252) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@6 which was called:
# once (12µs+252µs) by Module::Runtime::require_module at line 6 # spent 264µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@6
# spent 252µs making 1 call to Sub::Exporter::__ANON__[/opt/flows/lib/lib/perl5/Sub/Exporter.pm:337] |
7 | 2 | 39µs | 2 | 96µs | # spent 54µs (13+42) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@7 which was called:
# once (13µs+42µs) by Module::Runtime::require_module at line 7 # spent 54µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@7
# spent 42µs making 1 call to Exporter::import |
8 | 2 | 132µs | 2 | 7.12ms | # spent 7.01ms (3.27+3.74) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@8 which was called:
# once (3.27ms+3.74ms) by Module::Runtime::require_module at line 8 # spent 7.01ms making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@8
# spent 108µs making 1 call to Exporter::import |
9 | 2 | 673µs | 2 | 572µs | # spent 291µs (9+281) within Search::Elasticsearch::Role::Serializer::JSON::BEGIN@9 which was called:
# once (9µs+281µs) by Module::Runtime::require_module at line 9 # spent 291µs making 1 call to Search::Elasticsearch::Role::Serializer::JSON::BEGIN@9
# spent 281µs making 1 call to namespace::clean::import |
10 | |||||
11 | 1 | 1µs | 1 | 148µs | has 'mime_type' => ( is => 'ro', default => 'application/json' ); # spent 148µs making 1 call to Moo::Role::has |
12 | |||||
13 | 1 | 1µs | 1 | 897µs | with 'Search::Elasticsearch::Role::Serializer'; # spent 897µs making 1 call to Moo::Role::with |
14 | |||||
15 | #=================================== | ||||
16 | sub encode { | ||||
17 | #=================================== | ||||
18 | my ( $self, $var ) = @_; | ||||
19 | unless ( ref $var ) { | ||||
20 | return is_utf8($var) | ||||
21 | ? encode_utf8($var) | ||||
22 | : $var; | ||||
23 | } | ||||
24 | return try { $self->JSON->encode($var) } | ||||
25 | catch { throw( "Serializer", $_, { var => $var } ) }; | ||||
26 | } | ||||
27 | |||||
28 | #=================================== | ||||
29 | sub encode_bulk { | ||||
30 | #=================================== | ||||
31 | my ( $self, $var ) = @_; | ||||
32 | unless ( ref $var ) { | ||||
33 | return is_utf8($var) | ||||
34 | ? encode_utf8($var) | ||||
35 | : $var; | ||||
36 | } | ||||
37 | |||||
38 | my $json = ''; | ||||
39 | throw( "Param", "Var must be an array ref" ) | ||||
40 | unless ref $var eq 'ARRAY'; | ||||
41 | return try { | ||||
42 | for (@$var) { | ||||
43 | $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n"; | ||||
44 | } | ||||
45 | return $json; | ||||
46 | } | ||||
47 | catch { throw( "Serializer", $_, { var => $var } ) }; | ||||
48 | } | ||||
49 | |||||
50 | #=================================== | ||||
51 | sub encode_pretty { | ||||
52 | #=================================== | ||||
53 | my ( $self, $var ) = @_; | ||||
54 | $self->JSON->pretty(1); | ||||
55 | |||||
56 | my $json; | ||||
57 | try { | ||||
58 | $json = $self->encode($var); | ||||
59 | } | ||||
60 | catch { | ||||
61 | die "$_"; | ||||
62 | } | ||||
63 | finally { | ||||
64 | $self->JSON->pretty(0); | ||||
65 | }; | ||||
66 | |||||
67 | return $json; | ||||
68 | } | ||||
69 | |||||
70 | #=================================== | ||||
71 | sub decode { | ||||
72 | #=================================== | ||||
73 | my ( $self, $json ) = @_; | ||||
74 | |||||
75 | return unless defined $json; | ||||
76 | |||||
77 | return is_utf8($json) ? $json : decode_utf8($json) | ||||
78 | unless substr( $json, 0, 1 ) =~ /^[\[{]/; | ||||
79 | |||||
80 | return try { | ||||
81 | $self->JSON->decode($json); | ||||
82 | } | ||||
83 | catch { | ||||
84 | throw( "Serializer", $_, { json => $json } ); | ||||
85 | }; | ||||
86 | } | ||||
87 | |||||
88 | #=================================== | ||||
89 | sub _set_canonical { | ||||
90 | #=================================== | ||||
91 | shift()->JSON->canonical(1); | ||||
92 | } | ||||
93 | |||||
94 | 1 | 7µs | 1; | ||
95 | |||||
96 | =pod | ||||
97 | |||||
98 | =encoding UTF-8 | ||||
99 | |||||
100 | =head1 NAME | ||||
101 | |||||
102 | Search::Elasticsearch::Role::Serializer::JSON - A Serializer role for JSON modules | ||||
103 | |||||
104 | =head1 VERSION | ||||
105 | |||||
106 | version 2.02 | ||||
107 | |||||
108 | =head1 DESCRIPTION | ||||
109 | |||||
110 | This role encodes Perl data structures into JSON strings, and | ||||
111 | decodes JSON strings into Perl data structures. | ||||
112 | |||||
113 | =head1 METHODS | ||||
114 | |||||
115 | =head2 C<encode()> | ||||
116 | |||||
117 | $bytes = $serializer->encode($ref); | ||||
118 | $bytes = $serializer->encode($str); | ||||
119 | |||||
120 | The L</encode()> method converts array and hash refs into their JSON | ||||
121 | equivalents. If a string is passed in, it is returned as the UTF8 encoded | ||||
122 | version of itself. The empty string and C<undef> are returned as is. | ||||
123 | |||||
124 | =head2 C<encode_pretty()> | ||||
125 | |||||
126 | $bytes = $serializer->encode_pretty($ref); | ||||
127 | $bytes = $serializer->encode_pretty($str); | ||||
128 | |||||
129 | Works exactly as L</encode()> but the JSON output is pretty-printed. | ||||
130 | |||||
131 | =head2 C<encode_bulk()> | ||||
132 | |||||
133 | $bytes = $serializer->encode_bulk([\%hash,\%hash,...]); | ||||
134 | $bytes = $serializer->encode_bulk([$str,$str,...]); | ||||
135 | |||||
136 | The L</encode_bulk()> method expects an array ref of hashes or strings. | ||||
137 | Each hash or string is processed by L</encode()> then joined together | ||||
138 | by newline characters, with a final newline character appended to the end. | ||||
139 | This is the special JSON format used for bulk requests. | ||||
140 | |||||
141 | =head2 C<decode()> | ||||
142 | |||||
143 | $var = $serializer->decode($json_bytes); | ||||
144 | $str = $serializer->decode($bytes); | ||||
145 | |||||
146 | If the passed in value looks like JSON (ie starts with a C<{> or C<[> | ||||
147 | character), then it is decoded from JSON, otherwise it is returned as | ||||
148 | the UTF8 decoded version of itself. The empty string and C<undef> are | ||||
149 | returned as is. | ||||
150 | |||||
151 | =head1 AUTHOR | ||||
152 | |||||
153 | Clinton Gormley <drtech@cpan.org> | ||||
154 | |||||
155 | =head1 COPYRIGHT AND LICENSE | ||||
156 | |||||
157 | This software is Copyright (c) 2016 by Elasticsearch BV. | ||||
158 | |||||
159 | This is free software, licensed under: | ||||
160 | |||||
161 | The Apache License, Version 2.0, January 2004 | ||||
162 | |||||
163 | =cut | ||||
164 | |||||
165 | 1 | 14µs | 1 | 535µs | __END__ # spent 535µs making 1 call to B::Hooks::EndOfScope::XS::__ANON__[/opt/flows/lib/lib/perl5/B/Hooks/EndOfScope/XS.pm:17] |