go icon indicating copy to clipboard operation
go copied to clipboard

Compatible with jsonpb

Open smthpickboy opened this issue 7 years ago • 1 comments

protobuf/jsonpb is using encoding/json with some modification to convert between protocol buffer and json, which is used by grpc-gateway to generate a HTTP proxy to gRPC backend service.

I tried to replace encoding/json with jsoniter, but it couldn't pass the unit tests of jsonpb:

--- FAIL: TestMarshaling (0.00s)
    jsonpb_test.go:467: required bytes: got [{"byts":null}] want [{"byts":""}]
--- FAIL: TestMarshalAnyJSONPBMarshaler (0.00s)
    jsonpb_test.go:532: marshalling JSON produced incorrect output: got {"@type":"type.googleapis.com/google.protobuf.jsonpb.testing.dynamicMessage","baz":[0, 1, 2, 3],"foo":"bar"}, wanted {"@type":"type.googleapis.com/google.protobuf.jsonpb.testing.dynamicMessage","baz":[0,1,2,3],"foo":"bar"}
--- FAIL: TestUnmarshaling (0.00s)
    jsonpb_test.go:779: simple pretty object: readUint64: unexpected character: �, error found in #2 byte of ...| "-640000000|..., bigger context ...| "-6400000000"|...
    jsonpb_test.go:779: repeated fields pretty object: readUint64: unexpected character: �, error found in #6 byte of ...|
            "-987654321|..., bigger context ...|
            "-987654321"|...
    jsonpb_test.go:779: nested message/enum pretty object: readUint64: unexpected character: �, error found in #2 byte of ...| "25"|..., bigger context ...| "25"|...
    jsonpb_test.go:787: null Struct: got [st: <
        >
        ] want []
    jsonpb_test.go:779: basic Struct: bad value in StructValue for key "a": unrecognized type for Value " \"x\""
    jsonpb_test.go:779: nested Struct: bad value in StructValue for key "a": bad value in StructValue for key "b": unrecognized type for Value " 1"
    jsonpb_test.go:787: null ListValue: got [lv: <
        >
        ] want []
    jsonpb_test.go:779: basic ListValue: unrecognized type for Value " 3"
    jsonpb_test.go:779: list of lists Value: unrecognized type for Value " \"z\""
--- FAIL: TestUnmarshalNext (0.00s)
    jsonpb_test.go:829: simple pretty object: readUint64: unexpected character: �, error found in #2 byte of ...| "-640000000|..., bigger context ...| "-6400000000"|...
    jsonpb_test.go:829: repeated fields pretty object: readUint64: unexpected character: �, error found in #6 byte of ...|
            "-987654321|..., bigger context ...|
            "-987654321"|...
--- FAIL: TestUnmarshalAnyJSONPBUnmarshaler (0.00s)
    jsonpb_test.go:968: message contents not set correctly after unmarshalling JSON: got {blah.com/google.protobuf.jsonpb.testing.dynamicMessage [10 32 123 34 98 97 122 34 58 91 48 44 32 49 44 32 50 44 32 51 93 44 34 102 111 111 34 58 34 98 97 114 34 125] {} [] 0}, wanted {blah.com/google.protobuf.jsonpb.testing.dynamicMessage [10 29 123 34 98 97 122 34 58 91 48 44 49 44 50 44 51 93 44 34 102 111 111 34 58 34 98 97 114 34 125] {} [] 0}
--- FAIL: TestUnmarshalUnsetRequiredFields (0.00s)
    jsonpb_test.go:1147: direct required field set to null: expecting error in unmarshaling with unset required fields {"str": null}
    jsonpb_test.go:1147: indirect required field set to null: expecting error in unmarshaling with unset required fields {"subm": {"str": null}}
    jsonpb_test.go:1147: direct required wkt field set to null: expecting error in unmarshaling with unset required fields {"str": null}
    jsonpb_test.go:1147: required in map value set to null: expecting error in unmarshaling with unset required fields {"map_field": {"a": {"str": "hello"}, "b": {"str": null}}}
    jsonpb_test.go:1147: required in slice item set to null: expecting error in unmarshaling with unset required fields {"slice_field": [{"str": "hello"}, {"str": null}]}
    jsonpb_test.go:1147: required inside oneof set to null: expecting error in unmarshaling with unset required fields {"msgWithRequired": {"str": null}}
    jsonpb_test.go:1147: required field in extension set to null: expecting error in unmarshaling with unset required fields {"[jsonpb.extm]":{"str": null}}
FAIL
FAIL	github.com/golang/protobuf/jsonpb	0.015s
?   	github.com/golang/protobuf/jsonpb/jsonpb_test_proto	[no test files]

It would really increase the performance of gRPC-gateway when transferring binary data if jsoniter could be compatible with jsonpb.

Thanks a lot!

smthpickboy avatar May 04 '18 08:05 smthpickboy

I forked a project to compatible with jsonpb: https://github.com/defool/go-json. But I think I still need a better solution.

defool avatar Dec 25 '22 08:12 defool