Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert GRPC protobuf response to PHP array without mapping

I am wondering if there is a quick way to get a PHP array from the \Google\Protobuf\Internal\Message object that is returned from the GRPC client without having to explicitly map the response objects fields to an array.

The GRPC tutorial seems to get the fields by calling their getters:

$point = new Routeguide\Point();
$point->setLatitude(409146138);
$point->setLongitude(-746188906);

// $feature is the response
list($feature, $status) = $client->GetFeature($point)->wait();

// Calling getters here
print sprintf("Found %s \n  at %f, %f\n", $feature->getName(),
              $feature->getLocation()->getLatitude() / COORD_FACTOR,
              $feature->getLocation()->getLongitude() / COORD_FACTOR);

Is there a faster way? I see the decode() method on the \Google\Protobuf\Internal\Message class but haven't had luck with getting it to work. I don't know if that is its intended purpose either.

like image 377
Tom Mertz Avatar asked Oct 29 '22 08:10

Tom Mertz


1 Answers

I know it's a bit late but for anyone looking for the answer today,
below is what I do to serialize the gRPC object response directly to a json object:

 $feature->serializeToJsonString();
like image 53
mallix Avatar answered Nov 09 '22 06:11

mallix