-
Notifications
You must be signed in to change notification settings - Fork 17.6k
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
time: time.Time's UnmarshalJSON produces bad error message when given "null" #9037
Comments
The documentation says: "The JSON null value unmarshals into an interface, map, pointer, or slice by setting that Go value to nil. Because null is often used in JSON to mean “not present,” unmarshaling a JSON null into any other Go type has no effect on the value and produces no error." My understanding is a null value bound to a time.Time should leave it unchanged (since a time.Time is a struct). Now, time.Time implements the json.Unmarshaller interface, and the implementation just parses a RFC3339 formatted string, without checking for JSON null values. In the JSON decoding engine, the call to the Unmarshaller interface takes precedence over null value management, which I guess is the origin of the problem. There are two ways to fix it:
I have a fix which seems to work fine for the first case, but I'm not 100% sure it is really the expected behavior. Can someone please advise? |
JSON null probably decode to the zero value of time.Time{} |
Currently, it returns an error. See http://play.golang.org/p/4hwb51LBPG The question is: should it decode to a zero value (making it a special case), or leave the value unchanged (as specified in the encoding/json package documentation)? And, in that case, should it be managed in encoding/json, or delegated to the unmarshaller implementation of time.Time? Note that this is actually not specific to time.Time, I can produce a similar issue with any type implementing the Unmarshaller interface - see http://play.golang.org/p/Q6_XQjYIbn |
I think we are in the freeze now, so I suppose we will not change the behavior described in the documentation. Mailing a CL implementing my first solution. |
CL https://golang.org/cl/9376 mentions this issue. |
This issue has to be reopened, and set to Go1.6 (because the fix has been reverted). |
CL https://golang.org/cl/14552 mentions this issue. |
Putting off again. The change is too intricate, and we didn't get to it in time. Sorry. |
@rsc moved the CL to 1.8; sadly we won't get to this for 1.7. |
CL https://golang.org/cl/30944 mentions this issue. |
I made an example of a Workaround for versions before 1.8 that uses this patch, solving the time.Time problem: |
@segeschecho FWIW, it only solves the |
by dannyyoo@google.com:
The text was updated successfully, but these errors were encountered: