multiformats/js-multiaddr

Add toJSON?

alanshaw opened this issue · 5 comments

If I JSON.stringify(multiaddr('/ip4/127.0.0.1')) I get "{\"buffer\":{\"type\":\"Buffer\",\"data\":[4,127,0,0,1]}}" should it be '/ip4/127.0.0.1' instead?

isn't that what you get from toString()?

yes, should the JSON representation be the same?

Not sure. JSON can provide more information than a String, i.e it could have a toJSON that already divided the multiaddr into tuples

There's no info missing in the string representation though...other than "I am a multiaddr", but neither representation says this atm.

Right now JSON.parse(JSON.stringify(multiaddr('/ip4/127.0.0.1'))) is not a valid multiaddr (not machine or human readable), but if toJSON returned '/ip4/127.0.0.1' then it would be a valid, human readable multiaddr, and I could also do this:

multiaddr(JSON.parse(JSON.stringify(multiaddr('/ip4/127.0.0.1'))))

Of note, if you give a multiaddr to IPFS config then bad things happen because a stringified multiaddr is saved to a JSON config file and read out as something it's not.

new IPFS({
  config: {
    Addresses: { Swarm: [multiaddr('...')] }
  }
})

Added in 6.0.6