mirror of
https://github.com/sourcegraph/jsonrpc2.git
synced 2026-06-16 04:04:56 +02:00
Homogenize treatment of params and meta in UnmarshalJSON
This change makes the treatment of params and meta the same, by assigning a well-known pointer at first to detect if the unmarshaling process overwrites it with an explicit nil, or it stays the same in which it means that it was unset from the beginning.
This commit is contained in:
parent
120d461fd1
commit
e4d4be050e
1 changed files with 11 additions and 5 deletions
16
jsonrpc2.go
16
jsonrpc2.go
|
|
@ -85,10 +85,12 @@ func (r Request) MarshalJSON() ([]byte, error) {
|
|||
func (r *Request) UnmarshalJSON(data []byte) error {
|
||||
r2 := make(map[string]interface{})
|
||||
|
||||
// Detect if the "params" field is JSON "null" or just not present
|
||||
// by seeing if the field gets overwritten to nil.
|
||||
// Detect if the "params" or "meta" fields are JSON "null" or just not
|
||||
// present by seeing if the field gets overwritten to nil.
|
||||
emptyParams := &json.RawMessage{}
|
||||
r2["params"] = emptyParams
|
||||
emptyMeta := &json.RawMessage{}
|
||||
r2["meta"] = emptyMeta
|
||||
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
|
|
@ -112,9 +114,13 @@ func (r *Request) UnmarshalJSON(data []byte) error {
|
|||
}
|
||||
r.Params = (*json.RawMessage)(&b)
|
||||
}
|
||||
meta, ok := r2["meta"]
|
||||
if ok {
|
||||
b, err := json.Marshal(meta)
|
||||
switch {
|
||||
case r2["meta"] == nil:
|
||||
r.Meta = &jsonNull
|
||||
case r2["meta"] == emptyMeta:
|
||||
r.Meta = nil
|
||||
default:
|
||||
b, err := json.Marshal(r2["meta"])
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal Meta: %w", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue