diff --git a/codec/dagjson/marshal.go b/codec/dagjson/marshal.go index ea0b7a4bd6e3d41369b43cd9e3ffed41ce0f9e84..5335f3a1152fe5b87bec7dae7cfadcda70083321 100644 --- a/codec/dagjson/marshal.go +++ b/codec/dagjson/marshal.go @@ -16,7 +16,21 @@ 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, allowLinks bool) error { +type MarshalOptions struct { + // If true, will encode nodes with a Link kind using the DAG-JSON + // `{"/":"cid string"}` form. + EncodeLinks bool + + // If true, will encode nodes with a Bytes kind using the DAG-JSON + // `{"/":{"bytes":"base64 bytes..."}}` form. + EncodeBytes bool + + // If true, will sort map keys prior to encoding using plain bytewise + // comparison. + SortMapKeys bool +} + +func Marshal(n ipld.Node, sink shared.TokenSink, options MarshalOptions) error { var tk tok.Token switch n.Kind() { case ipld.Kind_Invalid: @@ -32,33 +46,54 @@ func Marshal(n ipld.Node, sink shared.TokenSink, allowLinks bool) error { if _, err := sink.Step(&tk); err != nil { return err } - // Collect map entries, then sort by key - type entry struct { - key string - value ipld.Node - } - entries := []entry{} - for itr := n.MapIterator(); !itr.Done(); { - k, v, err := itr.Next() - if err != nil { - return err - } - keyStr, err := k.AsString() - if err != nil { - return err - } - entries = append(entries, entry{keyStr, v}) - } - sort.Slice(entries, func(i, j int) bool { return entries[i].key < entries[j].key }) - // Emit map contents (and recurse). - for _, e := range entries { - tk.Type = tok.TString - tk.Str = e.key - if _, err := sink.Step(&tk); err != nil { - return err + if options.SortMapKeys { + // Collect map entries, then sort by key + type entry struct { + key string + value ipld.Node + } + entries := []entry{} + for itr := n.MapIterator(); !itr.Done(); { + k, v, err := itr.Next() + if err != nil { + return err + } + keyStr, err := k.AsString() + if err != nil { + return err + } + entries = append(entries, entry{keyStr, v}) + } + sort.Slice(entries, func(i, j int) bool { return entries[i].key < entries[j].key }) + // Emit map contents (and recurse). + for _, e := range entries { + tk.Type = tok.TString + tk.Str = e.key + if _, err := sink.Step(&tk); err != nil { + return err + } + if err := Marshal(e.value, sink, options); err != nil { + return err + } } - if err := Marshal(e.value, sink, allowLinks); err != nil { - return err + } else { + // Don't sort map, emit map contents (and recurse). + for itr := n.MapIterator(); !itr.Done(); { + k, v, err := itr.Next() + if err != nil { + return err + } + tk.Type = tok.TString + tk.Str, err = k.AsString() + if err != nil { + return err + } + if _, err := sink.Step(&tk); err != nil { + return err + } + if err := Marshal(v, sink, options); err != nil { + return err + } } } // Emit map close. @@ -79,7 +114,7 @@ func Marshal(n ipld.Node, sink shared.TokenSink, allowLinks bool) error { if err != nil { return err } - if err := Marshal(v, sink, allowLinks); err != nil { + if err := Marshal(v, sink, options); err != nil { return err } } @@ -128,7 +163,7 @@ func Marshal(n ipld.Node, sink shared.TokenSink, allowLinks bool) error { if err != nil { return err } - if allowLinks { + if options.EncodeBytes { // Precisely seven tokens to emit: tk.Type = tok.TMapOpen tk.Length = 1 @@ -170,7 +205,7 @@ func Marshal(n ipld.Node, sink shared.TokenSink, allowLinks bool) error { return err } case ipld.Kind_Link: - if !allowLinks { + if !options.EncodeLinks { return fmt.Errorf("cannot Marshal ipld links to JSON") } v, err := n.AsLink() diff --git a/codec/dagjson/multicodec.go b/codec/dagjson/multicodec.go index 2b8f413af34a18c125ba88c30de6146fb0b8e560..232d5fe9bc64719e90c8ce25909e36830b4a0abc 100644 --- a/codec/dagjson/multicodec.go +++ b/codec/dagjson/multicodec.go @@ -21,7 +21,10 @@ func init() { } func Decode(na ipld.NodeAssembler, r io.Reader) error { - err := Unmarshal(na, json.NewDecoder(r), true) + err := Unmarshal(na, json.NewDecoder(r), UnmarshalOptions{ + ParseLinks: true, + ParseBytes: true, + }) if err != nil { return err } @@ -53,5 +56,10 @@ func Encode(n ipld.Node, w io.Writer) error { // Shell out directly to generic inspection path. // (There's not really any fastpaths of note for json.) // Write another function if you need to tune encoding options about whitespace. - return Marshal(n, json.NewEncoder(w, json.EncodeOptions{}), true) + return Marshal(n, json.NewEncoder(w, json.EncodeOptions{}), + MarshalOptions{ + EncodeLinks: true, + EncodeBytes: true, + SortMapKeys: true, + }) } diff --git a/codec/dagjson/unmarshal.go b/codec/dagjson/unmarshal.go index b4d157103dd5272030f83530ed1e889e8807ccf8..913d245ed2fd1c4fffe6e390d75439ff567119df 100644 --- a/codec/dagjson/unmarshal.go +++ b/codec/dagjson/unmarshal.go @@ -20,9 +20,19 @@ import ( // several steps of handling maps, because it necessitates peeking several // tokens before deciding what kind of value to create). -func Unmarshal(na ipld.NodeAssembler, tokSrc shared.TokenSource, parseLinks bool) error { +type UnmarshalOptions struct { + // If true, parse DAG-CBOR `{"/":"cid string"}` as a Link kind node rather + // than a plain map + ParseLinks bool + + // If true, parse DAG-CBOR `{"/":{"bytes":"base64 bytes..."}}` as a Bytes kind + // node rather than nested plain maps + ParseBytes bool +} + +func Unmarshal(na ipld.NodeAssembler, tokSrc shared.TokenSource, options UnmarshalOptions) error { var st unmarshalState - st.parseLinks = parseLinks + st.options = options done, err := tokSrc.Step(&st.tk[0]) if err != nil { return err @@ -34,9 +44,9 @@ func Unmarshal(na ipld.NodeAssembler, tokSrc shared.TokenSource, parseLinks bool } type unmarshalState struct { - tk [7]tok.Token // mostly, only 0'th is used... but [1:7] are used during lookahead for links. - shift int // how many times to slide something out of tk[1:7] instead of getting a new token. - parseLinks bool + tk [7]tok.Token // mostly, only 0'th is used... but [1:7] are used during lookahead for links. + shift int // how many times to slide something out of tk[1:7] instead of getting a new token. + options UnmarshalOptions } // step leaves a "new" token in tk[0], @@ -229,7 +239,7 @@ func (st *unmarshalState) unmarshal(na ipld.NodeAssembler, tokSrc shared.TokenSo case tok.TMapOpen: // dag-json has special needs: we pump a few tokens ahead to look for dag-json's "link" pattern. // We can't actually call BeginMap until we're sure it's not gonna turn out to be a link. - if st.parseLinks { + if st.options.ParseLinks { gotLink, err := st.linkLookahead(na, tokSrc) if err != nil { // return in error if any token peeks failed or if structure looked like a link but failed to parse as CID. return err @@ -237,7 +247,9 @@ func (st *unmarshalState) unmarshal(na ipld.NodeAssembler, tokSrc shared.TokenSo if gotLink { return nil } + } + if st.options.ParseBytes { gotBytes, err := st.bytesLookahead(na, tokSrc) if err != nil { return err diff --git a/codec/json/multicodec.go b/codec/json/multicodec.go index c3c807ddf7c794bbc2af6859981982ebc47a05c0..9f2f8c7cb13d2b4424861ef7e922ba07f9317178 100644 --- a/codec/json/multicodec.go +++ b/codec/json/multicodec.go @@ -24,7 +24,10 @@ func init() { func Decode(na ipld.NodeAssembler, r io.Reader) error { // Shell out directly to generic builder path. // (There's not really any fastpaths of note for json.) - err := dagjson.Unmarshal(na, rfmtjson.NewDecoder(r), false) + err := dagjson.Unmarshal(na, rfmtjson.NewDecoder(r), dagjson.UnmarshalOptions{ + ParseLinks: false, + ParseBytes: false, + }) if err != nil { return err } @@ -59,5 +62,9 @@ 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) + }), dagjson.MarshalOptions{ + EncodeLinks: false, + EncodeBytes: false, + SortMapKeys: false, + }) } diff --git a/codec/jst/jst.go b/codec/jst/jst.go index 48747f0021455ced753966da96780351967539cc..438b5aa3335eb0265a800a2f2b507c106ded4a5e 100644 --- a/codec/jst/jst.go +++ b/codec/jst/jst.go @@ -37,10 +37,9 @@ import ( "bytes" "io" - "github.com/polydawn/refmt/json" - ipld "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/codec/dagjson" + "github.com/ipld/go-ipld-prime/codec/json" basicnode "github.com/ipld/go-ipld-prime/node/basic" ) @@ -168,8 +167,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{}), 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. + json.Encode(basicnode.NewString(string(cn)), &buf) // 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. } } @@ -297,9 +296,7 @@ func marshal(ctx *state, n ipld.Node, w io.Writer) error { // It doesn't colorize or anything else. To replace it with something clever that does, // we'll have to tear deeper into the plumbing level of json serializers; will, but later. 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) + err := dagjson.Encode(n, w) // never indent here: these values will always end up being emitted mid-line. if err != nil { return recordErrorPosition(ctx, err) } @@ -470,7 +467,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{}), true); err != nil { + if err := dagjson.Encode(k, w); err != nil { return recordErrorPosition(ctx, err) } if ctx.cfg.Color.Enabled { diff --git a/node/tests/testcase.go b/node/tests/testcase.go index 8072bd46b57ebabd84c43752d5a8223dcb560556..63648f8786e63e88c288418b1d7e5dadf40fee2d 100644 --- a/node/tests/testcase.go +++ b/node/tests/testcase.go @@ -210,7 +210,11 @@ 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), true) + err := dagjson.Marshal(n, json.NewEncoder(&buf, prettyprint), dagjson.MarshalOptions{ + EncodeLinks: true, + EncodeBytes: true, + SortMapKeys: true, + }) if err != nil { t.Errorf("marshal failed: %s", err) }