Skip to content

Commit

Permalink
Issue #399: base on Cpanel::JSON::XS
Browse files Browse the repository at this point in the history
remove workarounds for JSON::XS.
  • Loading branch information
bschmalhofer committed Jun 9, 2023
1 parent 961dbaf commit a5e3233
Showing 1 changed file with 18 additions and 73 deletions.
91 changes: 18 additions & 73 deletions Kernel/System/JSON.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@

package Kernel::System::JSON;

use v5.24;
use strict;
use warnings;
use namespace::autoclean;
use utf8;

use JSON;
# core modules

# CPAN modules
use Cpanel::JSON::XS;

# OTOBO modules

our @ObjectDependencies = (
'Kernel::System::Log',
Expand Down Expand Up @@ -47,10 +55,7 @@ sub new {
my ( $Type, %Param ) = @_;

# allocate new hash for object
my $Self = {};
bless( $Self, $Type );

return $Self;
return bless {}, $Type;
}

=head2 Encode()
Expand All @@ -75,13 +80,12 @@ sub Encode {
Priority => 'error',
Message => 'Need Data!',
);

return;
}

# create a JSON object
# The backend JSON::XS the default backend when it is available.
# As a fallback the backend JSON::PP is used.
my $JSONObject = JSON->new();
# create a JSON::XS compatible object
my $JSONObject = Cpanel::JSON::XS->new;

# grudgingly accept data that is neither a hash- nor an array reference
$JSONObject->allow_nonref(1);
Expand Down Expand Up @@ -152,9 +156,10 @@ sub Decode {
# check for needed data
return unless defined $Param{Data};

# create json object
my $JSONObject = JSON->new();
# create a JSON::XS compatible object
my $JSONObject = Cpanel::JSON::XS->new;

# grudgingly accept data that is neither a hash- nor an array reference
$JSONObject->allow_nonref(1);

# decode JSON encoded to perl structure
Expand All @@ -171,11 +176,6 @@ sub Decode {
return;
}

# sanitize leftover boolean objects
$Scalar = $Self->_BooleansProcess(
JSON => $Scalar,
);

return $Scalar;
}

Expand All @@ -197,10 +197,7 @@ as a JavaScript string instead.
=cut

sub True {

# Use constant instead of JSON::false() as this can cause nasty problems with JSON::XS on some platforms.
# (encountered object '1', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled)
return \1;
return Cpanel::JSON::XS::true;
}

=head2 False()
Expand All @@ -210,59 +207,7 @@ like C<True()>, but for a false boolean value.
=cut

sub False {

# Use constant instead of JSON::false() as this can cause nasty problems with JSON::XS on some platforms.
# (encountered object '0', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled)
return \0;
return Cpanel::JSON::XS::false;
}

=begin Internal:
=cut

=head2 _BooleansProcess()
decode boolean values leftover from JSON decoder to simple scalar values
my $ProcessedJSON = $JSONObject->_BooleansProcess(
JSON => $JSONData,
);
=cut

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

# convert scalars if needed
if ( JSON::is_bool( $Param{JSON} ) ) {
$Param{JSON} = ( $Param{JSON} ? 1 : 0 );
}

# recurse into arrays
elsif ( ref $Param{JSON} eq 'ARRAY' ) {

for my $Value ( @{ $Param{JSON} } ) {
$Value = $Self->_BooleansProcess(
JSON => $Value,
);
}
}

# recurse into hashes
elsif ( ref $Param{JSON} eq 'HASH' ) {

for my $Value ( values %{ $Param{JSON} } ) {
$Value = $Self->_BooleansProcess(
JSON => $Value,
);
}
}

return $Param{JSON};
}

=end Internal:
=cut

1;

0 comments on commit a5e3233

Please sign in to comment.