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

json: cannot unmarshal string into Go value of type multiaddr.Multiaddr #189

Closed
mg98 opened this issue Jan 20, 2023 · 3 comments
Closed

Comments

@mg98
Copy link

mg98 commented Jan 20, 2023

I'm having trouble to unmarshal JSONs to structs that include Multiaddr fields.

json: cannot unmarshal string into Go struct field .a of type multiaddr.Multiaddr

var d struct {
	A ma.Multiaddr `json:"a"`
}
if err := json.Unmarshal([]byte(`{"a":"/ip4/64.227.126.216/tcp/4001"}`), &d); err != nil {
	panic(err)
}

Go Playground

I think it can be further minimized to unmarshaling only the string itself:

var a ma.Multiaddr
b := []byte(`"/ip4/64.227.126.216/tcp/4001"`)
if err := json.Unmarshal(b, &a); err != nil {
	panic(err)
}

Here I'm getting json: cannot unmarshal string into Go value of type multiaddr.Multiaddr.

I see implementations of json marshaling in ma.Multiaddr, so I wonder if I'm doing something wrong?

@mg98
Copy link
Author

mg98 commented Jan 22, 2023

Hmm I see already, it must be because ma.Multiaddr is an interface and the exact type is not known.

@mg98 mg98 closed this as completed Jan 22, 2023
@mg98
Copy link
Author

mg98 commented Jan 26, 2023

Would like to reopen this... I highly advocate getting rid of the interface here and just sticking with a struct.
Also found in relation to this issue: #100

@mg98 mg98 reopened this Jan 26, 2023
@marten-seemann
Copy link
Contributor

👋 libp2p maintainer here.

I suggest closing this issue, since this seems to be exactly what #100 is about.

The core team won't have any capacity to resolve this issue any time soon, but we're happy to review a proposal and a PR. @mg98, would you mind commenting on #100 how exactly you'd like to solve this, so we can get the discussion going?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants