Protobuf data deserializing error in swift

1.7k Views Asked by At

I'm trying to deserialize response data from POST query. But my proto deserializer returns error:

'InvalidProtocolBuffer', reason: 'Invalid Tag: last tag 76'

Here is response string from rest client:

"EJMDGhnQlNC+0YHRgtGD0L8g0LfQsNC60YDRi9GC"

and here how i'm creating data from this string:

let data = dataString.dataUsingEncoding(NSUTF8StringEncoding);

And parse it:

var _deData = MobileGetNewsResponse.parseFromData(jsonData!)

Any suggestions to resolve this problem?

1

There are 1 best solutions below

0
On BEST ANSWER
"EJMDGhnQlNC+0YHRgtGD0L8g0LfQsNC60YDRi9GC"

This data is not, itself, a protobuf. It appears that it may be a base64-encoded protobuf. You need to base64-decode it first, then pass it to the protobuf parser.

Keep in mind that protobufs are raw bytes, not text. You should never attempt to store a raw protobuf in a string, nor attempt to interpret it as Unicode or UTF-8. It's just bytes. (base64 is one way of turning bytes into text.)

I'm also concerned about this line:

var _deData = MobileGetNewsResponse.parseFromData(jsonData!)

JSON and Protobufs are two completely different formats. You should not be attempting to parse a protobuf as JSON nor vice versa.