Skip to content

Commit

Permalink
Add json codec (#24)
Browse files Browse the repository at this point in the history
Block Encode/Decode is really nice and id would be nice to use it with
JSON :)
  • Loading branch information
hannahhoward authored Oct 18, 2024
1 parent e9b4aa0 commit 0909684
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions core/ipld/codec/json/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package json

import (
"github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/schema"
)

const Code = 0x0129

type codec struct{}

func (codec) Code() uint64 {
return Code
}

func (codec) Encode(val any, typ schema.Type) ([]byte, error) {
return Encode(val, typ)
}

func (codec) Decode(b []byte, bind any, typ schema.Type) error {
return Decode(b, bind, typ)
}

var Codec = codec{}

func Encode(val any, typ schema.Type) ([]byte, error) {
return ipld.Marshal(dagjson.Encode, val, typ)
}

func Decode(b []byte, bind any, typ schema.Type) error {
_, err := ipld.Unmarshal(b, dagjson.Decode, bind, typ)
return err
}

0 comments on commit 0909684

Please sign in to comment.