diff --git a/types/json.go b/types/json.go index 2f953d525..1fc8b7970 100644 --- a/types/json.go +++ b/types/json.go @@ -46,6 +46,9 @@ func (j *JSON) UnmarshalJSON(data []byte) error { // MarshalJSON returns j as the JSON encoding of j. func (j JSON) MarshalJSON() ([]byte, error) { + if j == nil { + return []byte("null"), nil + } return j, nil } diff --git a/types/json_test.go b/types/json_test.go index c5c959224..542a43ac5 100644 --- a/types/json_test.go +++ b/types/json_test.go @@ -76,6 +76,20 @@ func TestJSONUnmarshalJSON(t *testing.T) { } } +func TestJSONMarshalJSON_Null(t *testing.T) { + t.Parallel() + + var j JSON + res, err := j.MarshalJSON() + if err != nil { + t.Error(err) + } + + if !bytes.Equal(res, []byte(`null`)) { + t.Errorf("Expected %q, got %v", `null`, res) + } +} + func TestJSONMarshalJSON(t *testing.T) { t.Parallel()