| 1 | package Search::Elasticsearch::Role::Serializer::JSON; | ||||||||||||||
| 2 | 55 | $Search::Elasticsearch::Role::Serializer::JSON::VERSION = '6.80'; | |||||||||||||
| 3 | 55 | ▶ | use Moo::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 55 | requires 'JSON'; | |||||||||||||
| 5 | |||||||||||||||
| 6 | 55 | ▶ | use Search::Elasticsearch::Util qw(throw); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 55 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 55 | ▶ | use Encode qw(encode_utf8 decode_utf8 is_utf8); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 55 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 55 | has 'mime_type' => ( is => 'ro', default => 'application/json' ); | |||||||||||||
| 12 | |||||||||||||||
| 13 | 55 | with 'Search::Elasticsearch::Role::Serializer'; | |||||||||||||
| 14 | |||||||||||||||
| 15 | #=================================== | ||||||||||||||
| 16 | sub encode { | ||||||||||||||
| 17 | #=================================== | ||||||||||||||
| 18 | 117 | ▶ | my ( $self, $var ) = @_; | ||||||||||||
|
Subroutine
encode: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 19 | 117 | ▶ | unless ( ref $var ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | 60 | ▶ | return is_utf8($var) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | ? encode_utf8($var) | ||||||||||||||
| 22 | : $var; | ||||||||||||||
| 23 | } | ||||||||||||||
| 24 | 57 | ▶ | return try { $self->JSON->encode($var) } | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 57 | ▶ | catch { throw( "Serializer", $_, { var => $var } ) }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | } | ||||||||||||||
| 27 | |||||||||||||||
| 28 | #=================================== | ||||||||||||||
| 29 | sub encode_bulk { | ||||||||||||||
| 30 | #=================================== | ||||||||||||||
| 31 | 1 | ▶ | my ( $self, $var ) = @_; | ||||||||||||
|
Subroutine
encode_bulk: called CC=4 SCAR=16.0
Pod
documented
| |||||||||||||||
| 32 | 1 | ▶ | unless ( ref $var ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | 0 | ▶ | return is_utf8($var) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | ? encode_utf8($var) | ||||||||||||||
| 35 | : $var; | ||||||||||||||
| 36 | } | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 1 | my $json = ''; | |||||||||||||
| 39 | 1 | ▶ | throw( "Param", "Var must be an array ref" ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | unless ref $var eq 'ARRAY'; | ||||||||||||||
| 41 | return try { | ||||||||||||||
| 42 | 1 | ▶ | for (@$var) { | ||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||
| 43 | 1 | ▶ | $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | } | ||||||||||||||
| 45 | 1 | return $json; | |||||||||||||
| 46 | } | ||||||||||||||
| 47 | 1 | ▶ | catch { throw( "Serializer", $_, { var => $var } ) }; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | #=================================== | ||||||||||||||
| 51 | sub encode_pretty { | ||||||||||||||
| 52 | #=================================== | ||||||||||||||
| 53 | 34 | ▶ | my ( $self, $var ) = @_; | ||||||||||||
|
Subroutine
encode_pretty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 54 | 34 | $self->JSON->pretty(1); | |||||||||||||
| 55 | |||||||||||||||
| 56 | 34 | my $json; | |||||||||||||
| 57 | try { | ||||||||||||||
| 58 | 34 | ▶ | $json = $self->encode($var); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 59 | } | ||||||||||||||
| 60 | catch { | ||||||||||||||
| 61 | 3 | ▶ | die "$_"; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 62 | } | ||||||||||||||
| 63 | finally { | ||||||||||||||
| 64 | 34 | ▶ | $self->JSON->pretty(0); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | 34 | }; | |||||||||||||
| 66 | |||||||||||||||
| 67 | 31 | return $json; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | #=================================== | ||||||||||||||
| 71 | sub decode { | ||||||||||||||
| 72 | #=================================== | ||||||||||||||
| 73 | 254 | ▶ | my ( $self, $json ) = @_; | ||||||||||||
|
Subroutine
decode: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 74 | |||||||||||||||
| 75 | 254 | ▶ | return unless defined $json; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | 184 | ▶ | return is_utf8($json) ? $json : decode_utf8($json) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | unless substr( $json, 0, 1 ) =~ /^[\[{]/; | ||||||||||||||
| 79 | |||||||||||||||
| 80 | return try { | ||||||||||||||
| 81 | 46 | ▶ | $self->JSON->decode($json); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | } | ||||||||||||||
| 83 | catch { | ||||||||||||||
| 84 | 6 | ▶ | throw( "Serializer", $_, { json => $json } ); | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 85 | 46 | }; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | |||||||||||||||
| 88 | #=================================== | ||||||||||||||
| 89 | sub _set_canonical { | ||||||||||||||
| 90 | #=================================== | ||||||||||||||
| 91 | 0 | ▶ | shift()->JSON->canonical(1); | ||||||||||||
|
Subroutine
_set_canonical: not called CC=1 SCAR=6.9
| |||||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 55 | 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 6.80 | ||||||||||||||
| 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 | Enrico Zimuel <enrico.zimuel@elastic.co> | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 156 | |||||||||||||||
| 157 | This software is Copyright (c) 2020 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 | __END__ | ||||||||||||||