mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-19 05:30:03 +02:00
better null handling in (Request).params and (Response).result (#7)
This makes the MarshalJSON/UnmarshalJSON methods centralize the logic, instead of requiring callers to occasionally munge responses to make them valid to later JSON-marshal.
This commit is contained in:
parent
6e88a787fb
commit
cffa092597
2 changed files with 114 additions and 27 deletions
49
jsonrpc2.go
49
jsonrpc2.go
|
|
@ -43,10 +43,7 @@ type Request struct {
|
|||
|
||||
// MarshalJSON implements json.Marshaler and adds the "jsonrpc":"2.0"
|
||||
// property.
|
||||
func (r *Request) MarshalJSON() ([]byte, error) {
|
||||
if r == nil {
|
||||
return nil, errors.New("can't marshal nil *jsonrpc2.Request")
|
||||
}
|
||||
func (r Request) MarshalJSON() ([]byte, error) {
|
||||
r2 := struct {
|
||||
Method string `json:"method"`
|
||||
Params *json.RawMessage `json:"params,omitempty"`
|
||||
|
|
@ -73,11 +70,22 @@ func (r *Request) UnmarshalJSON(data []byte) error {
|
|||
Meta *json.RawMessage `json:"meta,omitempty"`
|
||||
ID *ID `json:"id"`
|
||||
}
|
||||
|
||||
// Detect if the "params" field is JSON "null" or just not present
|
||||
// by seeing if the field gets overwritten to nil.
|
||||
r2.Params = &json.RawMessage{}
|
||||
|
||||
if err := json.Unmarshal(data, &r2); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Method = r2.Method
|
||||
r.Params = r2.Params
|
||||
if r2.Params == nil {
|
||||
r.Params = &jsonNull
|
||||
} else if len(*r2.Params) == 0 {
|
||||
r.Params = nil
|
||||
} else {
|
||||
r.Params = r2.Params
|
||||
}
|
||||
r.Meta = r2.Meta
|
||||
if r2.ID == nil {
|
||||
r.ID = ID{}
|
||||
|
|
@ -115,7 +123,7 @@ func (r *Request) SetMeta(v interface{}) error {
|
|||
// http://www.jsonrpc.org/specification#response_object.
|
||||
type Response struct {
|
||||
ID ID `json:"id"`
|
||||
Result *json.RawMessage `json:"result,omitempty"`
|
||||
Result *json.RawMessage `json:"result"`
|
||||
Error *Error `json:"error,omitempty"`
|
||||
|
||||
// SPEC NOTE: The spec says "If there was an error in detecting
|
||||
|
|
@ -128,14 +136,12 @@ type Response struct {
|
|||
|
||||
// MarshalJSON implements json.Marshaler and adds the "jsonrpc":"2.0"
|
||||
// property.
|
||||
func (r *Response) MarshalJSON() ([]byte, error) {
|
||||
if r == nil {
|
||||
return nil, errors.New("can't marshal nil *jsonrpc2.Response")
|
||||
}
|
||||
func (r Response) MarshalJSON() ([]byte, error) {
|
||||
if (r.Result == nil || len(*r.Result) == 0) && r.Error == nil {
|
||||
return nil, errors.New("can't marshal *jsonrpc2.Response (must have result or error)")
|
||||
}
|
||||
b, err := json.Marshal(*r)
|
||||
type tmpType Response // avoid infinite MarshalJSON recursion
|
||||
b, err := json.Marshal(tmpType(r))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -143,6 +149,25 @@ func (r *Response) MarshalJSON() ([]byte, error) {
|
|||
return b, nil
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler.
|
||||
func (r *Response) UnmarshalJSON(data []byte) error {
|
||||
type tmpType Response
|
||||
|
||||
// Detect if the "result" field is JSON "null" or just not present
|
||||
// by seeing if the field gets overwritten to nil.
|
||||
*r = Response{Result: &json.RawMessage{}}
|
||||
|
||||
if err := json.Unmarshal(data, (*tmpType)(r)); err != nil {
|
||||
return err
|
||||
}
|
||||
if r.Result == nil { // JSON "null"
|
||||
r.Result = &jsonNull
|
||||
} else if len(*r.Result) == 0 {
|
||||
r.Result = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetResult sets r.Result to the JSON representation of v. If JSON
|
||||
// marshaling fails, it returns an error.
|
||||
func (r *Response) SetResult(v interface{}) error {
|
||||
|
|
@ -531,7 +556,7 @@ type anyMessage struct {
|
|||
response *Response
|
||||
}
|
||||
|
||||
func (m *anyMessage) MarshalJSON() ([]byte, error) {
|
||||
func (m anyMessage) MarshalJSON() ([]byte, error) {
|
||||
var v interface{}
|
||||
switch {
|
||||
case m.request != nil && m.response == nil:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue