<commit_before># --
# Kernel/Output/HTML/LayoutAJAX.pm - provides generic HTML output
# Copyright (C) 2001-2009 OTRS AG, http://otrs.org/
# --
# $Id: LayoutAJAX.pm,v 1.23 2009-12-02 08:44:09 mn Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::Output::HTML::LayoutAJAX;

use strict;
use warnings;

use vars qw(@ISA $VERSION);
$VERSION = qw($Revision: 1.23 $) [1];

=item JSON()

build a JSON output based on perl data

    my $JSON = $LayoutObject->JSON(
        Data => $DataRef,
    );

=cut

sub JSON {
    my ( $Self, %Param ) = @_;

    my $JSON = '';

    # check needed stuff
    for (qw(Data)) {
        if ( !defined $Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
            return;
        }
    }

    # array
    if ( ref $Param{Data} eq 'ARRAY' ) {
        $JSON .= '[';
        for my $Key ( @{ $Param{Data} } ) {
            if ( ref $Key ) {
                $JSON .= $Self->JSON( Data => $Key ) . ',';
            }
            else {
                if ( $Key =~ /^\d+$/ ) {
                    $JSON .= $Self->JSONQuote( Data => $Key ) . ',';
                }
                else {
                    $JSON .= '"' . $Self->JSONQuote( Data => $Key ) . '",';
                }
            }
        }

        # delete comma at end of string
        $JSON =~ s{ , \z }{}xms;
        $JSON .= ']';
    }

    # hash
    elsif ( ref $Param{Data} eq 'HASH' ) {
        $JSON .= '{';
        for my $Key ( sort keys %{ $Param{Data} } ) {
            $JSON .= '"' . $Key . '":';
            if ( ref $Param{Data}->{$Key} ) {
                $JSON .= $Self->JSON( Data => $Param{Data}->{$Key} ) . ',';
            }
            else {
                if ( $Param{Data}->{$Key} =~ /^\d+$/ ) {
                    $JSON .= $Self->JSONQuote( Data => $Param{Data}->{$Key} ) . ',';
                }
                else {
                    $JSON .= '"' . $Self->JSONQuote( Data => $Param{Data}->{$Key} ) . '",';
                }
            }
        }

        # delete comma at end of string
        $JSON =~ s{ , \z }{}xms;
        $JSON .= '}';
    }

    # string
    else {
        if ( $Param{Data} =~ /^\d+$/ ) {
            $JSON .= $Self->JSONQuote( Data => $Param{Data} ) . ',';
        }
        else {
            $JSON .= '"' . $Self->JSONQuote( Data => $Param{Data} ) . '",';
        }
    }<commit_msg>Fixed bug with wrong comma at the end of strings.<commit_after># --
# Kernel/Output/HTML/LayoutAJAX.pm - provides generic HTML output
# Copyright (C) 2001-2009 OTRS AG, http://otrs.org/
# --
# $Id: LayoutAJAX.pm,v 1.24 2009-12-11 17:45:51 mn Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::Output::HTML::LayoutAJAX;

use strict;
use warnings;

use vars qw(@ISA $VERSION);
$VERSION = qw($Revision: 1.24 $) [1];

=item JSON()

build a JSON output based on perl data

    my $JSON = $LayoutObject->JSON(
        Data => $DataRef,
    );

=cut

sub JSON {
    my ( $Self, %Param ) = @_;

    my $JSON = '';

    # check needed stuff
    for (qw(Data)) {
        if ( !defined $Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
            return;
        }
    }

    # array
    if ( ref $Param{Data} eq 'ARRAY' ) {
        $JSON .= '[';
        for my $Key ( @{ $Param{Data} } ) {
            if ( ref $Key ) {
                $JSON .= $Self->JSON( Data => $Key ) . ',';
            }
            else {
                if ( $Key =~ /^\d+$/ ) {
                    $JSON .= $Self->JSONQuote( Data => $Key ) . ',';
                }
                else {
                    $JSON .= '"' . $Self->JSONQuote( Data => $Key ) . '",';
                }
            }
        }

        # delete comma at end of string
        $JSON =~ s{ , \z }{}xms;
        $JSON .= ']';
    }

    # hash
    elsif ( ref $Param{Data} eq 'HASH' ) {
        $JSON .= '{';
        for my $Key ( sort keys %{ $Param{Data} } ) {
            $JSON .= '"' . $Key . '":';
            if ( ref $Param{Data}->{$Key} ) {
                $JSON .= $Self->JSON( Data => $Param{Data}->{$Key} ) . ',';
            }
            else {
                if ( $Param{Data}->{$Key} =~ /^\d+$/ ) {
                    $JSON .= $Self->JSONQuote( Data => $Param{Data}->{$Key} ) . ',';
                }
                else {
                    $JSON .= '"' . $Self->JSONQuote( Data => $Param{Data}->{$Key} ) . '",';
                }
            }
        }

        # delete comma at end of string
        $JSON =~ s{ , \z }{}xms;
        $JSON .= '}';
    }

    # string
    else {
        if ( $Param{Data} =~ /^\d+$/ ) {
            $JSON .= $Self->JSONQuote( Data => $Param{Data} );
        }
        else {
            $JSON .= '"' . $Self->JSONQuote( Data => $Param{Data} ) . '"';
        }
    }
{'fraction_non_alphanumeric': 0.19454650937923032, 'fraction_numerical': 0.011216399149100754, 'mean_word_length': 1.403345724907063, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4205610', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1697, 'n_words': 407}