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