File Coverage

blib/lib/Data/Printer/Filter/JSON.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Data::Printer::Filter::JSON;
2              
3 9     9   830420 use 5.006;
  9         36  
  9         332  
4 9     9   50 use strict;
  9         15  
  9         309  
5 9     9   46 use warnings 'all';
  9         24  
  9         406  
6 9     9   48 use Carp;
  9         13  
  9         753  
7              
8 9     9   7761 use Data::Printer::Filter qw/filter p/;
  9         64095  
  9         70  
9 9     9   919 use Term::ANSIColor;
  9         20  
  9         2854  
10              
11             our $VERSION = '0.3';
12              
13             =head1 NAME
14              
15             Data::Printer::Filter::JSON - pretty-print your decoded JSON structures!
16              
17             =head1 VERSION
18              
19             Version 0.03
20              
21             =head1 SYNOPSIS
22              
23             # In your program:
24              
25             use Data::Printer filters => {
26             -external => [ 'JSON' ],
27             };
28              
29             # or, in your C<.dataprinter> file:
30              
31             {
32             filters => {
33             -external => [ 'JSON' ],
34             },
35             };
36              
37             # You can also tweak the colors:
38              
39             use Data::Printer {
40             filters => {
41             -external => [ 'JSON' ],
42             }, color => {
43             JSON => {
44             true => 'bright_blue on_black',
45             false => 'black on_bright_blue'
46             }
47             },
48             };
49              
50             =head1 DESCRIPTION
51              
52             Almost every JSON decoder on CPAN handles JavaScript's booleans with objects, and some
53             even reuse them in the resulting data structure. The result? A tiny JSON like this:
54              
55             {
56             "alpha": true,
57             "beta" : false,
58             "gamma": true,
59             "zeta" : false
60             }
61              
62             Results in this Data::Printer output:
63              
64             \ {
65             alpha JSON::XS::Boolean {
66             Parents JSON::Boolean
67             public methods (0)
68             private methods (1) : __ANON__
69             internals: 1
70             },
71             beta JSON::XS::Boolean {
72             Parents JSON::Boolean
73             public methods (0)
74             private methods (1) : __ANON__
75             internals: 0
76             },
77             gamma var{alpha},
78             zeta var{beta}
79             }
80              
81             While all I wanted was this:
82              
83             \ {
84             alpha true,
85             beta false,
86             gamma true,
87             zeta false
88             }
89              
90             This module fixes that! :)
91              
92             =head2 Handles
93              
94             L and L (L 2.x), L (L 1.x),
95             L, L, L, L, L (used by
96             L) and L.
97              
98             =head3 JSON::JOM Caveat
99              
100             When working with L, make sure you load it B loading L.
101             This is L's working on it!|https://github.com/garu/Data-Printer/issues/33>.
102              
103             =head2 Can't Handle
104              
105             The output of any JSON decoder that does NOT use a blessed reference for its
106             booleans, like L or L.
107              
108             =head1 AUTHOR
109              
110             Nuba Princigalli
111              
112             =head1 CONTRIBUTORS
113              
114             Tim Heaney
115              
116             =head1 COPYRIGHT AND LICENSE
117              
118             Copyright (c) 2012, Nuba Princigalli . All rights reserved.
119              
120             This is free software; you can redistribute it and/or modify it under the same
121             terms as the Perl 5 programming language system itself.
122              
123             =cut
124              
125              
126             # JSON::NotString is from JSON 1.x
127             # boolean is used by Pegex::JSON
128              
129             for ( qw/ JSON::DWIW::Boolean JSON::NotString JSON::PP::Boolean JSON::SL::Boolean
130             JSON::XS::Boolean Mojo::JSON::_Bool boolean /)
131             {
132             filter "$_" =>
133             sub {
134             my ( $obj, $p ) = @_;
135             my $str;
136              
137             if ( $obj->isa('JSON::NotString') ) {
138             $str = $obj->{value};
139             }
140             else {
141             $str = $$obj == 1 ? 'true' : 'false';
142             }
143              
144             my %defaults = (
145             true => 'bright_green on_black',
146             false => 'bright_red on_black'
147             );
148              
149             my $color = $p->{JSON}{$str};
150             $color = $defaults{$str} unless defined $color;
151             return colored( $str, $color );
152             },
153             {
154             show_repeated => 1, # handles object reuse
155             };
156             };
157              
158             for ( qw/ JSON::JOM::Value JSON::JOM::Array JSON::JOM::Object /)
159             {
160             filter "$_" =>
161             sub {
162             my ( $obj, $p ) = @_;
163             return p($obj->TO_JSON);
164             };
165             };
166            
167              
168             1;