diff --git a/cid.go b/cid.go index f04c95d..ececb26 100644 --- a/cid.go +++ b/cid.go @@ -404,10 +404,15 @@ func (c *Cid) UnmarshalJSON(b []byte) error { obj := struct { CidTarget string `json:"/"` }{} - err := json.Unmarshal(b, &obj) + objptr := &obj + err := json.Unmarshal(b, &objptr) if err != nil { return err } + if objptr == nil { + *c = Cid{} + return nil + } if obj.CidTarget == "" { return fmt.Errorf("cid was incorrectly formatted") @@ -430,6 +435,9 @@ 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) { + if !c.Defined() { + return []byte("null"), nil + } return []byte(fmt.Sprintf("{\"/\":\"%s\"}", c.String())), nil }