protobufjs / protobuf.js

Protocol Buffers for JavaScript & TypeScript.
Other
9.83k stars 1.41k forks source link

Serializing protobufs to json #351

Closed mwildehahn closed 7 years ago

mwildehahn commented 8 years ago

with proto3, protocol buffers now have a standard json format, is there a way to leverage this with this library?

my use case is caching protocol buffers in local storage. currently i've been base64 encoding the messages before storing them. this becomes more complex with the Map type.

dcodeIO commented 8 years ago

Have you tried Message#encodeJSON?

lanttu commented 8 years ago

The method doesn't seem to use canonical proto3 JSON encoding as specfied in here: https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json

joeblew99 commented 8 years ago

Did you sort out if Google's support for json, makes bufferjs redundant ?

I suspect that bufferjs allows marshalling between json and PB client side, and grpc allows marshalling between the two server side at the facade.

I would like to know what the answer to this is from an architectural perspective.

leeyeh commented 8 years ago

+1.

My use case is to encode to and decode from the canonical formatted JSON data, since React Native's js-native bright is text-based.

dcodeIO commented 7 years ago

Closing this for now.

protobuf.js 6.0.0

Feel free to send a pull request if this is still a requirement.

aidansteele commented 7 years ago

@dcodeIO Sorry, it's not clear to me - is the issue closed because any changes discussed here are no longer relevant (due to rewrite) or because protobuf.js 6.x now supports the functionality?

dcodeIO commented 7 years ago

or because protobuf.js 6.x now supports the functionality

Not quite, but the default toJSON implementation of message instances should be close.

someMessage.toJSON();
// or
SomeMessage.toObject(someMessage, util.toJSONOptions);

Not yet supported:

Feel free to reopen!

dcodeIO commented 7 years ago

Related: https://github.com/dcodeIO/protobuf.js/issues/677