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: some strings aren't marshaled/unmarshaled correctly #9068

Closed
gopherbot opened this issue Nov 7, 2014 · 2 comments

Comments

Projects
None yet
2 participants
@gopherbot
Copy link

commented Nov 7, 2014

by slunk@terpmail.umd.edu:

go version go1.3.1 darwin/amd64

The following snippet illustrates the problem I'm encountering:
http://play.golang.org/p/7qCY_H5pnN
Another example: http://play.golang.org/p/5Jt-wqnlNT

What happened?

If you try to marshal / unmarshal a sha1 hash ([]byte) cast to a string, you don't get
the same string out as you put in.

What should have happened instead?

Ideally, you should get the same string out as you put in. If that turns out to be
impossible for some reason, maybe make the Marshal function return an error for strings
it can't handle?

Additional

You guys probably don't need this, but I tried the following as a sanity check for
myself on what it means to cast from []byte to string in go:
http://play.golang.org/p/9n7GLU9R14
@ianlancetaylor

This comment has been minimized.

Copy link
Contributor

commented Nov 7, 2014

Comment 1:

This behaviour, while sometimes annoying, is working as intended and is documented on
tip: "String values encode as JSON strings coerced to valid UTF-8, replacing invalid
bytes with the Unicode replacement rune."  To use random bytes as a JSON string, you
need to convert them to hex, for example, modifying your first test case as
http://play.golang.org/p/qrpMkJC8Mb .

Status changed to WorkingAsIntended.

@gopherbot

This comment has been minimized.

Copy link
Author

commented Nov 7, 2014

Comment 2 by slunk@terpmail.umd.edu:

Ah, okay. Thanks for the quick response and sorry for the spurious report.

@golang golang locked and limited conversation to collaborators Jun 25, 2016

This issue was closed.

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.