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

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

Open
arijitAD opened this issue Dec 1, 2020 · 0 comments
Open

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

arijitAD opened this issue Dec 1, 2020 · 0 comments
Labels

Comments

@arijitAD
Copy link

arijitAD commented Dec 1, 2020

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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

1 participant