json Encode fails on links

parent 5ab5068c
......@@ -59,7 +59,7 @@ func ExampleCreatingViaADL() {
// To marshal the ADL, just use marshal methods on its substrate as normal:
var marshalBuffer bytes.Buffer
err := dagjson.Marshal(substrateNode, json.NewEncoder(&marshalBuffer, json.EncodeOptions{}))
err := dagjson.Marshal(substrateNode, json.NewEncoder(&marshalBuffer, json.EncodeOptions{}), true)
fmt.Printf("marshalled: %v\n", marshalBuffer.String())
fmt.Printf("marshal error: %v\n", err)
......
......@@ -14,7 +14,7 @@ import (
// except for the `case ipld.Kind_Link` block,
// which is dag-json's special sauce for schemafree links.
func Marshal(n ipld.Node, sink shared.TokenSink) error {
func Marshal(n ipld.Node, sink shared.TokenSink, allowLinks bool) error {
var tk tok.Token
switch n.Kind() {
case ipld.Kind_Invalid:
......@@ -44,7 +44,7 @@ func Marshal(n ipld.Node, sink shared.TokenSink) error {
if _, err := sink.Step(&tk); err != nil {
return err
}
if err := Marshal(v, sink); err != nil {
if err := Marshal(v, sink, allowLinks); err != nil {
return err
}
}
......@@ -66,7 +66,7 @@ func Marshal(n ipld.Node, sink shared.TokenSink) error {
if err != nil {
return err
}
if err := Marshal(v, sink); err != nil {
if err := Marshal(v, sink, allowLinks); err != nil {
return err
}
}
......@@ -120,6 +120,9 @@ func Marshal(n ipld.Node, sink shared.TokenSink) error {
_, err = sink.Step(&tk)
return err
case ipld.Kind_Link:
if !allowLinks {
return fmt.Errorf("cannot Marshal ipld links to JSON")
}
v, err := n.AsLink()
if err != nil {
return err
......
......@@ -56,5 +56,5 @@ func Encode(n ipld.Node, w io.Writer) error {
return Marshal(n, json.NewEncoder(w, json.EncodeOptions{
Line: []byte{'\n'},
Indent: []byte{'\t'},
}))
}), true)
}
......@@ -59,5 +59,5 @@ func Encode(n ipld.Node, w io.Writer) error {
return dagjson.Marshal(n, rfmtjson.NewEncoder(w, rfmtjson.EncodeOptions{
Line: []byte{'\n'},
Indent: []byte{'\t'},
}))
}), false)
}
......@@ -168,8 +168,8 @@ func (tab *table) Finalize() {
var buf bytes.Buffer
for _, cn := range cols {
buf.Reset()
dagjson.Marshal(basicnode.NewString(string(cn)), json.NewEncoder(&buf, json.EncodeOptions{})) // FIXME this would be a lot less irritating if we had more plumbing access to the json encoding -- we want to encode exactly one string into a buffer, it literally can't error.
tab.keySize[cn] = buf.Len() // FIXME this is ignoring charsets, renderable glyphs, etc at present.
dagjson.Marshal(basicnode.NewString(string(cn)), json.NewEncoder(&buf, json.EncodeOptions{}), false) // FIXME this would be a lot less irritating if we had more plumbing access to the json encoding -- we want to encode exactly one string into a buffer, it literally can't error.
tab.keySize[cn] = buf.Len() // FIXME this is ignoring charsets, renderable glyphs, etc at present.
}
}
......@@ -299,7 +299,7 @@ func marshal(ctx *state, n ipld.Node, w io.Writer) error {
func marshalPlain(ctx *state, n ipld.Node, w io.Writer) error {
err := dagjson.Marshal(n, json.NewEncoder(w, json.EncodeOptions{
// never indent here: these values will always end up being emitted mid-line.
}))
}), true)
if err != nil {
return recordErrorPosition(ctx, err)
}
......@@ -470,7 +470,7 @@ func emitKey(ctx *state, k ipld.Node, w io.Writer) error {
if ctx.cfg.Color.Enabled {
w.Write(ctx.cfg.Color.KeyHighlight)
}
if err := dagjson.Marshal(k, json.NewEncoder(w, json.EncodeOptions{})); err != nil {
if err := dagjson.Marshal(k, json.NewEncoder(w, json.EncodeOptions{}), true); err != nil {
return recordErrorPosition(ctx, err)
}
if ctx.cfg.Color.Enabled {
......
......@@ -209,7 +209,7 @@ func testMarshal(t *testing.T, n ipld.Node, data string) {
// We'll marshal with "pretty" linebreaks and indents (and re-format the fixture to the same) for better diffing.
prettyprint := json.EncodeOptions{Line: []byte{'\n'}, Indent: []byte{'\t'}}
var buf bytes.Buffer
err := dagjson.Marshal(n, json.NewEncoder(&buf, prettyprint))
err := dagjson.Marshal(n, json.NewEncoder(&buf, prettyprint), true)
if err != nil {
t.Errorf("marshal failed: %s", err)
}
......
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