Created
December 19, 2010 10:05
-
-
Save hideo55/747234 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Android::FixupUTF8; | |
sub fixup { | |
package Android; | |
no warnings 'redefine'; | |
*do_rpc = sub { | |
my $self = shift; | |
if ( $self->trace ) { | |
show_trace(qq[do_rpc: $self: @_]); | |
} | |
my $method = pop; | |
my $request = to_json( | |
{ | |
id => $self->{id}, | |
method => $method, | |
params => [@_] | |
}, | |
{ utf8 => 1 } | |
); | |
if ( defined $self->{conn} ) { | |
print { $self->{conn} } $request, "¥n"; | |
if ( $self->trace ) { | |
show_trace(qq[client: sent: "$request"]); | |
} | |
$self->{id}++; | |
my $response = readline( $self->{conn} ); | |
chomp $response; | |
if ( $self->trace ) { | |
show_trace(qq[client: rcvd: "$response"]); | |
} | |
if ( defined $response && length $response ) { | |
my $result = from_json( $response, { utf8 => 1 } ); | |
my $success = 0; | |
my $error; | |
if ( defined $result ) { | |
if ( ref $result eq 'HASH' ) { | |
if ( defined $result->{error} ) { | |
$error = to_json( { error => $result->{error} }, | |
{ utf8 => 1 } ); | |
} | |
else { | |
$success = 1; | |
} | |
} | |
else { | |
$error = "illegal JSON reply: $result"; | |
} | |
} | |
unless ( $success || defined $error ) { | |
$error = "unknown JSON error"; | |
} | |
if ( defined $error ) { | |
printf STDERR "$0: client: error: %s¥n", $error; | |
} | |
if ( $Opt{trace} ) { | |
print STDERR Data::Dumper->Dump( [$result], [qw(result)] ); | |
} | |
return $result; | |
} | |
} | |
$self->close; | |
return; | |
}; | |
} | |
1; | |
__END__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment