Skip to content

Commit 2cf8c1b

Browse files
authored
fix: google protobuf json serialization is malformed (#10)
1 parent d62cbcb commit 2cf8c1b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
"php": "^8.3",
2626
"ext-grpc": "*",
2727
"google/protobuf": "^4.28",
28-
"grpc/grpc": "^1.57"
28+
"grpc/grpc": "^1.57",
29+
"symfony/serializer": "^7.3",
30+
"symfony/property-access": "^7.3"
2931
},
3032
"require-dev": {
3133
"laravel/pint": "^1.18",

src/GrpcClient/GrpcClient.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use SpaceX\API\Device\Response;
1313
use SRWieZ\StarlinkClient\Exceptions\GrpcCallFailedException;
1414
use SRWieZ\StarlinkClient\Exceptions\PermissionDeniedException;
15+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
16+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
17+
use Symfony\Component\Serializer\Serializer;
1518

1619
class GrpcClient
1720
{
@@ -81,16 +84,18 @@ public static function responseToArray(?Message $response): array
8184
return [];
8285
}
8386

84-
$return = (array) json_decode(
85-
json: $response->serializeToJsonString(),
86-
associative: true,
87+
$serializer = new Serializer(
88+
normalizers: [new ObjectNormalizer],
89+
encoders: [new JsonEncoder]
8790
);
8891

89-
if (json_last_error() !== JSON_ERROR_NONE) {
90-
throw new \RuntimeException('Failed to decode JSON response: '.json_last_error_msg());
91-
}
92-
93-
return $return;
92+
return $serializer->normalize(
93+
data: $response,
94+
format: 'json',
95+
context: [
96+
'json_encode_options' => JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT,
97+
]
98+
);
9499
}
95100

96101
public function disconnect(): void

0 commit comments

Comments
 (0)