blib/lib/Test/CPAN/Health/Reporter/JSON.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Test/CPAN/Health/Reporter/HTML.pm ↑ Summary blib/lib/Test/CPAN/Health/Reporter/Markdown.pm »
1 package Test::CPAN::Health::Reporter::JSON;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 use Carp qw(croak);
8 use JSON::MaybeXS qw(JSON);
9 use Params::Validate::Strict qw(validate_strict);
10 use Scalar::Util qw(blessed);
11  
12 our $VERSION = '0.1.0';
13  
14 =head1 NAME
15  
16 Test::CPAN::Health::Reporter::JSON - Render a health report as a JSON document
17  
18 =head1 SYNOPSIS
19  
20 use Test::CPAN::Health::Reporter::JSON;
21  
22 my $reporter = Test::CPAN::Health::Reporter::JSON->new(pretty => 1);
23 print $reporter->render($report);
24  
25 =head1 DESCRIPTION
26  
27 Serialises a L<Test::CPAN::Health::Report> to a JSON document via
28 L<JSON::MaybeXS>. Suitable for piping to other tools, CI artifact storage,
29 or consuming from scripts.
30  
31 The emitted structure mirrors C<< $report->as_hash >>.
32  
33 =cut
34  
35 sub new {
36 my ($class, %args) = @_;
37  
38 %args = %{ validate_strict(
39 schema => {
40 pretty => { type => 'scalar', optional => 1, default => 0 },
41 canonical => { type => 'scalar', optional => 1, default => 1 },
42 },
43 input => \%args,
44 ) };
45  
46 my $json = JSON()->new->utf8(1)
47 ->canonical($args{canonical})
48 ->pretty($args{pretty});
49  
50 my $self = bless {
51 _json => $json,
52 }, $class;
53  
54 return $self;
55 }
56  
57 =head2 render
58  
59 =head3 PURPOSE
60  
61 Serialise a Report to a JSON string.
62  
63 =head3 API SPECIFICATION
64  
65 =head4 INPUT
66  
67 report Test::CPAN::Health::Report required
68  
69 =head4 OUTPUT
70  
71 Scalar string: valid JSON (UTF-8 encoded).
72  
73 =head3 MESSAGES
74  
75 Code | Severity | Message | Resolution
76 ------+----------+------------------------------------+---------------------
77 JSN01 | FATAL | report must be a Report object | Pass a Report instance
78  
79 =head3 FORMAL SPECIFICATION
80  
81 -- Z schema (placeholder) --
82 RenderOp
83 report : Report
84 json! : String
85 -------------------------------------------------------
86 valid_json(json!)
87 decode_json(json!).overall_score = report.overall_score
88  
89 =head3 SIDE EFFECTS
90  
91 None.
92  
93 =head3 USAGE EXAMPLE
94  
95 my $json_str = $reporter->render($report);
96 write_file('report.json', $json_str);
97  
98 =cut
99  
100 sub render {
101 my ($self, $report) = @_;
102  
103 croak 'report must be a Test::CPAN::Health::Report'
104 unless blessed($report) && $report->isa('Test::CPAN::Health::Report');
105  
106 return $self->{_json}->encode($report->as_hash);
107 }
108  
109 =head1 AUTHOR
110  
111 Nigel Horne, C<< <njh at nigelhorne.com> >>
112  
113 =head1 LICENSE AND COPYRIGHT
114  
115 Copyright (C) 2025-2026 Nigel Horne.
116  
117 This program is free software; you can redistribute it and/or modify it
118 under the terms of the GNU General Public License as published by the Free
119 Software Foundation; either version 2 of the License, or (at your option)
120 any later version.
121  
122 =cut
123  
124 1;
« blib/lib/Test/CPAN/Health/Reporter/HTML.pm ↑ Summary blib/lib/Test/CPAN/Health/Reporter/Markdown.pm »