-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/json: panic if marshaling pointer and non-pointer of same base type #6458
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
Labels
Milestone
Comments
Clarification: only panics if marshaling non-pointer value after pointer values, not the other way around. Most likely cause: https://code.google.com/p/go/source/browse/src/pkg/encoding/json/encode.go#370 The check for reflect.Value.CanAddr is only performed once per field (then encoderFunc gets cached) and returns true if passed struct value was a pointer. reflect.Value.CanAddr returns false for the same field if the containing struct, passed in, was not a pointer value, thus the panic. |
Also odd: http://play.golang.org/p/oL41gqyFYw go1.1.2 {"Raw":{"key":"val"}} {"Raw":"eyAia2V5IjogInZhbCIgfQ=="} Why does the json.RawMessage encode differently for json.Marshal(&x) vs json.Marshal(x)? |
Exactly. Methods are declared for *RawMessage and struct field is of type RawMessage. So it probably makes sense to not treat it as json.Marshaler. In any case, how this field is treated by json.Marshal should not depend on whether the containing struct was passed as pointer or as a value. This is caused by the exact line i posted before, by the way. From Brad's example: Raw is addressable in first case and not addressable in the second (as reported by reflect.Value.CanAddr). |
Labels changed: added go1.2, removed priority-triage. Owner changed to @bradfitz. Status changed to Accepted. |
Sent out https://golang.org/cl/13839045/ Status changed to Started. |
This issue was closed by revision 0f3ea75. Status changed to Fixed. |
jonboulle
added a commit
to jonboulle/spec
that referenced
this issue
Mar 11, 2015
RawMessage does not marshal correctly without a pointer receiver: - golang/go#6528 - golang/go#6458 (comment) This is only a partial solution as really we should be marshalling the IsolatorValue rather than the RawMessage (since currently this will only work when marshalling Isolators that have RawMessage populated).
jonboulle
added a commit
to jonboulle/spec
that referenced
this issue
Mar 11, 2015
RawMessage does not marshal correctly without a pointer receiver: - golang/go#6528 - golang/go#6458 (comment) This is only a partial solution as really we should be marshalling the IsolatorValue rather than the RawMessage (since currently this will only work when marshalling Isolators that have RawMessage populated).
CL https://golang.org/cl/21811 mentions this issue. |
This issue was closed.
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
The text was updated successfully, but these errors were encountered: