mirror of
https://github.com/weloe/token-go.git
synced 2025-11-03 10:01:09 +08:00
feat: add SerializerAdapter support custom serializer
This commit is contained in:
27
persist/json_adapter.go
Normal file
27
persist/json_adapter.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package persist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/weloe/token-go/model"
|
||||
)
|
||||
|
||||
type JsonAdapter struct {
|
||||
*DefaultAdapter
|
||||
}
|
||||
|
||||
func NewJsonAdapter() *JsonAdapter {
|
||||
return &JsonAdapter{NewDefaultAdapter()}
|
||||
}
|
||||
|
||||
func (j *JsonAdapter) Serialize(session *model.Session) ([]byte, error) {
|
||||
return json.Marshal(session)
|
||||
}
|
||||
|
||||
func (j *JsonAdapter) UnSerialize(bytes []byte) (*model.Session, error) {
|
||||
s := &model.Session{}
|
||||
err := json.Unmarshal(bytes, s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
Reference in New Issue
Block a user