From 2bd85089f42a12a47c3f9bea01d6fa35e10480e4 Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Sat, 26 Feb 2022 11:56:29 -0500 Subject: [PATCH] Put integers in quotes as well. Proper JSON has all values in quotes. This is important because integers like 0123 are invalid --- scripts/ZoneMinder/lib/ZoneMinder/General.pm | 39 ++++++++++---------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/scripts/ZoneMinder/lib/ZoneMinder/General.pm b/scripts/ZoneMinder/lib/ZoneMinder/General.pm index 90e7399ce..3d1dd0e49 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/General.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/General.pm @@ -432,13 +432,13 @@ our $testedJSON = 0; our $hasJSONAny = 0; sub _testJSON { - return if ( $testedJSON ); + return if $testedJSON; my $result = eval { require JSON::MaybeXS; JSON::MaybeXS->import(); }; $testedJSON = 1; - $hasJSONAny = 1 if ( $result ); + $hasJSONAny = 1 if $result; } sub _getJSONType { @@ -451,40 +451,41 @@ sub _getJSONType { return 'string'; } -sub jsonEncode; - sub jsonEncode { my $value = shift; _testJSON(); - if ( $hasJSONAny ) { - my $string = eval { JSON::MaybeXS->encode_json( $value ) }; - Fatal( "Unable to encode object to JSON: $@" ) unless( $string ); - return( $string ); + if ($hasJSONAny) { + my $string = eval { + JSON::MaybeXS->encode_json($value); + }; + Error('Unable to encode object to JSON: '.$@) unless $string; + return $string; } my $type = _getJSONType($value); - if ( $type eq 'integer' || $type eq 'double' ) { - return( $value ); - } elsif ( $type eq 'boolean' ) { - return( $value?'true':'false' ); + if ($type eq 'integer' || $type eq 'double') { + return '"'.$value.'"'; + } elsif ($type eq 'boolean') { + return $value ? 'true' : 'false'; } elsif ( $type eq 'string' ) { $value =~ s|(["\\/])|\\$1|g; $value =~ s|\r?\n|\n|g; - return( '"'.$value.'"' ); + return '"'.$value.'"'; } elsif ( $type eq 'null' ) { - return( 'null' ); + return 'null'; } elsif ( $type eq 'array' ) { - return( '['.join( ',', map { jsonEncode( $_ ) } @$value ).']' ); + return '['.join( ',', map { jsonEncode( $_ ) } @$value ).']'; } elsif ( $type eq 'hash' ) { my $result = '{'; - while ( my ( $subKey=>$subValue ) = each( %$value ) ) { + while ( my ( $subKey=>$subValue ) = each(%$value) ) { $result .= ',' if ( $result ne '{' ); - $result .= '"'.$subKey.'":'.jsonEncode( $subValue ); + $result .= '"'.$subKey.'":'.jsonEncode($subValue); } - return( $result.'}' ); + return $result.'}'; } else { - Fatal( "Unexpected type '$type'" ); + Error("Unexpected type '$type'"); + return ''; } }