internal/jsonrpc2: omit empty error data

The spec states error data may be omitted. It is currently always encoded
as null, despite having no usage.

Omit the field if empty, and add a test to prove the behaviour.

Fixes golang/go#39736

Change-Id: Icdb39409010f3a42f84d2372c2061e4bc7cc198e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/239059
Reviewed-by: Ian Cottrell <iancottrell@google.com>
diff --git a/internal/jsonrpc2/wire.go b/internal/jsonrpc2/wire.go
index cfef6c2..6593023 100644
--- a/internal/jsonrpc2/wire.go
+++ b/internal/jsonrpc2/wire.go
@@ -81,7 +81,7 @@
 	// Message is a short description of the error.
 	Message string `json:"message"`
 	// Data is optional structured data containing additional information about the error.
-	Data *json.RawMessage `json:"data"`
+	Data *json.RawMessage `json:"data,omitempty"`
 }
 
 // wireVersionTag is a special 0 sized struct that encodes as the jsonrpc version
diff --git a/internal/jsonrpc2/wire_test.go b/internal/jsonrpc2/wire_test.go
index 4d8832c..ff62de0 100644
--- a/internal/jsonrpc2/wire_test.go
+++ b/internal/jsonrpc2/wire_test.go
@@ -81,6 +81,17 @@
 	}
 }
 
+func TestErrorEncode(t *testing.T) {
+	b, err := json.Marshal(jsonrpc2.NewError(0, ""))
+	if err != nil {
+		t.Fatal(err)
+	}
+	checkJSON(t, b, []byte(`{
+		"code": 0,
+		"message": ""
+	}`))
+}
+
 func TestErrorResponse(t *testing.T) {
 	// originally reported in #39719, this checks that result is not present if
 	// it is an error response
@@ -93,8 +104,7 @@
 		"jsonrpc":"2.0",
 		"error":{
 			"code":0,
-			"message":"computing fix edits",
-			"data":null
+			"message":"computing fix edits"
 		},
 		"id":3
 	}`))