How do I maintain the backslash when converting to json String using Json Format of Protobuf?

Viewed 31

I have to use gRPC.

I was converting the object I received into json string, and the following problem occurred

example proto

hash: "v\016\177\350\207y\225wM\335]1(Z\266\305\376\027\310_v\321\016Q\v\332\030\303^\032|\375"

but, However, if I convert using Protobuf's util JsonFormat, I get the following results

 "hash": "dg5/6Id5lXdN3V0xKFq2xf4XyF920Q5RC9oYw14afP0="

I want to get this back to its original form, is there a way to write another library or decode it in reverse?

1 Answers

Forget about the format, basically; these are just two ways of representing the same data. The second version is base-64, and decodes to the bytes:

76-0E-7F-E8-87-79-95-77-4D-DD-5D-31-28-5A-B6-C5-FE-17-C8-5F-76-D1-0E-51-0B-DA-18-C3-5E-1A-7C-FD

The first version is C-literal style with octal escapes; v is ASCII 118, aka hex 0x76; \016 is escaped octal for decimal 14, aka hex 0x0E; \177 is escaped octal for decimal 127, aka hex 0x7F - and so on. Most languages have a base-64 encode/decode; the C-literal style with octal escape sequences is ... more niche, and you might need to write your own decoder for that. Depending on where the first string came from, it is worth noting that protobuf (at least the schema variant) also allows fixed-width unicode escapes, via \uNNNN and \UNNNNNNNN, IIRC. And note: the octal in .proto schemas can short-circuit: \12n means the same as \012n - at most 3 digits are taken, but if a non-digit character is encountered, it is still valid as a shorter form.

Related