Fix from_json methods (#1303)

This commit is contained in:
Clément Oudot 2017-09-28 12:52:14 +00:00
parent fc582377ff
commit a7401b72f6
17 changed files with 36 additions and 27 deletions

View File

@ -120,7 +120,7 @@ sub getJson {
my $resp = $self->ua->get( $self->base . $url, @_ );
if ( $resp->is_success ) {
my $res;
eval { $res = from_json( $resp->content ) };
eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
if ($@) {
print STDERR "Unable to decode session: $@\n";
return 0;
@ -177,7 +177,7 @@ sub newSession {
#my $resp = $self->ua->request($req);
#if ( $resp->is_success ) {
# my $res;
# eval { $res = from_json( $resp->content ) };
# eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
# if ( $@ or !$res->{result} ) {
# die "Unable to create session: bad REST response $@";
# }
@ -229,7 +229,7 @@ sub save {
if ( $resp->is_success ) {
my $res;
eval { $res = from_json( $resp->content ) };
eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
if ($@) {
die "Bad REST response: $@";
}

View File

@ -49,7 +49,7 @@ sub load {
}
my $r;
if ( $row->[0] =~ /^\s*\{/s ) {
eval { $r = from_json( $row->[0] ); };
eval { $r = from_json( $row->[0], { allow_nonref => 1 } ); };
}
else { # Old format
require Storable;

View File

@ -50,7 +50,7 @@ sub getJson {
my $resp = $self->ua->get( $self->base . $url, @_ );
if ( $resp->is_success ) {
my $res;
eval { $res = from_json( $resp->content ) };
eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
if ($@) {
$Lemonldap::NG::Common::Conf::msg .= "Request failed: $@\n";
return undef;
@ -109,7 +109,7 @@ sub store {
my $resp = $self->ua->request($req);
if ( $resp->is_success ) {
my $res;
eval { $res = from_json( $resp->content ) };
eval { $res = from_json( $resp->content, { allow_nonref => 1 } ) };
if ($@) {
$Lemonldap::NG::Common::Conf::msg .= "Unknown error: $@";
return undef;

View File

@ -98,7 +98,10 @@ sub unserialize {
unless ( utf8::is_utf8($v) ) {
$v = encode( 'UTF-8', $v );
}
$conf->{$k} = ( $v =~ /./ ? eval { from_json($v) } : {} );
$conf->{$k} =
( $v =~ /./
? eval { from_json( $v, { allow_nonref => 1 } ) }
: {} );
if ($@) {
$Lemonldap::NG::Common::Conf::msg .=
"Unable to decode $k, switching to old format.\n";

View File

@ -9,7 +9,7 @@ our $VERSION = '2.0.0';
sub newNotification {
my ( $self, $jsonString ) = @_;
my $json;
eval { $json = from_json($jsonString) };
eval { $json = from_json( $jsonString, { allow_nonref => 1 } ) };
if ( my $err = $@ ) {
eval { $self->logger->error("Unable to decode JSON file: $err") };
return 0;
@ -66,7 +66,7 @@ sub deleteNotification {
my $count = 0;
foreach my $ref ( keys %$user ) {
my $json = from_json( $user->{$ref} );
my $json = from_json( $user->{$ref}, { allow_nonref => 1 } );
# Browse notification in file
foreach my $notif (@$json) {

View File

@ -128,7 +128,8 @@ sub jsonResponse {
print STDERR "Result dump :\n" . Data::Dumper::Dumper($res);
die "Manager lib does not return a 200 code, aborting";
}
my $href = from_json( $res->[2]->[0] ) or die 'Response is not JSON';
my $href = from_json( $res->[2]->[0], { allow_nonref => 1 } )
or die 'Response is not JSON';
return $href;
}
@ -141,7 +142,8 @@ sub jsonPostResponse {
print STDERR "Result dump :\n" . Data::Dumper::Dumper($res);
die "Manager lib does not return a 200 code, aborting";
}
my $href = from_json( $res->[2]->[0] ) or die 'Response is not JSON';
my $href = from_json( $res->[2]->[0], { allow_nonref => 1 } )
or die 'Response is not JSON';
return $href;
}
@ -154,7 +156,8 @@ sub jsonPutResponse {
print STDERR "Result dump :\n" . Data::Dumper::Dumper($res);
die "Manager lib does not return a 200 code, aborting";
}
my $href = from_json( $res->[2]->[0] ) or die 'Response is not JSON';
my $href = from_json( $res->[2]->[0], { allow_nonref => 1 } )
or die 'Response is not JSON';
return $href;
}

View File

@ -94,7 +94,7 @@ sub jsonBodyToObj {
$self->error('No data');
return undef;
}
my $j = eval { from_json( $self->content ) };
my $j = eval { from_json( $self->content, { allow_nonref => 1 } ) };
if ($@) {
$self->error("$@$!");
return undef;

View File

@ -38,7 +38,7 @@ sub loadVhostConfig {
$get->header( Host => $vhost );
my $resp = $class->ua->request($get);
if ( $resp->is_success ) {
eval { $json = from_json( $resp->content ) };
eval { $json = from_json( $resp->content, { allow_nonref => 1 } ) };
if ($@) {
$class->logger->error(
"Bad rules.json for $vhost, skipping ($@)");

View File

@ -286,7 +286,7 @@ sub newNotification {
}
else {
eval {
my $tmp = from_json( $json->{xml} );
my $tmp = from_json( $json->{xml}, { allow_nonref => 1 } );
$json->{$_} = $tmp->{$_} foreach ( keys %$tmp );
delete $json->{xml};
};

View File

@ -119,7 +119,7 @@ sub extractFormInfo {
my $json_hash;
eval { $json_hash = from_json $content; };
eval { $json_hash = from_json( $content, { allow_nonref => 1 } ); };
if ($@) {
$self->logger->error("Unable to decode JSON $content");
@ -148,7 +148,9 @@ sub extractFormInfo {
my $people_content = $people_response->decoded_content;
eval { $json_hash = from_json $people_content; };
eval {
$json_hash = from_json( $people_content, { allow_nonref => 1 } );
};
if ($@) {
$self->logger->error("Unable to decode JSON $people_content");
return PE_ERROR;

View File

@ -36,8 +36,8 @@ sub checkForNotifications {
# Transform notifications
my $i = 0; #Files count
foreach my $file ( values %$notifs ) {
my $json = from_json($file);
my $j = 0; #Notifications count
my $json = from_json( $file, { allow_nonref => 1 } );
my $j = 0; #Notifications count
my @res;
$json = [$json] unless ( ref $json eq 'ARRAY' );
LOOP: foreach my $notif ( @{$json} ) {
@ -117,7 +117,7 @@ sub getNotifBack {
foreach my $fileName ( keys %$notifs ) {
my $file = $notifs->{$fileName};
my $fileResult = 1;
my $json = from_json($file);
my $json = from_json( $file, { allow_nonref => 1 } );
$json = [$json] unless ( ref $json eq 'ARRAY' );
# Get pending notifications and verify that they have been accepted

View File

@ -96,7 +96,7 @@ sub getToken {
return undef;
}
$self->cache->remove($id);
return from_json($data);
return from_json( $data, { allow_nonref => 1 } );
}
else {

View File

@ -1269,7 +1269,8 @@ sub getIDTokenSub {
sub getJWTJSONData {
my ( $self, $jwt ) = @_;
my $jwt_parts = $self->extractJWT($jwt);
return from_json( decode_base64url( $jwt_parts->[1] ) );
return from_json(
decode_base64url( $jwt_parts->[1], { allow_nonref => 1 } ) );
}
# Return JWKS representation of a key

View File

@ -24,7 +24,7 @@ sub restCall {
unless ( $resp->is_success ) {
die $resp->status_line;
}
my $res = eval { from_json( $resp->content ) };
my $res = eval { from_json( $resp->content, { allow_nonref => 1 } ) };
die "Bad REST response: $@" if ($@);
return $res;
}

View File

@ -45,7 +45,7 @@ sub getUser {
return PE_ERROR;
}
$self->logger->debug('Proxy gets a response');
my $res = eval { JSON::from_json( $resp->content ) };
my $res = eval { JSON::from_json( $resp->content, { allow_nonref => 1 } ) };
if ($@) {
$self->logger->error("Bad content: $@");
return PE_ERROR;
@ -81,7 +81,7 @@ sub setSessionInfo {
return PE_ERROR;
}
$self->logger->debug('Proxy gets a response');
my $res = eval { JSON::from_json( $resp->content ) };
my $res = eval { JSON::from_json( $resp->content, { allow_nonref => 1 } ) };
if ($@) {
$self->logger->error("Bad content: $@");
return PE_ERROR;

View File

@ -93,7 +93,7 @@ sub translate {
open F, $json or die $!;
$json = join '', <F>;
close F;
$lang = from_json($json);
$lang = from_json( $json, { allow_nonref => 1 } );
return sub {
($_) = @_;
$$_ =~ s/\s+trspan="(\w+?)"(.*?)>.*?</"$2>".($lang->{$1}||$1).'<'/gse;

View File

@ -35,7 +35,7 @@ sub status {
$buf .= $_;
}
if ($buf) {
eval { $res = from_json($buf) };
eval { $res = from_json( $buf, { allow_nonref => 1 } ) };
if ($@) {
$self->logger->error("JSON error: $@");
$self->logger->error("JSON: $buf");