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
ghost opened this issue Aug 9, 2016 · 5 comments
Closed

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

ghost opened this issue Aug 9, 2016 · 5 comments

Comments

@ghost
Copy link

ghost 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
Copy link
Contributor

cc @ccirello

@ucirello
Copy link
Contributor

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
Copy link

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

@joegrasse
Copy link

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
Copy link
Contributor

ucirello 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

@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.
Projects
None yet
Development

No branches or pull requests

5 participants