gorilla/rpc

[bug] Unmarshaling arrays items to struct for json rpc 2.

arijitAD opened this issue · 0 comments

Describe the bug
Unmarshaling arrays items to a request struct for JSON RPC 2 doesn't work.

params := [1]interface{}{args}

Here since args is passed as a pointer it fails to Unmarshall.

Versions
27d3316

Steps to Reproduce
Added an example code.

Expected behavior
When a JSON RPC v2 request is sent like this. It should be parsed to the corresponding request struct.
Request: ["Hello world", 10, false]
After Parsing: main.Notification{Message:"Hello world", Priority:0xa, Critical:false}

Code Snippets

package main

import (
	"encoding/json"
	"fmt"
	"log"
)

type Notification struct {
	Message  string
	Priority uint8
	Critical bool
}

func UnmarshalJSONCustom(buf []byte, n interface{}) error {
	tmp := []interface{}{n}
	if err := json.Unmarshal(buf, &tmp); err != nil {
		return err
	}
	fmt.Println(tmp)
	return nil
}

func main() {
	input := `["Hello world", 10, false]`
	var n Notification
	if err := UnmarshalJSONCustom([]byte(input), n); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%#v\n", n)
}