-
Notifications
You must be signed in to change notification settings - Fork 810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MarshalJSONPB is not called for custom type if it's an alias to a primitive #411
Comments
Have you tried https://github.com/gogo/protobuf/blob/master/custom_types.md |
Hm, I had those before but ran into some other issue. Notably, if the custom type implements Thanks. |
Actually, I think there's still something wrong. I changed the code to only provide MarshalJSON / UnmarshalJSON functions, and my parsing tests started breaking. When I restore
Note that UnmarshalJSON is never called. If I remove UnmarshalJSONPB function, then UnmarshalJSON is called only two times in the whole test, for numeric values (the custom type is an alias to uint64), but for hex values it's not even called, something else is throwing an error.
|
Is it possible to provide this as a test that we could merge into gogoprotobuf? Then it might also be easier to me to debug. |
I created a simple test here https://github.com/yurishkuro/gogoproto-custom-type. If you rename
|
Kind of related - should I be using |
Ah yes I think you nailed it. PS |
I actually decided to use |
Ok so both TraceID and SpanID are now bytes fields which use customtype.
But I am just shooting in the dark right now. |
I updated https://github.com/yurishkuro/gogoproto-custom-type to reflect the exact use case I have in Jaeger PR. Added README. |
Thank you so much. So when the customtype's Go type is a struct or []byte (previous experience) then you only need to implement UnmarshalJSON, but when the cusomtype's Go type is a native type like uint64, then it calls UnmarshalJSONPB over UnmarshalJSON. I think we can merge your test into gogoprotobuf and attempt a fix. |
Sadly, I originally tried to write the example & tests as a branch of gogoprotobuf repo, but had issues with unrelated test failures before I could do anything. Right now my workaround is to call UnmarshalJSON from UnmarshalJSONPB function. |
If you have the unrelated test failures handy, I can help take a look. But if you prefer that we take a look, given your great work to produce a test case (thanks again), then we can also do that. Also thanks for the work around. |
* added test for MarshalJSONPB not being called for custom type if it's an alias to a primitive (#411) * removed work-around so test fails * added comment thanking contributor * bumped the go version * regenerated after nil check * regenerated after nil check * #411 - check for UnmarshalJSON interface, regardless of reflect.Kind()
@yurishkuro, we've merged in your test, and our fix. Your test now passes. I'm going to close this issue now. Please re-open it if our fix doesn't resolve your problem. |
I am using a custom type
When serializing to JSON using
github.com/gogo/protobuf/jsonpb
I realized that its MarshalJSONPB method is never called, even though it works for other custom types that are defined as structs, instead of a type alias. Instead, the stdlib'sjson
package is used:which happens to call
MarshalText()
.The text was updated successfully, but these errors were encountered: