vendredi 31 juillet 2020

How to convert protobuf wireformat to JSON with just a message descriptor?

Normally, to convert a protobuf message from wire format to JSON, you simply unmarshal the wire format into a proto.Message whose dynamic type is a concrete Go type (generated by protoc-gen-go), and then unmarshal that to JSON with the protojson package.

I would like to know how to do the same if you don't have a concrete Go type, but a protoreflect.MessageDescriptor instead. The descriptor should have all the information necessary to parse the wire format and construct a JSON (or other formats) from it, but I can't seem to find an API for that. It seems like I need something like the following:

func UnmarshalFromWire(b []byte, desc protoreflect.MessageDescriptor) (protoreflect.Message, error)

func MarshalToJSON(m protoreflect.Message) ([]byte, error)

Is there an API like that or similar?





Aucun commentaire:

Enregistrer un commentaire