Change:
falseBytes = []byte("false") ) <del> func toUnstructured(sv, dv reflect.Value) error { st, dt := sv.Type(), dv.Type() </del> <ins> func getMarshaler(v reflect.Value) (encodingjson.Marshaler, bool) { // Check value receivers if v is not a pointer and pointer receivers if v is a pointer if v.Type().Implements(marshalerType) { return v.Interface().(encodingjson.Marshaler), true } // Check pointer receivers if v is not a pointer if v.Kind() != reflect.Ptr && v.CanAddr() { v = v.Addr() if v.Type().Implements(marshalerType) { return v.Interface().(encodingjson.Marshaler), true } } return nil, false } func toUnstructured(sv, dv reflect.Value) error { </ins> // Check if the object has a custom JSON marshaller/unmarshaller. <del> if st.Implements(marshalerType) { </del> <ins> if marshaler, ok := getMarshaler(sv); ok { </ins> if sv.Kind() == reflect.Ptr && sv.IsNil() { // We're done - we don't need to store anything. return nil } <del> marshaler := sv.Interface().(encodingjson.Marshaler) </del> data, err := marshaler.MarshalJSON() if err != nil { return err

Discussion:
Kubernetes version (use ): master. What happened: Unstructured converter from does not roundtrip . What you expected to happen: When I call on a which is backed by it should basically make a copy of the content (can be optimized btw using deep copy ) and not a json object with key. How to reproduce it (as minimally and precisely as possible): See the test in my branch Test output: Anything else we need to know: For some reason changed the argument types from to . I guess the intent is to make the converter a more generic tool. But based on the previous signature I assume I'm doing the right thing. Actually, my code works with and I don't know if the actual object behind the interface is an or not - I just pass it to the converter and it works for usual API types, but not for unstructured. Also it would be useful to have a more type-safe version of the converter (i.e. the previous signature) that just delegates to a lower-level converter that does not care about types.
There are no sig labels on this issue. Please by:<br(1) mentioning a sig: <br(2) specifying the label manually: <br<brNote: method (1) will trigger a notification to the team. You can find the team list and label list
/sig api-machinery
/kind bug
/assign
Looked into this issue just now. It is caused by the pointer vs non-pointer receiver of the method - similar to . In detail: is passed as to when it calls passing as . if passed value implements (has method ) does not implement this interface, does. Because on it . So perhaps the fix is to do the dereferencing inside of the ? Or maybe the same way as - check if the value is addressable, get a pointer to the value and then see if it matches the interface (I dunno if it will work that way)? In any case, the check whether a value implements the interface should be done on both the value itself () and the pointer to the value (; if the value is addressable) to cover both valid cases. This is what stdlib package does. Right now it is broken - it dereferences the pointer and then checks if the thing implements the interface. Does it make sense?

Reiterating the mentions to trigger a notification: <detailsIn response to : Instructions for interacting with me using PR comments are available . If you have questions or suggestions related to my behavior, please file an issue against the repository. </details
does this helps? Look like we need to do MarshalJSON on non-pointers for it to work. I didn't get into the details but if that is the case then making Unstructured's MarshalJSON() receiver, a object instead of pointer should fix your problem, right?
Only if we don't fix the actual cause of the bug which I described above. From the stack overflow question that is mentioned in the link you provided: This code is buggy - to work it should be passing into and into . Yes, json package tries to take the address of the passed (so a pointer receiver should satisfy the interface check) but it cannot take an address of a function argument ( will return false). So, pointer receiver will be invoked if either is a field in a struct (so that an address can be taken via reflection) and a struct is passed (either by value or via a pointer) to / OR is passed. Please take a look at - both issues have the same cause.
I'm happy to take a stab at it and submit a PR but I don't want to spend my time if it is not going to get merged - if we could merge first to make sure people who can approve/lgtm are happy with the approach taken. I strongly believe it is the right thing to do - that is how standard library does it and, well, it is just logical and makes sense :)