JSON::Color(3)

πŸ“¦
version 0.134 Β· 3 versions
, in fedora 43

πŸ’– JSON::Color(3) β€” the manual page, but make it a group chat πŸ’–

okay bestie, so this is JSON::Color(3), a Perl module, and it's literally the cutest thing: it makes your JSON pretty and colorful. ANSI escape sequences, but make it fashion. πŸŽ€


✨ VERSION ✨

This document describes version 0.134 of JSON::Color (from the Perl distribution JSON-Color), released on 2023-07-02. yes she's been around a minute, she's got history, she's iconic. πŸ‘‘


🌸 SYNOPSIS 🌸

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"}],
                });

that's it. that's the whole vibe. three lines of setup and you're glowing. ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

This module generates JSON, colorized with ANSI escape sequences. like, it takes your boring little data structure and gives it main character energy.

want to change the color? check out the %theme in the source code, bestie. and here's the fun part β€” in theory you can also modify it to colorize using HTML. not saying you should, but the option is there, and that's lowkey amazing. πŸ’…


πŸ’… FUNCTIONS πŸ’…

πŸ’– encode_json($data, \%opts) => STR

Encode to JSON. and ngl, she's dramatic β€” this will die on error (e.g. when encountering non-encodeable data like Regexp or a file handle). she does not do ambiguity, she does not do "maybe", she just dies. periodt. πŸ’€

Known options:

  • color_theme => STR β€” pick a color theme, which is a ColorTheme color theme module, found under these namespaces: ColorTheme::JSON::Color::, ColorTheme::. the default is default_ansi, which is the module ColorTheme::JSON::Color::default_ansi. peep the module source code for an example of writing your own color theme module, bc honestly you could. you could. πŸ‘‘
  • pretty => BOOL (default: 0) β€” pretty-print. yes. make it readable. treat yourself. 🌸
  • linum => BOOL (default: 0) β€” show line number. for when you need to point at a specific line and be like "this one. this is the one." πŸ™ˆ
  • 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 the code is executed in the JSON::Color namespace, example:
# 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) }});

yes you get $JSON::Color::a and $JSON::Color::b instead of the usual $a and $b. it's giving ✨custom namespace✨. iykyk.


🫢 FAQ 🫢

πŸ’œ What about loading?

Use JSON. short answer, sweet answer, we move on. πŸŽ€

πŸ’œ How to handle non-encodeable data?

Use Data::Clean::JSON. it handles the mess so you don't have to. iconic behavior. ✨

πŸ’œ Why do numbers become strings?

example:

% perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])'
["1","1"]

😭 yeah. both of them got stringified. 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"]

look at that. 1 is a number. "1" is a string. the way that works now. obsessed. πŸ”₯


🌈 ENVIRONMENT 🌈

🌸 NO_COLOR

If defined, will set default color theme to ColorTheme::NoColor. see <https://no-color.org/> for more details. respectfully, choosing no color is a valid choice and we support you. 🫢

🌸 JSON_COLOR_COLOR_THEME

Set default color theme. Has precedence over COLOR_THEME. she's the bossy older sister of the two. πŸ‘‘

🌸 COLOR_THEME

Set default color theme. the chill one. πŸ’…


πŸ’– HOMEPAGE πŸ’–

Please visit the project's homepage at <https://metacpan.org/release/JSON-Color>. go say hi, she loves visitors. 🌸


πŸŽ€ SOURCE πŸŽ€

Source repository is at <https://github.com/perlancar/perl-JSON-Color>. it's giving open source. it's giving community. we love to see it. πŸ¦‹


⭐ SEE ALSO ⭐

To colorize with HTML, you can try Syntax::Highlight::JSON. bestie that's literally the whole plot twist.

Syntax::SourceHighlight can also colorize JSON/JavaScript to HTML or ANSI escape. it requires the GNU Source-highlight library. yes, an extra dependency. worth it tho. ✨


πŸ‘‘ AUTHOR πŸ‘‘

perlancar <perlancar@cpan.org>


🌟 CONTRIBUTOR 🌟

Steven Haryanto <stevenharyanto@gmail.com>


πŸ’œ CONTRIBUTING πŸ’œ

to contribute, you can send patches by email/via RT, or send pull requests on GitHub. pick your lane bestie, both are valid. 🫢

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. so if you had to do extra steps, that's not a you problem, that's a bug. periodt. πŸ’…


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. free software, free as in freedom, free as in slay. πŸ’–


πŸ’€ BUGS πŸ’€

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. help me help you, bestie. the more info you give, the faster it gets fixed. 🫢


okay so that's the whole page!! this doc ships with perl v5.42.0 and the page itself is dated 2025-07-25, and it's JSON::Color(3) β€” go forth and make your JSON glow βœ¨πŸ’…πŸ¦‹

πŸ“¦ where this page comes from