reprogl
1package session2
3import (4"bytes"5"encoding/json"6
7"xelbot.com/reprogl/security"8)
9
10type serializer interface {11serialize(src internalData) ([]byte, error)12deserialize(src []byte) (internalData, error)13}
14
15type jsonEncoder struct{}16
17func (e jsonEncoder) serialize(src internalData) ([]byte, error) {18aux := struct {19Identity security.Identity `json:"a"`20Values map[string]any `json:"v,omitempty"`21Deadline deadline `json:"d"`22}{23Identity: src.identity,24Values: src.values,25Deadline: src.deadline,26}27
28buf := new(bytes.Buffer)29if err := json.NewEncoder(buf).Encode(&aux); err != nil {30return nil, err31}32
33return buf.Bytes(), nil34}
35
36func (e jsonEncoder) deserialize(src []byte) (internalData, error) {37aux := struct {38Identity security.Identity `json:"a"`39Values map[string]any `json:"v,omitempty"`40Deadline deadline `json:"d"`41}{}42
43if err := json.NewDecoder(bytes.NewReader(src)).Decode(&aux); err != nil {44return internalData{}, DecodeError45}46
47var values = aux.Values48if aux.Values == nil {49values = make(map[string]any)50}51
52return internalData{53identity: aux.Identity,54values: values,55deadline: aux.Deadline,56}, nil57}
58