github.com/urso/go-structform@v0.0.2/bench/encoder.go (about)

     1  package bench
     2  
     3  import (
     4  	"bytes"
     5  	stdjson "encoding/json"
     6  	"io"
     7  
     8  	// jsoniter "github.com/json-iterator/go"
     9  	"github.com/ugorji/go/codec"
    10  	"github.com/urso/go-structform/cborl"
    11  	"github.com/urso/go-structform/gotype"
    12  	"github.com/urso/go-structform/json"
    13  	"github.com/urso/go-structform/ubjson"
    14  )
    15  
    16  type encoderFactory func(io.Writer) func(interface{}) error
    17  type decoderFactory func([]byte) func(interface{}) error
    18  type transcodeFactory func(io.Writer) func([]byte) error
    19  
    20  func stdJSONEncoder(w io.Writer) func(interface{}) error {
    21  	enc := stdjson.NewEncoder(w)
    22  	return enc.Encode
    23  }
    24  
    25  func stdJSONDecoder(r io.Reader) func(interface{}) error {
    26  	dec := stdjson.NewDecoder(r)
    27  	return dec.Decode
    28  }
    29  
    30  func stdJSONBufDecoder(b []byte) func(interface{}) error {
    31  	return stdJSONDecoder(bytes.NewReader(b))
    32  }
    33  
    34  func gocodecJSONDecoder(r io.Reader) func(interface{}) error {
    35  	h := &codec.JsonHandle{}
    36  	dec := codec.NewDecoder(r, h)
    37  	return dec.Decode
    38  }
    39  
    40  /*
    41  func jsoniterDecoder(r io.Reader) func(interface{}) error {
    42  	iter := jsoniter.Parse(r, 4096)
    43  	return func(v interface{}) error {
    44  		iter.ReadVal(v)
    45  		return iter.Error
    46  	}
    47  }
    48  
    49  func jsoniterBufDecoder(b []byte) func(interface{}) error {
    50  	iter := jsoniter.ParseBytes(b)
    51  	return func(v interface{}) error {
    52  		iter.ReadVal(v)
    53  		return iter.Error
    54  	}
    55  }
    56  */
    57  
    58  func structformJSONEncoder(w io.Writer) func(interface{}) error {
    59  	vs := json.NewVisitor(w)
    60  	folder, _ := gotype.NewIterator(vs)
    61  	return folder.Fold
    62  }
    63  
    64  func structformUBJSONEncoder(w io.Writer) func(interface{}) error {
    65  	vs := ubjson.NewVisitor(w)
    66  	folder, _ := gotype.NewIterator(vs)
    67  	return folder.Fold
    68  }
    69  
    70  func structformCBORLEncoder(w io.Writer) func(interface{}) error {
    71  	vs := cborl.NewVisitor(w)
    72  	folder, _ := gotype.NewIterator(vs)
    73  	return folder.Fold
    74  }
    75  
    76  func structformJSONBufDecoder(keyCache int) func([]byte) func(interface{}) error {
    77  	return func(b []byte) func(interface{}) error {
    78  		u, _ := gotype.NewUnfolder(nil)
    79  		dec := json.NewBytesDecoder(b, u)
    80  		return makeStructformDecoder(u, dec.Next, keyCache)
    81  	}
    82  }
    83  
    84  func structformUBJSONBufDecoder(keyCache int) func([]byte) func(interface{}) error {
    85  	return func(b []byte) func(interface{}) error {
    86  		u, _ := gotype.NewUnfolder(nil)
    87  		dec := ubjson.NewBytesDecoder(b, u)
    88  		return makeStructformDecoder(u, dec.Next, keyCache)
    89  	}
    90  }
    91  
    92  func structformCBORLBufDecoder(keyCache int) func([]byte) func(interface{}) error {
    93  	return func(b []byte) func(interface{}) error {
    94  		u, _ := gotype.NewUnfolder(nil)
    95  		dec := cborl.NewBytesDecoder(b, u)
    96  		return makeStructformDecoder(u, dec.Next, keyCache)
    97  	}
    98  }
    99  
   100  func makeStructformDecoder(
   101  	u *gotype.Unfolder,
   102  	next func() error,
   103  	keyCache int,
   104  ) func(interface{}) error {
   105  	if keyCache > 0 {
   106  		u.EnableKeyCache(keyCache)
   107  	}
   108  	return func(v interface{}) error {
   109  		if err := u.SetTarget(v); err != nil {
   110  			return err
   111  		}
   112  		return next()
   113  	}
   114  }
   115  
   116  func makeCBORL2JSONTranscoder(w io.Writer) func([]byte) error {
   117  	j := json.NewVisitor(w)
   118  	p := cborl.NewParser(j)
   119  	return p.Parse
   120  }
   121  
   122  func makeUBJSON2JSONTranscoder(w io.Writer) func([]byte) error {
   123  	j := json.NewVisitor(w)
   124  	p := ubjson.NewParser(j)
   125  	return p.Parse
   126  }