Navigation Menu

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: misleading error parsing invalid json with multibyte characters #57405

Open
adamroyjones opened this issue Dec 20, 2022 · 1 comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@adamroyjones
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.19.4 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

Linux, amd64.

What did you do?

Here is a playground link.

package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	// This uses typographic quotes, not straight quotes.
	bad := `{ “hello”: “world” }`
	var j json.RawMessage
	err := json.Unmarshal([]byte(bad), &j)
	fmt.Printf("err: %v\n", err)
	// prints: err: invalid character 'â' looking for beginning of object key string
}

What did you expect to see?

I'd expect the error message to identify the grapheme.

What did you see instead?

The error message prints out â. This corresponds to the first byte of the multibyte sequence that composes the grapheme .

Specifically

"“".bytes.first == 226 # true

which commonly corresponds to â.

I'm sure this is all well-known, but I didn't see a crisp example of this in the issue log. I may have missed it. If so, I'm sorry.

@mvdan
Copy link
Member

mvdan commented Dec 20, 2022

This is somewhat similar to #56332, but in this case there is a reasonably better error message we could provide: using the full rune.

@dr2chase dr2chase added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Dec 20, 2022
@seankhliao seankhliao changed the title encoding/json: Misleading error when unmarshalling invalid JSON encoding/json: misleading error parsing invalid json with multibyte characters Dec 24, 2022
@seankhliao seankhliao added this to the Unplanned milestone Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants