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 does not keep context for nested UnmarshalJSON errors #18227

Closed
LeGEC opened this issue Dec 7, 2016 · 4 comments
Closed

Comments

@LeGEC
Copy link

LeGEC commented Dec 7, 2016

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

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

playground : go1.7 amd64p32 - nacl

What did you do?

I triggered a json error :
the error is raised by a specific UnmarshalJSON() function on one of the fields of the go struct value

In this specific example : a time.Time field tries to parse the string "null"

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

What did you expect to see?

some error wrapped into a json.SomeErrorType value.
maybe a new json.UnmarshalValueError :

json.UnmarshalValueError{Offset: 24, Fieldname: "Clock", Suberror: time.ParseError{ ... } ...

(some way to get the context of where the error occurred)

What did you see instead?

the raw error value from the time package :

&time.ParseError{Layout:"\"2006-01-02T15:04:05Z07:00\"", Value:"null", LayoutElem:"\"", ValueElem:"null", Message:""}

additional notes

Since using this as the default behaviour would break the backward compatibility promise, maybe add a switch on the json.Decoder() type :

 func (dec *Decoder) UseDetailedUnmarshalErrors(v bool) {
     ...
}

This would not fix the direct use of json.Unmarshal() function, but would still give a way to get this error context.

@LeGEC LeGEC changed the title json.Decoder does not keep context for nested UnmarshalJSON errors json.Decoder does not keep context for nested UnmarshalJSON errors Dec 7, 2016
@bradfitz bradfitz changed the title json.Decoder does not keep context for nested UnmarshalJSON errors encoding/json: Decoder does not keep context for nested UnmarshalJSON errors Dec 7, 2016
@bradfitz bradfitz added this to the Go1.9 milestone Dec 7, 2016
@bradfitz
Copy link
Contributor

bradfitz commented Dec 7, 2016

Can you try Go 1.8beta1? We're no longer making changes to Go 1.7.

@LeGEC
Copy link
Author

LeGEC commented Dec 8, 2016

In Go 1.8beta1 :

  • json.Unmarshal() will not raise any error when trying to fit a null value into a non pointer time.Time field (question: is this behaviour as expected ?)
  • json.Unmarshal() will still return a raw &time.ParseError{...} when trying to unmarshal a value with an incorrect time format

@bradfitz
Copy link
Contributor

bradfitz commented Dec 8, 2016

Yes, the Unmarshal of null behavior was changed in 1.8. See https://beta.golang.org/doc/go1.8#encoding_json

@bradfitz bradfitz removed this from the Go1.9 milestone Jun 13, 2017
@bradfitz
Copy link
Contributor

I'm going to close this bug, but if you have a concrete JSON change proposal, please file a proposal per https://github.com/golang/proposal#readme

@golang golang locked and limited conversation to collaborators Jun 13, 2018
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

3 participants