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: Decoder.Token: on error SyntaxError.Offset is 0 #22478

Closed
markusheukelom opened this issue Oct 28, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@markusheukelom
Copy link

commented Oct 28, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.9.2 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/Markus/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9.2/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bf/kbhmwc8j7nx3kjb285knl23c0000gn/T/go-build616159285=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

Process a file with JSON as a stream use json.Decoder.Token.

I try to use the SyntraxError.Offset to determine the line+pos of the error, for feedback to the user.

Minimal example:

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

package main

import (
"fmt"
"encoding/json"
"strings"
)

func main() {
dec := json.NewDecoder(strings.NewReader({"foo": "bar"error}))

for {
	_, err := dec.Token()
	if err != nil {
		syntax := err.(*json.SyntaxError)
		fmt.Printf("offset: %d (expected 13)", syntax.Offset)
		return
	}
}

}

What did you expect to see?

I expect SyntaxError.Offset to be the byte offset into the Reader passed to DecoderNew.

What did you see instead?

It seems that the method Decoder.Token in the json package returns an SyntaxError with Offset always set to 0.

@ianlancetaylor ianlancetaylor added this to the Go1.10 milestone Oct 28, 2017

@gopherbot

This comment has been minimized.

Copy link

commented Oct 29, 2017

Change https://golang.org/cl/74251 mentions this issue: encoding/json: Include the offset of a SyntaxError

@gopherbot gopherbot closed this in f1ce59d Oct 31, 2017

@golang golang locked and limited conversation to collaborators Oct 31, 2018

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.