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

gopherbot opened this issue Nov 7, 2014 · 2 comments


Copy link


go version go1.3.1 darwin/amd64

The following snippet illustrates the problem I'm encountering:
Another example:

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?


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

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 .

Status changed to WorkingAsIntended.

Copy link

Comment 2 by

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.
None yet

No branches or pull requests

2 participants