diff --git a/response.go b/response.go index 971b2d9..06b73cf 100644 --- a/response.go +++ b/response.go @@ -159,6 +159,12 @@ func (r *Response) UnmarshalJSON(data []byte) error { return nil } + // If the result is null, Unmarshal won't work, so skip it. + if resultData == nil { + r.Result = nil + return nil + } + // Restore the userResult and finish unmarshaling. r.Result = userResult return json.Unmarshal(resultData, &r.Result) diff --git a/response_test.go b/response_test.go index 95bc5c0..ed1fd5c 100644 --- a/response_test.go +++ b/response_test.go @@ -41,6 +41,11 @@ var responseTests = []struct { Name: "bad version", Data: `{"result":"result"}`, Err: "invalid JSON-RPC 2.0 version", +}, { + Name: "null result", + Data: `{"jsonrpc":"2.0","result":null,"id":5}`, + Res: &Response{ID: 5, Result: nil}, + Result: nil, }} func TestResponse(t *testing.T) { @@ -57,4 +62,17 @@ func TestResponse(t *testing.T) { }) } }) + t.Run("UnmarshalJSON", func(t *testing.T) { + for _, test := range responseTests { + if test.Res == nil { + continue + } + t.Run(test.Name, func(t *testing.T) { + assert := assert.New(t) + err := test.Res.UnmarshalJSON([]byte(test.Data)) + assert.NoError(err) + assert.Equal(test.Result, test.Res.Result) + }) + } + }) }