]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_perl/Maasha/BGB/Session.pm
changed BGB session format to JSON
[biopieces.git] / code_perl / Maasha / BGB / Session.pm
index 129e41fd8d0a91f9dc676da7002021948ef439cd..d55132a1dd113227e589c9ba2e2cdfcb2a047f6b 100644 (file)
@@ -32,6 +32,7 @@ use warnings;
 use strict;
 use Data::Dumper;
 use Digest::MD5;
+use JSON;
 use Maasha::Common;
 use Maasha::Filesys;
 
@@ -72,26 +73,21 @@ sub session_restore
 
     # Returns a hashref.
 
-    my ( $fh, $line, $user, $password, $sid, $time, %session );
+    my ( $fh, $json, $json_text, $session );
+
+    local $/ = undef;
 
     $fh = Maasha::Filesys::file_read_open( $file );
 
-    while ( $line = <$fh> )
-    {
-        chomp $line;
+    $json_text = <$fh>;
 
-        ( $user, $password, $sid, $time ) = split /\t/, $line;
+    close $fh;
 
-        $session{ $user } = {
-            PASSWORD   => $password,
-            SESSION_ID => $sid,
-            TIME       => $time,
-        };
-    }
+    $json = new JSON;
 
-    close $fh;
+    $session = $json->decode( $json_text );
 
-    return wantarray ? %session : \%session;
+    return wantarray ? %{ $session } : $session;
 }
 
 
@@ -107,20 +103,15 @@ sub session_store
 
     # Returns nothing.
 
-    my ( $fh, $user );
+    my ( $json, $json_text, $fh );
+
+    $json = new JSON;
+
+    $json_text = $json->pretty->encode( $session );
 
     $fh = Maasha::Filesys::file_write_open( $file );
 
-    foreach $user ( keys %{ $session } )
-    {
-        print $fh join(
-            "\t",
-            $user,
-            $session->{ $user }->{ 'PASSWORD' },
-            $session->{ $user }->{ 'SESSION_ID' },
-            $session->{ $user }->{ 'TIME' }
-        ), "\n";
-    }
+    print $fh $json_text;
 
     close $fh;
 }
@@ -130,3 +121,5 @@ sub session_store
 
 
 1;
+
+__END__