Put integers in quotes as well. Proper JSON has all values in quotes. This is important because integers like 0123 are invalid
This commit is contained in:
parent
6fc5803e16
commit
2bd85089f4
|
@ -432,13 +432,13 @@ our $testedJSON = 0;
|
||||||
our $hasJSONAny = 0;
|
our $hasJSONAny = 0;
|
||||||
|
|
||||||
sub _testJSON {
|
sub _testJSON {
|
||||||
return if ( $testedJSON );
|
return if $testedJSON;
|
||||||
my $result = eval {
|
my $result = eval {
|
||||||
require JSON::MaybeXS;
|
require JSON::MaybeXS;
|
||||||
JSON::MaybeXS->import();
|
JSON::MaybeXS->import();
|
||||||
};
|
};
|
||||||
$testedJSON = 1;
|
$testedJSON = 1;
|
||||||
$hasJSONAny = 1 if ( $result );
|
$hasJSONAny = 1 if $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
sub _getJSONType {
|
sub _getJSONType {
|
||||||
|
@ -451,40 +451,41 @@ sub _getJSONType {
|
||||||
return 'string';
|
return 'string';
|
||||||
}
|
}
|
||||||
|
|
||||||
sub jsonEncode;
|
|
||||||
|
|
||||||
sub jsonEncode {
|
sub jsonEncode {
|
||||||
my $value = shift;
|
my $value = shift;
|
||||||
|
|
||||||
_testJSON();
|
_testJSON();
|
||||||
if ( $hasJSONAny ) {
|
if ($hasJSONAny) {
|
||||||
my $string = eval { JSON::MaybeXS->encode_json( $value ) };
|
my $string = eval {
|
||||||
Fatal( "Unable to encode object to JSON: $@" ) unless( $string );
|
JSON::MaybeXS->encode_json($value);
|
||||||
return( $string );
|
};
|
||||||
|
Error('Unable to encode object to JSON: '.$@) unless $string;
|
||||||
|
return $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
my $type = _getJSONType($value);
|
my $type = _getJSONType($value);
|
||||||
if ( $type eq 'integer' || $type eq 'double' ) {
|
if ($type eq 'integer' || $type eq 'double') {
|
||||||
return( $value );
|
return '"'.$value.'"';
|
||||||
} elsif ( $type eq 'boolean' ) {
|
} elsif ($type eq 'boolean') {
|
||||||
return( $value?'true':'false' );
|
return $value ? 'true' : 'false';
|
||||||
} elsif ( $type eq 'string' ) {
|
} elsif ( $type eq 'string' ) {
|
||||||
$value =~ s|(["\\/])|\\$1|g;
|
$value =~ s|(["\\/])|\\$1|g;
|
||||||
$value =~ s|\r?\n|\n|g;
|
$value =~ s|\r?\n|\n|g;
|
||||||
return( '"'.$value.'"' );
|
return '"'.$value.'"';
|
||||||
} elsif ( $type eq 'null' ) {
|
} elsif ( $type eq 'null' ) {
|
||||||
return( 'null' );
|
return 'null';
|
||||||
} elsif ( $type eq 'array' ) {
|
} elsif ( $type eq 'array' ) {
|
||||||
return( '['.join( ',', map { jsonEncode( $_ ) } @$value ).']' );
|
return '['.join( ',', map { jsonEncode( $_ ) } @$value ).']';
|
||||||
} elsif ( $type eq 'hash' ) {
|
} elsif ( $type eq 'hash' ) {
|
||||||
my $result = '{';
|
my $result = '{';
|
||||||
while ( my ( $subKey=>$subValue ) = each( %$value ) ) {
|
while ( my ( $subKey=>$subValue ) = each(%$value) ) {
|
||||||
$result .= ',' if ( $result ne '{' );
|
$result .= ',' if ( $result ne '{' );
|
||||||
$result .= '"'.$subKey.'":'.jsonEncode( $subValue );
|
$result .= '"'.$subKey.'":'.jsonEncode($subValue);
|
||||||
}
|
}
|
||||||
return( $result.'}' );
|
return $result.'}';
|
||||||
} else {
|
} else {
|
||||||
Fatal( "Unexpected type '$type'" );
|
Error("Unexpected type '$type'");
|
||||||
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue