github.com/ipld/go-ipld-prime@v0.21.0/codec/dagjson/multicodec.go (about)

     1  package dagjson
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/ipld/go-ipld-prime/codec"
     7  	"github.com/ipld/go-ipld-prime/datamodel"
     8  	"github.com/ipld/go-ipld-prime/multicodec"
     9  )
    10  
    11  var (
    12  	_ codec.Decoder = Decode
    13  	_ codec.Encoder = Encode
    14  )
    15  
    16  func init() {
    17  	multicodec.RegisterEncoder(0x0129, Encode)
    18  	multicodec.RegisterDecoder(0x0129, Decode)
    19  }
    20  
    21  // Decode deserializes data from the given io.Reader and feeds it into the given datamodel.NodeAssembler.
    22  // Decode fits the codec.Decoder function interface.
    23  //
    24  // A similar function is available on DecodeOptions type if you would like to customize any of the decoding details.
    25  // This function uses the defaults for the dag-json codec
    26  // (meaning: links are decoded, and bytes are decoded).
    27  //
    28  // This is the function that will be registered in the default multicodec registry during package init time.
    29  func Decode(na datamodel.NodeAssembler, r io.Reader) error {
    30  	return DecodeOptions{
    31  		ParseLinks: true,
    32  		ParseBytes: true,
    33  	}.Decode(na, r)
    34  }
    35  
    36  // Encode walks the given datamodel.Node and serializes it to the given io.Writer.
    37  // Encode fits the codec.Encoder function interface.
    38  //
    39  // A similar function is available on EncodeOptions type if you would like to customize any of the encoding details.
    40  // This function uses the defaults for the dag-json codec
    41  // (meaning: links are encoded, bytes are encoded, and map keys are sorted during encode).
    42  //
    43  // This is the function that will be registered in the default multicodec registry during package init time.
    44  func Encode(n datamodel.Node, w io.Writer) error {
    45  	return EncodeOptions{
    46  		EncodeLinks: true,
    47  		EncodeBytes: true,
    48  		MapSortMode: codec.MapSortMode_Lexical,
    49  	}.Encode(n, w)
    50  }