-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON representation of ProtoBufJS 'bytes' type #191
Comments
Internally, bytes fields are represented by a ByteBuffer object, which in turn wraps either an ArrayBuffer in the browser or a node Buffer under node.js. As these types are not valid in JSON, handling them requires special care. You might either convert them to hex or base64 encoding as a string and convert it back later on or develop a custom conversion. Let's say you want to display the contents of a bytes field in a form as text. You'd then call |
Well my server side code is var obj = JSON.parse(json_from_client, null, 4); since i am directly using the encoding on JSON from client , now do i need to parse through the JSON and for all bytes type fields do i need to convert them back from hex string to node buffer. |
I see. What you should be able to do currently is using an array of octets for a buffer field, which later on should properly be wrapped by a bytebuffer. For example, if you receive the following JSON:
And |
Thanks a lot , i will try this server side conversion (many of my bytes field belong to repeated messages type so may be i have to iterate them) |
I have similar issue and I think that the current implementation is not optimal. Currently it wraps the data to ByteBuffer with utf-8 encoding. If the results is "bytes", which indicates binary data, then the utf-8 encoding is probably not very suitable. The default encoding should be either base64 or binary. (in Field.verifyValue) The other possibility is to have the a parameter |
See: Changes in ProtoBuf.js 3.8 This will break any applications already using |
In my project i am dynamically generating json structure from input fields:
Structure of my sample project is as follows:
Now all work fine for string,uint,int etc
But how to i represent bytes type. i tried pushing a object with bytes something like 3000 into {0: 0, 1: 0, 2: 0, 3: 0, 4: 11, 5: 184} also i tried setting it to [0,0,0,11,184] all resulting in errors like
[Error: Cannot wrap buffer of type object, Object]
,[Error: Illegal value for Message.Field .protocol.ServerRequest.device_id: 3,1,0,0 (no array expected)] respectively
how exactly can we represent bytes in json
The text was updated successfully, but these errors were encountered: