Skip to content

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

Description

@arijitAD

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions