Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ap/json-tohtml
render JSON-based Perl datastructures as HTML tables
https://github.com/ap/json-tohtml
json json-renderer perl
Last synced: about 2 months ago
JSON representation
render JSON-based Perl datastructures as HTML tables
- Host: GitHub
- URL: https://github.com/ap/json-tohtml
- Owner: ap
- Created: 2024-04-09T04:21:12.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-04-09T05:03:36.000Z (9 months ago)
- Last Synced: 2024-10-13T11:22:41.339Z (3 months ago)
- Topics: json, json-renderer, perl
- Language: Perl
- Homepage: https://metacpan.org/release/JSON-ToHTML
- Size: 3.91 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.pod
- Changelog: Changes
Awesome Lists containing this project
README
use 5.006; use strict; use warnings;
package JSON::ToHTML;
our $VERSION = '0.001';
use Scalar::Util ();
sub json_object_to_html;
sub json_array_to_html;sub json_values_to_html {
my $copy;
map +(
( not defined $_ ) ? 'null'
: 'HASH' eq ref $_ ? json_object_to_html $_
: 'ARRAY' eq ref $_ ? json_array_to_html $_
: eval { $$_ eq 1 or $$_ eq 0 } ? ( $$_ ? 'true' : 'false' )
: Scalar::Util::looks_like_number $_ ? qq'$_'
: grep s!([<>"'&@\x{80}-\x{10FFFF}])!''.(ord $1).';'!ge || 1, $copy = $_
), @_
}sub json_identical_keys {
return if grep 'HASH' ne ref, @_;
my $keyset = join ':', map length . $_, my @sk = sort keys %{ shift @_ };
$keyset eq ( join ':', map length . $_, sort keys %$_ ) or return for @_;
@sk;
}sub json_object_to_html {
my ( $o ) = @_;my @sk = sort keys %$o;
return 'empty object' unless @sk;my @ik = @sk > 1 ? json_identical_keys values %$o : ();
return
( 'key'
. ( join '', json_values_to_html @ik )
. ''
. ( join '', map join( '', json_values_to_html $_, @{ $o->{$_} }{ @ik } ), @sk )
. ''
) if @ik;my @k = json_values_to_html @sk;
my @v = json_values_to_html @$o{ @sk };
join '', '', ( map "$k[$_]$v[$_]", 0 .. $#k ), ''
}sub json_array_to_html {
my ( $a ) = @_;return 'empty array' unless @$a;
my @ik = json_identical_keys @$a;
return
( '#'
. ( join '', json_values_to_html @ik )
. ''
. ( join '', map join( '', "$_", json_values_to_html @{ $a->[$_] }{ @ik } ), 0 .. $#$a )
. ''
) if @ik;my $i;
join '', (
'',
( map ''.$i++.qq'$_', json_values_to_html @$a ),
'',
);
}1;
__END__
=pod
=head1 NAME
JSON::ToHTML - render JSON-based Perl datastructures as HTML tables
=head1 DESCRIPTION
This module will provide functions to render JSON-based Perl datastructures
as HTML tables which do not look like code.
The focus is on the display of regular tabular datastructures such as database
resultsets to non- (or not particularly) technical users.=cut