Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-cid
Commits
10a4d040
Unverified
Commit
10a4d040
authored
Jul 12, 2018
by
Steven Allen
Committed by
GitHub
Jul 12, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #55 from ipfs/bug/marshal
Use value receiver for MarshalJSON so we can marshal values
parents
5b04f304
d204c18f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
1 deletion
+30
-1
cid.go
cid.go
+1
-1
cid_test.go
cid_test.go
+29
-0
No files found.
cid.go
View file @
10a4d040
...
...
@@ -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
}
...
...
cid_test.go
View file @
10a4d040
...
...
@@ -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"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment