Skip to content
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

encoding/json: explain how to properly Marshall RawMessage #16648

Closed
hydroflame opened this issue Aug 9, 2016 · 5 comments

Comments

Projects
None yet
6 participants
@hydroflame
Copy link

commented Aug 9, 2016

encoding/json has an example for Unmarshalling json.RawMessage but not for Marshalling it. The example doesn't point out that Marshalling requires a *RawMessage. Using RawMessage doesn't give the expected results and it's not immediately obvious why.
https://play.golang.org/p/A1nbikzzVY

@josharian josharian changed the title doc encoding/json: explain how to properly Marshall RawMessage encoding/json: explain how to properly Marshall RawMessage Aug 10, 2016

@josharian

This comment has been minimized.

Copy link
Contributor

commented Aug 10, 2016

@ucirello

This comment has been minimized.

Copy link
Contributor

commented Aug 10, 2016

I think it makes sense adding such example, namely because the documentation says it can be used both to delay parsing as much to precompute a JSON encoding - the latter missing. Likely something along the lines: https://play.golang.org/p/OdTp9Dh0o1

@gopherbot

This comment has been minimized.

Copy link

commented Aug 10, 2016

CL https://golang.org/cl/26692 mentions this issue.

@joegrasse

This comment has been minimized.

Copy link

commented Aug 10, 2016

Back to the example that @hydroflame gave, here is another way it works.

https://play.golang.org/p/sMKGqodDde

So is it better to just always pass the pointer of the thing to json.Marshal?

cc @ccirello @josharian

@quentinmit quentinmit added this to the Go1.8 milestone Aug 11, 2016

@ucirello

This comment has been minimized.

Copy link
Contributor

commented Aug 12, 2016

I believe it to be a bit misleading to say that is better to call always json.Marshal(&v). json.Marshal will execute json.Marshaler of pointers, otherwise it will just take the concrete type and marshal it.

https://play.golang.org/p/-lwP-OzIEx

@gopherbot gopherbot closed this in 7a974a4 Aug 16, 2016

@golang golang locked and limited conversation to collaborators Aug 16, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.