google / rejoiner

Generates a unified GraphQL schema from gRPC microservices and other Protobuf sources
https://google.github.io/rejoiner/
Apache License 2.0
3.67k stars 143 forks source link

returning bytes as a utf8-encoded string #103

Open DianaSuvorova opened 4 years ago

DianaSuvorova commented 4 years ago

Currently bytes are returned as an array of bytes, which gets transferred as an array of numbers in Json.

Before

This pr changes so the input and the output for Bytes is expected to be utf-8 encoded string

After
sheepdreamofandroids commented 4 years ago

Bytes are just small integer numbers, why would you assume that an array of them is a String? In UTF-8 even, why not ASCII? I could understand if this were an array of characters. Am I missing something?

DianaSuvorova commented 4 years ago

@sheepdreamofandroids you are correct, bytes are just small numbers. In case when you communicate over Json, there is no such type as byte, so byte array will be transferred as an array of integers. So normally if you need to transfer byteArray via Json you'd convert it to string, most commonly using base64. In this particular example, I thought using utf-8 is ok, because these bytes are from proto's byteString which I thought already is using utf-8. internally, but I might be totally wrong, and it may need other decoder.

sheepdreamofandroids commented 4 years ago

I see, you simply want a more compact encoding of the bytes. In that case I think that base64 is much safer than a straightforward conversion to text because you avoid all kinds of transformations based on encodings. On the other hand, if that kind of efficiency is important to you, why not use protobuf or another binary encoding on the client side as well? Nobody forces you to use JSON.