package securecookie import ( "bytes" "encoding/gob" "encoding/json" ) // Serialize encodes a value using gob. func (e GobEncoder) Serialize(src interface{}) ([]byte, error) { buf := new(bytes.Buffer) enc := gob.NewEncoder(buf) if err := enc.Encode(src); err != nil { return nil, cookieError{cause: err, typ: usageError} } return buf.Bytes(), nil } // Deserialize decodes a value using gob. func (e GobEncoder) Deserialize(src []byte, dst interface{}) error { dec := gob.NewDecoder(bytes.NewBuffer(src)) if err := dec.Decode(dst); err != nil { return cookieError{cause: err, typ: decodeError} } return nil } // Serialize encodes a value using encoding/json. func (e JSONEncoder) Serialize(src interface{}) ([]byte, error) { buf := new(bytes.Buffer) enc := json.NewEncoder(buf) if err := enc.Encode(src); err != nil { return nil, cookieError{cause: err, typ: usageError} } return buf.Bytes(), nil } // Deserialize decodes a value using encoding/json. func (e JSONEncoder) Deserialize(src []byte, dst interface{}) error { dec := json.NewDecoder(bytes.NewReader(src)) if err := dec.Decode(dst); err != nil { return cookieError{cause: err, typ: decodeError} } return nil } // Serialize passes a []byte through as-is. func (e NopEncoder) Serialize(src interface{}) ([]byte, error) { if b, ok := src.([]byte); ok { return b, nil } return nil, errValueNotByte } // Deserialize passes a []byte through as-is. func (e NopEncoder) Deserialize(src []byte, dst interface{}) error { if _, ok := dst.([]byte); ok { dst = src return nil } return errValueNotByte }