Compare commits

..

2 Commits

Author SHA1 Message Date
Jeromy
a22bf1e2bf gx publish 0.7.11 2017-02-05 23:52:16 -08:00
Jeromy
7bcaf9264a do cid json handling in this package 2017-02-05 23:52:06 -08:00
4 changed files with 32 additions and 4 deletions

View File

@@ -1 +1 @@
0.7.10: QmTau856czj6wc5UyKQX2MfBQZ9iCZPsuUsVW2b2pRtLVp
0.7.11: QmV5gPoRsjN1Gid3LMdNZTyfCtP2DsvqEbMAmz82RmmiGk

17
cid.go
View File

@@ -3,6 +3,7 @@ package cid
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"strings"
@@ -209,7 +210,19 @@ func (c *Cid) UnmarshalJSON(b []byte) error {
if len(b) < 2 {
return fmt.Errorf("invalid cid json blob")
}
out, err := Decode(string(b[1 : len(b)-1]))
obj := struct {
CidTarget string `json:"/"`
}{}
err := json.Unmarshal(b, &obj)
if err != nil {
return err
}
if obj.CidTarget == "" {
return fmt.Errorf("cid was incorrectly formatted")
}
out, err := Decode(obj.CidTarget)
if err != nil {
return err
}
@@ -221,7 +234,7 @@ func (c *Cid) UnmarshalJSON(b []byte) error {
}
func (c *Cid) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("\"%s\"", c.String())), nil
return []byte(fmt.Sprintf("{\"/\":\"%s\"}", c.String())), nil
}
func (c *Cid) KeyString() string {

View File

@@ -2,6 +2,7 @@ package cid
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"strings"
@@ -200,3 +201,17 @@ func TestHexDecode(t *testing.T) {
t.Fatal("hash value failed to round trip decoding from hex")
}
}
func TestFromJson(t *testing.T) {
cval := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG"
jsoncid := []byte(`{"/":"` + cval + `"}`)
var c Cid
err := json.Unmarshal(jsoncid, &c)
if err != nil {
t.Fatal(err)
}
if c.String() != cval {
t.Fatal("json parsing failed")
}
}

View File

@@ -25,6 +25,6 @@
"license": "MIT",
"name": "go-cid",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.7.10"
"version": "0.7.11"
}