Skip to content
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

encoding/protojson: Is there a way to override marshalling/unmarshalling logic for specific messages? #1322

Closed
svelez opened this issue May 18, 2021 · 2 comments

Comments

@svelez
Copy link

svelez commented May 18, 2021

As was possible in jsonpb?

// JSONPBMarshaler is implemented by protobuf messages that customize the
// way they are marshaled to JSON. Messages that implement this should also
// implement JSONPBUnmarshaler so that the custom format can be parsed.
//
// The JSON marshaling must follow the proto to JSON specification:
// https://developers.google.com/protocol-buffers/docs/proto3#json
//
// Deprecated: Custom types should implement protobuf reflection instead.
type JSONPBMarshaler interface {
MarshalJSONPB(*Marshaler) ([]byte, error)
}

The deprecation comment says to use protobuf reflection, but assuming that I do discover how to completely implement my own message without relying on protoc, I don't see how I can use reflection to have the json structure be different from the proto message structure (as is the case with several well known types)

I am assuming that the comment The JSON marshaling must follow the proto to JSON specification: implies that serialization to json should not utilize arbitrary structure, and that the ability to do this as been dropped from the current library, but I would just like to make sure.

@neild
Copy link
Contributor

neild commented May 18, 2021

There is no way to override the JSON representation for specific messages in encoding/protojson.

The purpose of this package is to (de)serialize messages in the standard protobuf JSON encoding, which is supported by many different implementations in many different languages. Custom encodings for specific messages would prevent cross-implementation interoperability.

@svelez
Copy link
Author

svelez commented May 18, 2021

K. That's what I suspected. Thanks.

@svelez svelez closed this as completed May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants