This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,55 @@ | ||
package encoding | ||
|
||
import "encoding/json" | ||
import ( | ||
"encoding/json" | ||
"io" | ||
) | ||
|
||
type UnmarshalFunc func([]byte, interface{}) error | ||
|
||
func (u UnmarshalFunc) Unmarshal(b []byte, v interface{}) error { | ||
return u(b, v) | ||
type Codec interface { | ||
Encode(io.Writer, interface{}) error | ||
Marshal(interface{}) ([]byte, error) | ||
Decode(io.Reader, interface{}) error | ||
Unmarshal([]byte, interface{}) error | ||
Pool([]byte) | ||
} | ||
|
||
type MarshalFunc func(interface{}) ([]byte, error) | ||
type JSON struct{} | ||
|
||
func (m MarshalFunc) Marshal(v interface{}) ([]byte, error) { | ||
return m(v) | ||
func (JSON) Encode(w io.Writer, v interface{}) error { | ||
return json.NewEncoder(w).Encode(v) | ||
} | ||
|
||
type PoolFunc func([]byte) | ||
func (JSON) Marshal(v interface{}) ([]byte, error) { | ||
return json.Marshal(v) | ||
} | ||
|
||
func (p PoolFunc) Pool(b []byte) { | ||
p(b) | ||
func (JSON) Decode(r io.Reader, v interface{}) error { | ||
return json.NewDecoder(r).Decode(v) | ||
} | ||
|
||
type Codec interface { | ||
Unmarshal([]byte, interface{}) error | ||
Marshal(interface{}) ([]byte, error) | ||
Pool([]byte) | ||
func (JSON) Unmarshal(data []byte, v interface{}) error { | ||
return json.Unmarshal(data, v) | ||
} | ||
|
||
func (JSON) Pool([]byte) {} | ||
|
||
var DefaultCodec Codec = JSON{} | ||
|
||
func Encode(w io.Writer, v interface{}) error { | ||
return DefaultCodec.Encode(w, v) | ||
} | ||
|
||
type funcCodec struct { | ||
UnmarshalFunc | ||
MarshalFunc | ||
PoolFunc | ||
func Marshal(v interface{}) ([]byte, error) { | ||
return DefaultCodec.Marshal(v) | ||
} | ||
|
||
func NewFuncCodec(unmarshal UnmarshalFunc, marshal MarshalFunc, pool PoolFunc) Codec { | ||
if pool == nil { | ||
pool = func([]byte) {} | ||
} | ||
return funcCodec{ | ||
UnmarshalFunc: unmarshal, | ||
MarshalFunc: marshal, | ||
PoolFunc: pool, | ||
} | ||
func Decode(r io.Reader, v interface{}) error { | ||
return DefaultCodec.Decode(r, v) | ||
} | ||
|
||
var JSON = NewFuncCodec(json.Unmarshal, json.Marshal, nil) | ||
func Unmarshal(data []byte, v interface{}) error { | ||
return DefaultCodec.Unmarshal(data, v) | ||
} | ||
func Pool(data []byte) { | ||
DefaultCodec.Pool(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.