golang / protobuf

Go support for Google's protocol buffers
BSD 3-Clause "New" or "Revised" License
9.74k stars 1.58k forks source link

How to un/marshal custom types with Google? #1509

Closed ivanjaros closed 1 year ago

ivanjaros commented 1 year ago

So this library now recommends to switch to google's implementation, which is ok with me. But the problem is that this library has

type JSONPBMarshaler interface {
    MarshalJSONPB(*Marshaler) ([]byte, error)
}

type JSONPBUnmarshaler interface {
    UnmarshalJSONPB(*Unmarshaler, []byte) error
}

but the Google library does not. So what is the migration path in this case? The note // Deprecated: Custom types should implement protobuf reflection instead. is quite useless because it does not say anything useful.

puellanivis commented 1 year ago

I think we had the JSONPB marhsaller overriding because that was the only way to get into compliance with the JSON mapping standard with the old API, however, since the newer API provides protojson and follows the standards, and accounts for WKT, there’s no need to provide a way to override how to marshal/unmarshal to JSON.

Since it’s standardized, we need to follow the standard.

chressie commented 1 year ago

+1 to what puellanivis said. The standard way is to implement protoreflect.Message and use protojson to render it to JSON. I'm closing this. Feel free to re-open, if this didn't address your question.