File Coverage

blib/lib/Eve/Json.pm
Criterion Covered Total %
statement 36 38 94.7
branch 6 8 75.0
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 53 57 92.9


line stmt bran cond sub pod time code
1             package Eve::Json;
2              
3 9     9   16680 use parent qw(Eve::Class);
  9         17  
  9         77  
4              
5 9     9   580 use strict;
  9         22  
  9         302  
6 9     9   50 use warnings;
  9         18  
  9         256  
7              
8 9     9   1278 use JSON::XS;
  9         3993  
  9         607  
9              
10 9     9   55 use Eve::Exception;
  9         19  
  9         3029  
11              
12             =head1 NAME
13              
14             B - 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 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
33              
34             =cut
35              
36             sub init {
37 4     4 1 8 my $self = shift;
38              
39 4         896 $self->{'json'} = JSON::XS->new()->pretty(1);
40              
41 4         74 $self->json->utf8();
42             }
43              
44             =head2 B
45              
46             Encodes a reference and returns its JSON string representation.
47              
48             =head3 Arguments
49              
50             =over 4
51              
52             =item C
53              
54             =back
55              
56             =cut
57              
58             sub encode {
59 4     4 1 1629 my ($self, %arg_hash) = @_;
60 4         15 Eve::Support::arguments(\%arg_hash, my $reference);
61              
62 4         39 my $result;
63 4         6 eval {
64 4         57 $result = $self->{'json'}->encode($reference);
65             };
66              
67 4         2250 my $e;
68 4 100       23 if ($e = Eve::Exception::Die->caught()) {
    50          
69 1         299 Eve::Error::Value->throw(message => $e->message);
70             } elsif ($e = Exception::Class::Base->caught()) {
71 0         0 $e->rethrow();
72             }
73              
74 3         110 return $result;
75             }
76              
77             =head2 B
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
86              
87             =back
88              
89             =cut
90              
91             sub decode {
92 4     4 1 3959 my ($self, %arg_hash) = @_;
93 4         17 Eve::Support::arguments(\%arg_hash, my $string);
94              
95 4         42 my $result;
96 4         6 eval {
97 4         65 $result = $self->{'json'}->decode($string);
98             };
99              
100 4         2287 my $e;
101 4 100       35 if ($e = Eve::Exception::Die->caught()) {
    50          
102 1         269 Eve::Error::Value->throw(message => $e->message);
103             } elsif ($e = Exception::Class::Base->caught()) {
104 0         0 $e->rethrow();
105             }
106              
107 3         105 return $result;
108             }
109              
110             =head1 SEE ALSO
111              
112             =over 4
113              
114             =item C
115              
116             =item C
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
136              
137             =back
138              
139             =cut
140              
141             1;