1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
package Data::Printer::Profile::JSON;
use strict;
use warnings;
sub profile {
return {
show_tainted => 0,
show_unicode => 0,
show_lvalue => 0,
print_escapes => 0,
scalar_quotes => q("),
escape_chars => 'none',
string_max => 0,
unicode_charnames => 0,
array_max => 0,
index => 0,
hash_max => 0,
hash_separator => ': ',
align_hash => 0,
sort_keys => 0,
quote_keys => 1,
name => 'var',
return_value => 'dump',
output => 'stderr',
indent => 2,
show_readonly => 0,
show_tied => 0,
show_dualvar => 'off',
show_weak => 0,
show_refcount => 0,
show_memsize => 0,
separator => ',',
end_separator => 0,
caller_info => 0,
colored => 0,
class_method => undef,
# Data::Printer doesn't provide a way to directly
# decorate filters, so we do it ourselves:
filters => [
{
'-class' => \&_json_class_filter,
'SCALAR' => \&_json_scalar_filter,
'LVALUE' => \&_json_scalar_filter,
'CODE' => \&_json_code_filter,
'FORMAT' => \&_json_format_filter,
'GLOB' => \&_json_glob_filter,
'REF' => \&_json_ref_filter,,
'Regexp' => \&_json_regexp_filter,
'VSTRING' => \&_json_vstring_filter,
},
],
};
}
sub _json_class_filter {
my ($obj, $ddp) = @_;
Data::Printer::Common::_warn($ddp, 'json cannot express blessed objects. Showing internals only');
require Scalar::Util;
my $reftype = Scalar::Util::reftype($obj);
$reftype = 'Regexp' if $reftype eq 'REGEXP';
$ddp->indent;
my $string = $ddp->parse_as($reftype, $obj);
$ddp->outdent;
return $string;
}
sub _json_ref_filter {
my ($ref, $ddp) = @_;
my $reftype = ref $$ref;
if ($reftype ne 'HASH' && $reftype ne 'ARRAY') {
Data::Printer::Common::_warn($ddp, 'json cannot express references to scalars. Cast to non-reference');
}
require Scalar::Util;
my $id = pack 'J', Scalar::Util::refaddr($$ref);
if ($ddp->seen($$ref)) {
Data::Printer::Common::_warn($ddp, 'json cannot express circular references. Cast to string');
return '"' . $ddp->parse($$ref) . '"';
}
return $ddp->parse($$ref);
}
sub _json_glob_filter {
my (undef, $ddp) = @_;
Data::Printer::Common::_warn($ddp, 'json cannot express globs.');
return '';
}
sub _json_format_filter {
my $res = Data::Printer::Filter::FORMAT::parse(@_);
return '"' . $res . '"';
}
sub _json_regexp_filter {
my ($re, $ddp) = @_;
Data::Printer::Common::_warn($ddp, 'regular expression cast to string (flags removed)');
my $v = "$re";
my $mod = "";
if ($v =~ /^\(\?\^?([msixpadlun-]*):([\x00-\xFF]*)\)\z/) {
$mod = $1;
$v = $2;
$mod =~ s/-.*//;
}
$v =~ s{/}{\\/}g;
return '"' . "/$v/$mod" . '"';
}
sub _json_vstring_filter {
my ($scalar, $ddp) = @_;
Data::Printer::Common::_warn($ddp, 'json cannot express vstrings. Cast to string');
my $ret = Data::Printer::Filter::VSTRING::parse(@_);
return '"' . $ret . '"';
}
sub _json_scalar_filter {
my ($scalar, $ddp) = @_;
return $ddp->maybe_colorize('null', 'undef') if !defined $$scalar;
return Data::Printer::Filter::SCALAR::parse(@_);
}
sub _json_code_filter {
my (undef, $ddp) = @_;
Data::Printer::Common::_warn($ddp, 'json cannot express subroutines. Cast to string');
my $res = Data::Printer::Filter::CODE::parse(@_);
return '"' . $res . '"';
}
1;
__END__
=head1 NAME
Data::Printer::Profile::JSON - dump variables in JSON format
=head1 SYNOPSIS
While loading Data::Printer:
use DDP profile => 'JSON';
While asking for a print:
p $var, profile => 'JSON';
or in your C<.dataprinter> file:
profile = JSON
=head1 DESCRIPTION
This profile outputs your variables in JSON format. It's not nearly as efficient
as a regular JSON module, but it may be useful, specially if you're changing
the format directly in your .dataprinter.
=head1 CAVEATS
JSON is a super simple format that allows scalar, hashes and arrays. It doesn't
support many types that could be present on Perl data structures, such as
functions, globs and circular references. When printing those types, whenever
possible, this module will stringify the result.
Objects are also not shown, but their internal data structure is exposed.
This module also attempts to render Regular expressions as plain JS regexes.
While not directly supported in JSON, it should be parseable.
=head1 SEE ALSO
L<Data::Printer>
L<JSON::MaybeXS>>
|