Skip to content

Commit b0ce36a

Browse files
LesnyRumcajsrvagg
andcommitted
more concise marshal
Co-authored-by: Rod Vagg <[email protected]>
1 parent 85430f7 commit b0ce36a

File tree

1 file changed

+7
-22
lines changed

1 file changed

+7
-22
lines changed

Diff for: handler.go

+7-22
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,15 @@ func (r response) MarshalJSON() ([]byte, error) {
119119
// > `error`:
120120
// > This member is REQUIRED on error.
121121
// > This member MUST NOT exist if there was no error triggered during invocation.
122+
data := make(map[string]interface{})
123+
data["jsonrpc"] = r.Jsonrpc
124+
data["id"] = r.ID
122125
if r.Error != nil {
123-
// If there's an error, exclude result
124-
type responseWithoutResult struct {
125-
Jsonrpc string `json:"jsonrpc"`
126-
ID interface{} `json:"id"`
127-
Error *respError `json:"error"`
128-
}
129-
return json.Marshal(&responseWithoutResult{
130-
Jsonrpc: r.Jsonrpc,
131-
ID: r.ID,
132-
Error: r.Error,
133-
})
134-
}
135-
136-
type responseWithResult struct {
137-
Jsonrpc string `json:"jsonrpc"`
138-
Result interface{} `json:"result"`
139-
ID interface{} `json:"id"`
126+
data["error"] = r.Error
127+
} else {
128+
data["result"] = r.Result
140129
}
141-
return json.Marshal(&responseWithResult{
142-
Jsonrpc: r.Jsonrpc,
143-
Result: r.Result,
144-
ID: r.ID,
145-
})
130+
return json.Marshal(data)
146131
}
147132

148133
type handler struct {

0 commit comments

Comments
 (0)