| 1 | package Eve::Json; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 10 | ▶ | use parent qw(Eve::Class); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 10 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 10 | ▶ | use JSON::XS; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 10 | ▶ | use Eve::Exception; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | =head1 NAME | ||||||||||||||
| 13 | |||||||||||||||
| 14 | B<Eve::Json> - a JSON converter adapter. | ||||||||||||||
| 15 | |||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||
| 17 | |||||||||||||||
| 18 | use Eve::Json; | ||||||||||||||
| 19 | |||||||||||||||
| 20 | my $json = Eve::Json->new(); | ||||||||||||||
| 21 | |||||||||||||||
| 22 | my $json_string = $json->encode(reference => $reference); | ||||||||||||||
| 23 | my $decoded_reference = $json->decode(string => $json_string); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||
| 26 | |||||||||||||||
| 27 | The B<Eve::Json> class adapts the functionality of the JSON::XS | ||||||||||||||
| 28 | module to provide JSON encoding and decoding features service. | ||||||||||||||
| 29 | |||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head2 B<init()> | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =cut | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 4 | ▶ | sub init { | ||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 37 | 4 | my $self = shift; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 4 | $self->{'json'} = JSON::XS->new()->pretty(1); | |||||||||||||
| 40 | |||||||||||||||
| 41 | 4 | $self->json->utf8(); | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | =head2 B<encode()> | ||||||||||||||
| 45 | |||||||||||||||
| 46 | Encodes a reference and returns its JSON string representation. | ||||||||||||||
| 47 | |||||||||||||||
| 48 | =head3 Arguments | ||||||||||||||
| 49 | |||||||||||||||
| 50 | =over 4 | ||||||||||||||
| 51 | |||||||||||||||
| 52 | =item C<reference> | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =back | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =cut | ||||||||||||||
| 57 | |||||||||||||||
| 58 | 4 | ▶ | sub encode { | ||||||||||||
|
Subroutine
encode: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 59 | 4 | my ($self, %arg_hash) = @_; | |||||||||||||
| 60 | 4 | Eve::Support::arguments(\%arg_hash, my $reference); | |||||||||||||
| 61 | |||||||||||||||
| 62 | 4 | my $result; | |||||||||||||
| 63 | 4 | eval { | |||||||||||||
| 64 | 4 | $result = $self->{'json'}->encode($reference); | |||||||||||||
| 65 | }; | ||||||||||||||
| 66 | |||||||||||||||
| 67 | 4 | my $e; | |||||||||||||
| 68 | 4 | ▶ | if ($e = Eve::Exception::Die->caught()) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 69 | 1 | Eve::Error::Value->throw(message => $e->message); | |||||||||||||
| 70 | } elsif ($e = Exception::Class::Base->caught()) { | ||||||||||||||
| 71 | 0 | $e->rethrow(); | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | |||||||||||||||
| 74 | 3 | return $result; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | =head2 B<decode()> | ||||||||||||||
| 78 | |||||||||||||||
| 79 | Decodes a JSON string and returns a reference to its decoded contents. | ||||||||||||||
| 80 | |||||||||||||||
| 81 | =head3 Arguments | ||||||||||||||
| 82 | |||||||||||||||
| 83 | =over 4 | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =item C<string> | ||||||||||||||
| 86 | |||||||||||||||
| 87 | =back | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =cut | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 4 | ▶ | sub decode { | ||||||||||||
|
Subroutine
decode: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||
| 92 | 4 | my ($self, %arg_hash) = @_; | |||||||||||||
| 93 | 4 | Eve::Support::arguments(\%arg_hash, my $string); | |||||||||||||
| 94 | |||||||||||||||
| 95 | 4 | my $result; | |||||||||||||
| 96 | 4 | eval { | |||||||||||||
| 97 | 4 | $result = $self->{'json'}->decode($string); | |||||||||||||
| 98 | }; | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 4 | my $e; | |||||||||||||
| 101 | 4 | ▶ | if ($e = Eve::Exception::Die->caught()) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 102 | 1 | Eve::Error::Value->throw(message => $e->message); | |||||||||||||
| 103 | } elsif ($e = Exception::Class::Base->caught()) { | ||||||||||||||
| 104 | 0 | $e->rethrow(); | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 3 | return $result; | |||||||||||||
| 108 | } | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =head1 SEE ALSO | ||||||||||||||
| 111 | |||||||||||||||
| 112 | =over 4 | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =item C<JSON::XS> | ||||||||||||||
| 115 | |||||||||||||||
| 116 | =item C<Eve::Class> | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =back | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||
| 121 | |||||||||||||||
| 122 | Copyright 2012 Igor Zinovyev. | ||||||||||||||
| 123 | |||||||||||||||
| 124 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||
| 125 | under the terms of either: the GNU General Public License as published | ||||||||||||||
| 126 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||
| 127 | |||||||||||||||
| 128 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||
| 129 | |||||||||||||||
| 130 | |||||||||||||||
| 131 | =head1 AUTHOR | ||||||||||||||
| 132 | |||||||||||||||
| 133 | =over 4 | ||||||||||||||
| 134 | |||||||||||||||
| 135 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =back | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =cut | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 10 | 1; | |||||||||||||