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.130 of JSON::Color (from Perl
10 distribution JSON-Color), released on 2020-06-09.
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 JSON_COLOR_COLOR_THEME
81 Set default color theme. Has precedence over "COLOR_THEME".
82
83 COLOR_THEME
84 Set default color theme.
85
87 Please visit the project's homepage at
88 <https://metacpan.org/release/JSON-Color>.
89
91 Source repository is at <https://github.com/perlancar/perl-JSON-Color>.
92
94 Please report any bugs or feature requests on the bugtracker website
95 <https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-Color>
96
97 When submitting a bug or request, please include a test-file or a patch
98 to an existing test-file that illustrates the bug or desired feature.
99
101 To colorize with HTML, you can try Syntax::Highlight::JSON.
102
103 Syntax::SourceHighlight can also colorize JSON/JavaScript to HTML or
104 ANSI escape. It requires the GNU Source-highlight library.
105
107 perlancar <perlancar@cpan.org>
108
110 This software is copyright (c) 2020, 2016, 2015, 2014, 2012 by
111 perlancar@cpan.org.
112
113 This is free software; you can redistribute it and/or modify it under
114 the same terms as the Perl 5 programming language system itself.
115
116
117
118perl v5.32.0 2020-07-28 JSON::Color(3)