1JSON::Color(3) User Contributed Perl Documentation JSON::Color(3)
2
3
4
6 JSON::Color - Encode to colored JSON
7
9 This document describes version 0.134 of JSON::Color (from Perl
10 distribution JSON-Color), released on 2023-07-02.
11
13 use JSON::Color qw(encode_json);
14 say encode_json([1, "two", {three => 4}]);
15
16 # specify some options
17 say encode_json([1, "two", {three => 4}],
18 {
19 color_theme=>"bright256", # will be searched under ColorTheme::JSON::Color:: or ColorTheme::
20 });
21
22 # requires ColorTheme::Lens::Lighten
23 say encode_json([1, "two", {three => 4}],
24 {
25 color_theme=>["Lens::Lighten" =>{theme=>"JSON::Color::bright256"}],
26 });
27
29 This module generates JSON, colorized with ANSI escape sequences.
30
31 To change the color, see the %theme in the source code. In theory you
32 can also modify it to colorize using HTML.
33
35 encode_json($data, \%opts) => STR
36 Encode to JSON. Will die on error (e.g. when encountering non-
37 encodeable data like Regexp or file handle).
38
39 Known options:
40
41 • color_theme => STR
42
43 Pick a color theme, which is a ColorTheme color theme module, under
44 the following namespaces: "ColorTheme::JSON::Color::",
45 "ColorTheme::". The default is "default_ansi", which is the module
46 ColorTheme::JSON::Color::default_ansi. See the module source code
47 for an example of writing your own color theme module.
48
49 • pretty => BOOL (default: 0)
50
51 Pretty-print.
52
53 • linum => BOOL (default: 0)
54
55 Show line number.
56
57 • sort_by => CODE
58
59 If specified, then sorting of hash keys will be done using this
60 sort subroutine. This is similar to the "sort_by" option in the
61 JSON module. Note that code is executed in "JSON::Color" namespace,
62 example:
63
64 # reverse sort
65 encode_json(..., {sort_by => sub { $JSON::Color::b cmp $JSON::Color::a }});
66
67 Another example, using Sort::ByExample:
68
69 use Sort::ByExample cmp => {-as => 'by_eg', example => [qw/foo bar baz/]};
70 encode_json(..., {sort_by => sub { by_eg($JSON::Color::a, $JSON::Color::b) }});
71
73 What about loading?
74 Use JSON.
75
76 How to handle non-encodeable data?
77 Use Data::Clean::JSON.
78
79 Why do numbers become strings?
80 Example:
81
82 % perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])'
83 ["1","1"]
84
85 To detect whether a scalar is a number (e.g. differentiate between "1"
86 and 1), the XS module Scalar::Util::LooksLikeNumber is used. This is
87 set as an optional prerequisite, so you'll need to install it
88 separately. After the prerequisite is installed:
89
90 % perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])'
91 [1,"1"]
92
94 NO_COLOR
95 If defined, will set default color theme to ColorTheme::NoColor. See
96 <https://no-color.org/> for more details.
97
98 JSON_COLOR_COLOR_THEME
99 Set default color theme. Has precedence over "COLOR_THEME".
100
101 COLOR_THEME
102 Set default color theme.
103
105 Please visit the project's homepage at
106 <https://metacpan.org/release/JSON-Color>.
107
109 Source repository is at <https://github.com/perlancar/perl-JSON-Color>.
110
112 To colorize with HTML, you can try Syntax::Highlight::JSON.
113
114 Syntax::SourceHighlight can also colorize JSON/JavaScript to HTML or
115 ANSI escape. It requires the GNU Source-highlight library.
116
118 perlancar <perlancar@cpan.org>
119
121 Steven Haryanto <stevenharyanto@gmail.com>
122
124 To contribute, you can send patches by email/via RT, or send pull
125 requests on GitHub.
126
127 Most of the time, you don't need to build the distribution yourself.
128 You can simply modify the code, then test via:
129
130 % prove -l
131
132 If you want to build the distribution (e.g. to try to install it
133 locally on your system), you can install Dist::Zilla,
134 Dist::Zilla::PluginBundle::Author::PERLANCAR,
135 Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two
136 other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps
137 required beyond that are considered a bug and can be reported to me.
138
140 This software is copyright (c) 2023, 2021, 2016, 2015, 2014, 2012 by
141 perlancar <perlancar@cpan.org>.
142
143 This is free software; you can redistribute it and/or modify it under
144 the same terms as the Perl 5 programming language system itself.
145
147 Please report any bugs or feature requests on the bugtracker website
148 <https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-Color>
149
150 When submitting a bug or request, please include a test-file or a patch
151 to an existing test-file that illustrates the bug or desired feature.
152
153
154
155perl v5.36.1 2023-07-14 JSON::Color(3)