Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homogenize treatment of params and meta in UnmarshalJSON #52

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down