Skip to content

time: time.Time's UnmarshalJSON produces bad error message when given "null" #9037

@gopherbot

Description

@gopherbot

by dannyyoo@google.com:

The following demonstrates the problem:

---------------
package main

import (
    "encoding/json"
    "fmt"
    "bytes"
    "time"
)

type RegisterStructure struct {
    SignupTimestamp time.Time `json:"signuptimestamp,omitempty"`
}

func main() {
    requestBody := []byte(`{"signuptimestamp" : null}`)
    dec := json.NewDecoder(bytes.NewReader(requestBody))
    registerInstance := RegisterStructure{}
    err := dec.Decode(&registerInstance)
    if err != nil {
        fmt.Println(err)
    }
}
---------------

This reports the following error:

----------------------------
parsing time "null" as ""2006-01-02T15:04:05Z07:00"":
cannot parse "null" as """
----------------------------


Instead, what should probably happen is to set to the time.Time zero value.  This came
up in the course of answering on Stack Overflow:

http://stackoverflow.com/questions/26684752/json-decode-cannot-parse-timestamp-in-json-into-go-struct

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions