Unverified Commit 10a4d040 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #55 from ipfs/bug/marshal

Use value receiver for MarshalJSON so we can marshal values
parents 5b04f304 d204c18f
......@@ -410,7 +410,7 @@ func (c *Cid) UnmarshalJSON(b []byte) error {
//
// Note that this formatting comes from the IPLD specification
// (https://github.com/ipld/specs/tree/master/ipld)
func (c *Cid) MarshalJSON() ([]byte, error) {
func (c Cid) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("{\"/\":\"%s\"}", c.String())), nil
}
......
......@@ -383,3 +383,32 @@ func TestFromJson(t *testing.T) {
t.Fatal("json parsing failed")
}
}
func TestJsonRoundTrip(t *testing.T) {
exp, err := Decode("zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG")
if err != nil {
t.Fatal(err)
}
// Verify it works for a *Cid.
enc, err := json.Marshal(exp)
if err != nil {
t.Fatal(err)
}
var actual Cid
err = json.Unmarshal(enc, &actual)
if !exp.Equals(&actual) {
t.Fatal("cids not equal for *Cid")
}
// Verify it works for a Cid.
enc, err = json.Marshal(*exp)
if err != nil {
t.Fatal(err)
}
var actual2 Cid
err = json.Unmarshal(enc, &actual2)
if !exp.Equals(&actual2) {
t.Fatal("cids not equal for Cid")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment