Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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