Skip to content

Serializing protobufs to json #351

Closed

Description

@mwildehahn

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.

Activity

  1. dcodeIO commented on Nov 18, 2015

    @dcodeIO
    Member

    Have you tried Message#encodeJSON?

  2. lanttu commented on Dec 16, 2015

    @lanttu

    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

  3. joeblew99 commented on Dec 17, 2015

    @joeblew99

    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.

  4. leeyeh commented on Feb 23, 2016

    @leeyeh

    +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.

  5. dcodeIO commented on Nov 28, 2016

    @dcodeIO
    Member

    Closing this for now.

    protobuf.js 6.0.0

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

  6. aidansteele commented on Apr 11, 2017

    @aidansteele

    @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?

  7. dcodeIO commented on Apr 11, 2017

    @dcodeIO
    Member

    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:

    • "NaN", "Infinity" strings for floats
    • Any, Timestamp, Duration, Struct, Wrappers, FieldMask, ListValue, Value, NullValue are handled like messages

    Feel free to reopen!

  8. dcodeIO commented on Apr 11, 2017

    @dcodeIO
    Member

    Related: #677

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions