protoreflect icon indicating copy to clipboard operation
protoreflect copied to clipboard

how to convert dynamic message to json

Open Khushbukela opened this issue 3 years ago • 4 comments

not able to convert dynamic message to JSON if the dynamic message has any (type and URL)

md := fd.FindMessage(messageName) dm := dynamic.NewMessage(md) err := dm.Unmarshal(protobufMsg) now how can I convert dynamic message to json . this is the doubt

Khushbukela avatar Dec 21 '21 08:12 Khushbukela

I'm trying to understand the question. Are you saying that, in your example, md is the message descriptor for google.protobuf.Any? Or are you saying that protobufMsg is an encoded google.protobuf.Any, and you are trying to unmarshal its enclosed data into a dynamic message?

Perhaps a fuller code sample and example input data as well as both what you expected to happen and what you actually saw might help me understand exactly what you're trying to do.

jhump avatar Dec 21 '21 13:12 jhump

my protomsg containing this field image

dynamic message- other_properties:<type_url:"example.com/yaddayaddayadda/" value:"serialized" >

Khushbukela avatar Dec 22 '21 05:12 Khushbukela

@Khushbukela, I still don't follow. Can you provide a code example with more context and an actual JSON input example, too? And also say what it is you are seeing vs. what you expect to see? Are you getting an error? Is the unmarshaled value incorrect?

jhump avatar Feb 05 '22 16:02 jhump

You can just call dm.MarshalJSON or dm.MarshalJSONPB.

hitzhangjie avatar Apr 20 '22 13:04 hitzhangjie