diff --git a/transport/http/jsonrpc/request_response_types.go b/transport/http/jsonrpc/request_response_types.go index b4752baa1..1c6630bc8 100644 --- a/transport/http/jsonrpc/request_response_types.go +++ b/transport/http/jsonrpc/request_response_types.go @@ -35,6 +35,16 @@ func (id *RequestID) UnmarshalJSON(b []byte) error { return nil } +func (id *RequestID) MarshalJSON() ([]byte, error) { + if id.intError == nil { + return json.Marshal(id.intValue) + } else if id.floatError == nil { + return json.Marshal(id.floatValue) + } else { + return json.Marshal(id.stringValue) + } +} + // Int returns the ID as an integer value. // An error is returned if the ID can't be treated as an int. func (id *RequestID) Int() (int, error) { @@ -59,6 +69,7 @@ type Response struct { JSONRPC string `json:"jsonrpc"` Result json.RawMessage `json:"result,omitempty"` Error *Error `json:"error,omitempty"` + ID *RequestID `json:"id"` } const ( diff --git a/transport/http/jsonrpc/request_response_types_test.go b/transport/http/jsonrpc/request_response_types_test.go index 4f4abf3d6..8eb03ee93 100644 --- a/transport/http/jsonrpc/request_response_types_test.go +++ b/transport/http/jsonrpc/request_response_types_test.go @@ -109,3 +109,30 @@ func TestCanUnmarshalNullID(t *testing.T) { t.Fatalf("Expected ID to be nil, got %+v.\n", r.ID) } } + +func TestCanMarshalID(t *testing.T) { + cases := []struct { + JSON string + expType string + expValue interface{} + }{ + {`12345`, "int", 12345}, + {`12345.6`, "float", 12345.6}, + {`"stringaling"`, "string", "stringaling"}, + {`null`, "null", nil}, + } + + for _, c := range cases { + req := jsonrpc.Request{} + JSON := fmt.Sprintf(`{"jsonrpc":"2.0","id":%s}`, c.JSON) + json.Unmarshal([]byte(JSON), &req) + resp := jsonrpc.Response{ID: req.ID, JSONRPC: req.JSONRPC} + + want := JSON + bol, _ := json.Marshal(resp) + got := string(bol) + if got != want { + t.Fatalf("'%s': want %s, got %s.", c.expType, want, got) + } + } +} diff --git a/transport/http/jsonrpc/server.go b/transport/http/jsonrpc/server.go index 1b49fe7b5..0310f3edd 100644 --- a/transport/http/jsonrpc/server.go +++ b/transport/http/jsonrpc/server.go @@ -136,6 +136,7 @@ func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } res := Response{ + ID: req.ID, JSONRPC: Version, }