JSON::Color - Encode to colored JSON
This document describes version 0.134 of JSON::Color (from Perl distribution JSON-Color), released on 2023-07-02.
use JSON::Color qw(encode_json); say encode_json([1, "two", {three => 4}]); # specify some options say encode_json([1, "two", {three => 4}], { color_theme=>"bright256", # will be searched under ColorTheme::JSON::Color:: or ColorTheme:: }); # requires ColorTheme::Lens::Lighten say encode_json([1, "two", {three => 4}], { color_theme=>["Lens::Lighten" =>{theme=>"JSON::Color::bright256"}], });
This module generates JSON, colorized with ANSI escape sequences.
To change the color, see the %theme in the source code. In theory you can also modify it to colorize using HTML.
%theme
Encode to JSON. Will die on error (e.g. when encountering non-encodeable data like Regexp or file handle).
Known options:
color_theme => STR
Pick a color theme, which is a ColorTheme color theme module, under the following namespaces: ColorTheme::JSON::Color::, ColorTheme::. The default is default_ansi, which is the module ColorTheme::JSON::Color::default_ansi. See the module source code for an example of writing your own color theme module.
ColorTheme::JSON::Color::
ColorTheme::
default_ansi
pretty => BOOL (default: 0)
Pretty-print.
linum => BOOL (default: 0)
Show line number.
sort_by => CODE
If specified, then sorting of hash keys will be done using this sort subroutine. This is similar to the sort_by option in the JSON module. Note that code is executed in JSON::Color namespace, example:
sort_by
JSON::Color
# reverse sort encode_json(..., {sort_by => sub { $JSON::Color::b cmp $JSON::Color::a }});
Another example, using Sort::ByExample:
use Sort::ByExample cmp => {-as => 'by_eg', example => [qw/foo bar baz/]}; encode_json(..., {sort_by => sub { by_eg($JSON::Color::a, $JSON::Color::b) }});
Use JSON.
Use Data::Clean::JSON.
Example:
% perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])' ["1","1"]
To detect whether a scalar is a number (e.g. differentiate between "1" and 1), the XS module Scalar::Util::LooksLikeNumber is used. This is set as an optional prerequisite, so you'll need to install it separately. After the prerequisite is installed:
% perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])' [1,"1"]
If defined, will set default color theme to ColorTheme::NoColor. See https://no-color.org/ for more details.
Set default color theme. Has precedence over "COLOR_THEME".
Set default color theme.
Please visit the project's homepage at https://metacpan.org/release/JSON-Color.
Source repository is at https://github.com/perlancar/perl-JSON-Color.
To colorize with HTML, you can try Syntax::Highlight::JSON.
Syntax::SourceHighlight can also colorize JSON/JavaScript to HTML or ANSI escape. It requires the GNU Source-highlight library.
perlancar <perlancar@cpan.org>
Steven Haryanto <stevenharyanto@gmail.com>
To contribute, you can send patches by email/via RT, or send pull requests on GitHub.
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me.
This software is copyright (c) 2023, 2021, 2016, 2015, 2014, 2012 by perlancar <perlancar@cpan.org>.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-Color
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
To install JSON::Color, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::Color
CPAN shell
perl -MCPAN -e shell install JSON::Color
For more information on module installation, please visit the detailed CPAN module installation guide.