diff --git a/todoist/id.go b/todoist/id.go index ae33e01..d7080f9 100644 --- a/todoist/id.go +++ b/todoist/id.go @@ -34,6 +34,9 @@ func (i ID) MarshalJSON() ([]byte, error) { if IsTempID(i) { s = `"` + s + `"` } + if s == "0" { + s = "null" + } return []byte(s), nil } @@ -42,6 +45,9 @@ func (i *ID) UnmarshalJSON(b []byte) (err error) { if err != nil { s = string(b) // integer id } + if s == "null" { + s = "0" + } id, err := NewID(s) if err != nil { return err diff --git a/todoist/id_test.go b/todoist/id_test.go index 02fed1e..3341a1e 100644 --- a/todoist/id_test.go +++ b/todoist/id_test.go @@ -52,6 +52,11 @@ func TestID_MarshalJSON(t *testing.T) { if err != nil || string(b) != strconv.Quote(test.s) { t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b)) } + + b, err = ID("0").MarshalJSON() + if err != nil || string(b) != "null" { + t.Errorf("Expect %s, but got %s", strconv.Quote(test.s), string(b)) + } } func TestID_UnmarshalJSON(t *testing.T) { @@ -61,9 +66,17 @@ func TestID_UnmarshalJSON(t *testing.T) { if !reflect.DeepEqual(err, test.e) { t.Errorf("Expect %s, but got %s", test.e, err) } else if test.e == nil && v != test.v { - + t.Errorf("Expect %s, but got %s", test.v, v) } } + var v ID + err := v.UnmarshalJSON([]byte("null")) + if err != nil { + t.Errorf("Unexpect error: %s", err) + } + if v != ID("0") { + t.Errorf("Expect %s, but got %s", ID("0"), v) + } } func TestIsTempID(t *testing.T) {