-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
PeerInfo UnMarshal Error #393
Comments
We haven't defined a way to marshal/unmarshal peer IDs to/from JSON (although we probably should). |
This is just adding |
I think it's time to close this issue, func (pi *PeerInfo) MarshalJSON() ([]byte, error) {
out := make(map[string]interface{})
out["ID"] = pi.ID.Pretty()
var addrs []string
for _, a := range pi.Addrs {
addrs = append(addrs, a.String())
}
out["Addrs"] = addrs
return json.Marshal(out)
}
func (pi *PeerInfo) UnmarshalJSON(b []byte) error {
var data map[string]interface{}
err := json.Unmarshal(b, &data)
if err != nil {
return err
}
pid, err := peer.IDB58Decode(data["ID"].(string))
if err != nil {
return err
}
pi.ID = pid
addrs, ok := data["Addrs"].([]interface{})
if ok {
for _, a := range addrs {
pi.Addrs = append(pi.Addrs, ma.StringCast(a.(string)))
}
}
return nil
} |
Hm. Actually, I was wrong. The code in the top post should work. |
@Stebalien I figure it out. The reason is pi := host.Peerstore().PeerInfo(host.ID())
data, err := json.Marshal(&pi) |
I see. We should just change it to take a value. Also, we should change it to not panic on bad values. The current code is buggy as hell. |
@Stebalien I made a pull request. Now |
@Harrywang55666 I think it's time to close this issue. |
after running result:
The text was updated successfully, but these errors were encountered: