iris icon indicating copy to clipboard operation
iris copied to clipboard

Jsonpb support

Open siehc opened this issue 7 years ago • 8 comments

It would be great to support jsonpb. Standard library's json package has an issue to work with protobuf.

siehc avatar Feb 15 '19 02:02 siehc

Hello, I really don't understand what you want from Iris, protos are not the same as JSON, we can't do Marshal/Unmarshal on-fly of any object. You can do it by-hand by your own, iris has nothing to do with this matter. There is already proto cli plugin which will generate masrhal/unmarshal functions for each of your proto payloads.

Could you please explain us better what exactly is that you want?

Thanks, Gerasimos Maropoulos

kataras avatar Feb 15 '19 19:02 kataras

Sorry for not explaining it clearly.

I use iris as an gateway to grpc microservices. Sending grpc response to client by ctx,JSON() will cause wrong json field name issue. It would be great to have a ctx.JSONPB() which invoke jsonpb marshaler to generate JSON instead of standard json library.

siehc avatar Feb 16 '19 13:02 siehc

Ofc, send us a link of a jsonpb marshaler lib that you tested and we can use which is not a CLI app/generator and we can add a function like this on the Context. In short, how are you doing this so far without a ctx.JSONP func?

kataras avatar Feb 16 '19 16:02 kataras

Here is the jsonpb lib and sample code.

func SendJsonPB(ctx iris.Context, p proto.Message) {
	j := &jsonpb.Marshaler{}
	r, _ := j.MarshalToString(p)
	ctx.ContentType(context.ContentJSONHeaderValue)
	ctx.WriteString(r)
}

siehc avatar Feb 18 '19 07:02 siehc

@siehc i think its better to have it break it part and just making the separate repo to work with iris.context. Post the repo link to here as others can see that there is an app to work with iris. @kataras is that possible? the code base is too big for now, i think we should split it up to have other repo to work directly with iris.Context. i think the Context is pretty stable and robust now.

jjhesk avatar Feb 25 '19 10:02 jjhesk

Or, just make json marshaller in context.JSON() configurable?

siehc avatar Feb 26 '19 03:02 siehc

@siehc the context.JSON as all of the context methods can be overriden by custom implementations, take a look at the _examples/routing/custom-context.

@jjhesk The jsonpb library is huge because it depends on the github.com/golang/protobuf library itself... so you know why we can't add this, the avg time to download and install Iris will probably took x4 time than it currently does, I am sorry we can't add everything, this is why Iris is so modular, so you can add your own things if you want so like the SendJsonPB method you wrote above.

Thanks.

kataras avatar Jun 23 '19 18:06 kataras

@siehc just letting you know about the new context methods for protos:

  • Context.Protobuf(proto.Message) sends protobuf to the client
  • Context.JSON is able to send protos as JSON as well
  • Context.ReadProtobuf(ptr) binds request body to a proto message
  • Context.ReadJSONProtobuf(ptr, ...options) binds JSON request body to a proto message

kataras avatar Jun 28 '20 20:06 kataras