Skip to content

Commit 56bc19b

Browse files
Merge pull request #3507 from ipfs/fix/pbnode-json
merkledag: fix json marshalling of pbnode
2 parents f6948bb + 7c8a8a8 commit 56bc19b

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

merkledag/coding.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ func (n *ProtoNode) getPBNode() *pb.PBNode {
6060
pbn.Links[i] = &pb.PBLink{}
6161
pbn.Links[i].Name = &l.Name
6262
pbn.Links[i].Tsize = &l.Size
63-
pbn.Links[i].Hash = l.Cid.Bytes()
63+
if l.Cid != nil {
64+
pbn.Links[i].Hash = l.Cid.Bytes()
65+
}
6466
}
6567

6668
if len(n.data) > 0 {

merkledag/node.go

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package merkledag
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

78
node "gx/ipfs/QmRSU5EqqWVZSNdbU51yXmVoF1uNw3JgTNB6RaiL7DZM16/go-ipld-node"
@@ -228,6 +229,31 @@ func (n *ProtoNode) Loggable() map[string]interface{} {
228229
}
229230
}
230231

232+
func (n *ProtoNode) UnmarshalJSON(b []byte) error {
233+
s := struct {
234+
Data []byte `json:"data"`
235+
Links []*node.Link `json:"links"`
236+
}{}
237+
238+
err := json.Unmarshal(b, &s)
239+
if err != nil {
240+
return err
241+
}
242+
243+
n.data = s.Data
244+
n.links = s.Links
245+
return nil
246+
}
247+
248+
func (n *ProtoNode) MarshalJSON() ([]byte, error) {
249+
out := map[string]interface{}{
250+
"data": n.data,
251+
"links": n.links,
252+
}
253+
254+
return json.Marshal(out)
255+
}
256+
231257
func (n *ProtoNode) Cid() *cid.Cid {
232258
if n.encoded != nil && n.cached != nil {
233259
return n.cached

merkledag/node_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package merkledag_test
22

33
import (
4+
"bytes"
45
"context"
56
"testing"
67

@@ -128,3 +129,32 @@ func TestNodeCopy(t *testing.T) {
128129
t.Fatal("should be different objects")
129130
}
130131
}
132+
133+
func TestJsonRoundtrip(t *testing.T) {
134+
nd := new(ProtoNode)
135+
nd.SetLinks([]*node.Link{
136+
{Name: "a"},
137+
{Name: "c"},
138+
{Name: "b"},
139+
})
140+
nd.SetData([]byte("testing"))
141+
142+
jb, err := nd.MarshalJSON()
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
147+
nn := new(ProtoNode)
148+
err = nn.UnmarshalJSON(jb)
149+
if err != nil {
150+
t.Fatal(err)
151+
}
152+
153+
if !bytes.Equal(nn.Data(), nd.Data()) {
154+
t.Fatal("data wasnt the same")
155+
}
156+
157+
if !nn.Cid().Equals(nd.Cid()) {
158+
t.Fatal("objects differed after marshaling")
159+
}
160+
}

test/sharness/t0053-dag.sh

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ test_dag_cmd() {
4646
test_cmp file2 out2 &&
4747
test_cmp file3 out3
4848
'
49+
50+
test_expect_success "add a normal file" '
51+
HASH=$(echo "foobar" | ipfs add -q)
52+
'
53+
54+
test_expect_success "can view protobuf object with dag get" '
55+
ipfs dag get $HASH > dag_get_pb_out
56+
'
57+
58+
test_expect_success "output looks correct" '
59+
echo "{\"data\":\"CAISB2Zvb2JhcgoYBw==\",\"links\":[]}" > dag_get_pb_exp &&
60+
test_cmp dag_get_pb_exp dag_get_pb_out
61+
'
4962
}
5063

5164
# should work offline

0 commit comments

Comments
 (0)