File Coverage

blib/lib/JSON/Dumper/Compact.pm
Criterion Covered Total %
statement 24 25 96.0
branch n/a
condition n/a
subroutine 14 15 93.3
pod 2 2 100.0
total 40 42 95.2


line stmt bran cond sub pod time code
1             package JSON::Dumper::Compact;
2              
3 1     1   583 use JSON::MaybeXS;
  1         3  
  1         62  
4 1     1   7 use Mu::Tiny;
  1         2  
  1         12  
5 1     1   724 use Class::Method::Modifiers;
  1         1591  
  1         504  
6              
7             our $VERSION = '0.005002';
8             $VERSION =~ tr/_//d;
9              
10             extends 'Data::Dumper::Compact';
11              
12             lazy json_obj => sub {
13             JSON->new
14             ->allow_nonref(1)
15             ->relaxed(1)
16             ->filter_json_single_key_object(__bless__ => sub {
17 1     1   31 bless($_[0][1], $_[0][0]);
18 18     18   113 });
19             };
20              
21 9     9   28 sub _json_decode { shift->json_obj->decode(@_) }
22              
23 68     68   377 sub _build_dumper { my $j = shift->json_obj; sub { $j->encode($_[0]) } }
  9     9   58  
  9         136  
24              
25 119     119   293 sub _format_el { shift->_format(@_).',' }
26              
27 27     27   100 sub _format_hashkey { $_[0]->json_obj->encode($_[1]).':' }
28              
29 141     141   582 sub _format_string { '"'.$_[1].'"' }
30              
31 15     15   54 sub _format_thing { $_[1] }
32              
33             around _expand_blessed => sub {
34             my ($orig, $self) = (shift, shift);
35             my ($blessed) = @_;
36             return $self->expand($blessed->TO_JSON) if $blessed->can('TO_JSON');
37             return $self->$orig(@_);
38             };
39              
40             sub _format_blessed {
41 2     2   4 my ($self, $payload) = @_;
42 2         6 my ($content, $class) = @$payload;
43 2         9 $self->_format([ hash => [
44             [ '__bless__' ],
45             { '__bless__' => [ array => [ [ string => $class ], $content ] ] },
46             ] ]);
47             }
48              
49 0     0 1 0 sub encode { shift->dump(@_) }
50              
51             sub decode {
52 9     9 1 26 my ($self, $data, $opts) = @_;
53 9         31 $self->_optify($opts, _json_decode => $data);
54             }
55              
56             1;
57              
58             =head1 NAME
59              
60             JSON::Dumper::Compact - JSON processing with L aesthetics
61              
62             =head1 SYNOPSIS
63              
64             use JSON::Dumper::Compact 'jdc';
65            
66             my $json = jdc($data);
67              
68             =head1 DESCRIPTION
69              
70             JSON::Dumper::Compact is a subclass of L that turns
71             arrayrefs and hashrefs intead into JSON.
72              
73             Deep data structures are rendered highly compactly:
74              
75             [
76             "1556933590.65383", "Fri May 3 18:33:10 2019", 26794, "INFO", 3,
77             [ "SRV:8FB66F32" ], [ [
78             "/opt/voice-srvc-native/bin/async-srvc-att-gateway-poller", 33,
79             "NERV::Voice::SRV::Native::AsyncSRVATTGatewayPoller::main",
80             ] ],
81             "batch_nena_messages returned", "OK", 6, { "FILENAME": "lqxw020323" },
82             1556933584, "lqxw020323",
83             ]
84              
85             To ease debugging, blessed references without a C method are
86             rendered as an object with a single two-element arrayref value:
87              
88             { "__bless__": [
89             "The::Class",
90             { "the": "object" },
91             ] }
92              
93             =head1 METHODS
94              
95             In addition to the L methods, we provide:
96              
97             =head2 encode
98              
99             JSON::Dumper::Compact->encode($data, \%opts?);
100             $jdc->encode($data, \%opts?);
101              
102             Operates identically to L but named to be less
103             confusing to code expecting a JSON object.
104              
105             =head2 decode
106              
107             JSON::Dumper::Compact->decode($string, \%opts?);
108             $jdc->decode($string, \%opts);
109              
110             Runs the supplied string through an L C with options
111             set to be able to reliably reparse what we can currently format - notably
112             setting C to allow for trailing commas and using
113             C to re-inflate blessed objects.
114              
115             Note that using this method on untrusted data is a security risk. While
116             C/C should be usable for JSON formatting, in general,
117             C fully rehydrates for debugging purposes and as such can e.g.
118             cause DESTROY methods to be called unexpectedly, which can allow a
119             malicious user to do things to your perl5 VM. Rather than using
120             debugging specific code on untrusted data, use L or
121             L directly (if the C output doesn't parse correctly
122             via other libraries, please report that as a bug)..
123              
124             DO NOT USE THIS METHOD ON UNTRUSTED DATA IT WAS NOT DESIGNED TO BE SECURE.
125              
126             =head1 COPYRIGHT
127              
128             Copyright (c) 2019 the L and
129             L as listed in L.
130              
131             =head1 LICENSE
132              
133             This library is free software and may be distributed under the same terms
134             as perl itself. See L.
135              
136             =cut