为什么go的UnmarshalJSON方法接收JSON对象而不仅仅是值?

huangapple go评论110阅读模式
英文:

Why does go UnmarshalJSON receives Json Object and not just value

问题

我得到了以下自定义类型:

  1. type TimeWithoutZone struct {
  2. time.Time
  3. }

编组工作正常:

  1. const timeWithoutZoneFormat = "2006-01-02T15:04:05"
  2. func (t *TimeWithoutZone) MarshalJSON() ([]byte, error) {
  3. stamp := fmt.Sprintf(`"%s"`, t.Time.Format(timeWithoutZoneFormat))
  4. return []byte(stamp), nil
  5. }

但是在这里,日期无法解析:

  1. func (t *TimeWithoutZone) UnmarshalJSON(data []byte) (err error) {
  2. log.Println("Parsing: " + string(data))
  3. t.Time, err = time.Parse(`"`+timeWithoutZoneFormat+`"`, string(data))
  4. if err != nil {
  5. return err
  6. }
  7. return nil
  8. }

它记录了:Parsing: {"time":"2016-09-06T11:06:16"},但我希望它只解析time的值。

我做错了什么?这是相关的测试:

  1. type TimeTestObj struct {
  2. Time TimeWithoutZone `json:"time"`
  3. }
  4. func TestParseDataWithoutTimezone(t *testing.T) {
  5. parsed := TimeWithoutZone{}
  6. data := `{"time":"2016-09-06T11:06:16"}`
  7. err := json.Unmarshal([]byte(data), &parsed)
  8. if err != nil {
  9. t.Error(err)
  10. }
  11. if parsed.Unix() != 1473152776 {
  12. t.Error(parsed.Unix(), "!=", 1473152776)
  13. }
  14. }

我找到的所有示例,甚至Go时间包的默认解析器似乎都是这样工作的...

英文:

I got the following custom type:

  1. type TimeWithoutZone struct {
  2. time.Time
  3. }

The Marshaling works fine:

  1. const timeWithoutZoneFormat = "2006-01-02T15:04:05"
  2. func (t *TimeWithoutZone) MarshalJSON() ([]byte, error) {
  3. stamp := fmt.Sprintf(`"%s"`, t.Time.Format(timeWithoutZoneFormat ))
  4. return []byte(stamp), nil
  5. }

But here the date can not be parsed:

  1. func (t *TimeWithoutZone) UnmarshalJSON(data []byte) (err error) {
  2. log.Println("Parsing: " + string(data))
  3. t.Time, err = time.Parse(`"` + timeWithoutZoneFormat + `"`, string(data))
  4. if err != nil {
  5. return err
  6. }
  7. return nil
  8. }

It logs: Parsing: {"time":"2016-09-06T11:06:16"} but I would expect it to parse just the value of time

What am I doing wrong? here is the related test:

  1. type TimeTestObj struct {
  2. Time TimeWithoutZone `json:"time"`
  3. }
  4. func TestParseDataWithoutTimezone(t *testing.T) {
  5. parsed := TimeWithoutZone{}
  6. data := `{"time":"2016-09-06T11:06:16"}`
  7. err := json.Unmarshal([]byte(data), &parsed)
  8. if err != nil {
  9. t.Error(err)
  10. }
  11. if parsed.Unix() != 1473152776 {
  12. t.Error(parsed.Unix(), "!=", 1473152776)
  13. }
  14. }

All the examples I find, and even the default parser from the Go time package seem to work that way...

答案1

得分: 1

哇,我在这一行中写错了类型:

  1. parsed := TimeWithoutZone{}

应该是

  1. parsed := TimeTestObj{}

...

英文:

Wow I just have the wrong type in this line:

  1. parsed := TimeWithoutZone{}

must be

  1. parsed := TimeTestObj{}

...

huangapple
  • 本文由 发表于 2016年9月6日 17:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/39345921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定