github.com/zly-app/zapp@v1.3.3/pkg/serializer/json.go (about) 1 package serializer 2 3 import ( 4 "encoding/json" 5 "io" 6 ) 7 8 const JsonSerializerName = "json" 9 10 type jsonSerializer struct{} 11 12 func (jsonSerializer) Marshal(a interface{}, w io.Writer) error { 13 return json.NewEncoder(w).Encode(a) 14 } 15 16 func (s jsonSerializer) MarshalBytes(a interface{}) ([]byte, error) { 17 return json.Marshal(a) 18 } 19 20 func (jsonSerializer) Unmarshal(r io.Reader, a interface{}) error { 21 return json.NewDecoder(r).Decode(a) 22 } 23 24 func (s jsonSerializer) UnmarshalBytes(data []byte, a interface{}) error { 25 return json.Unmarshal(data, a) 26 }