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.12 of JSON::Color (from Perl
10 distribution JSON-Color), released on 2016-08-23.
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 · pretty => BOOL (default: 0)
30
31 Pretty-print.
32
33 · linum => BOOL (default: 0)
34
35 Show line number.
36
37 · sort_by => CODE
38
39 If specified, then sorting of hash keys will be done using this
40 sort subroutine. This is similar to the "sort_by" option in the
41 JSON module. Note that code is executed in "JSON::Color" namespace,
42 example:
43
44 # reverse sort
45 encode_json(..., {sort_by => sub { $JSON::Color::b cmp $JSON::Color::a }});
46
47 Another example, using Sort::ByExample:
48
49 use Sort::ByExample cmp => {-as => 'by_eg', example => [qw/foo bar baz/]};
50 encode_json(..., {sort_by => sub { by_eg($JSON::Color::a, $JSON::Color::b) }});
51
53 What about loading?
54 Use JSON.
55
56 How to handle non-encodeable data?
57 Use Data::Clean::JSON.
58
59 Why do numbers become strings?
60 Example:
61
62 % perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])'
63 ["1","1"]
64
65 To detect whether a scalar is a number (e.g. differentiate between "1"
66 and 1), the XS module Scalar::Util::LooksLikeNumber is used. This is
67 set as an optional prerequisite, so you'll need to install it
68 separately. After the prerequisite is installed:
69
70 % perl -MJSON::Color=encode_json -E'say encode_json([1, "1"])'
71 [1,"1"]
72
74 Please visit the project's homepage at
75 <https://metacpan.org/release/JSON-Color>.
76
78 Source repository is at <https://github.com/perlancar/perl-JSON-Color>.
79
81 Please report any bugs or feature requests on the bugtracker website
82 <https://rt.cpan.org/Public/Dist/Display.html?Name=JSON-Color>
83
84 When submitting a bug or request, please include a test-file or a patch
85 to an existing test-file that illustrates the bug or desired feature.
86
88 To colorize with HTML, you can try Syntax::Highlight::JSON.
89
90 Syntax::SourceHighlight can also colorize JSON/JavaScript to HTML or
91 ANSI escape. It requires the GNU Source-highlight library.
92
94 perlancar <perlancar@cpan.org>
95
97 This software is copyright (c) 2016 by perlancar@cpan.org.
98
99 This is free software; you can redistribute it and/or modify it under
100 the same terms as the Perl 5 programming language system itself.
101
102
103
104perl v5.30.0 2019-07-26 JSON::Color(3)