1 Star 1 Fork 0

orbit/meteor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
serializer.go 5.60 KB
一键复制 编辑 原始数据 按行查看 历史
package serialize
import (
"encoding/base64"
"strings"
sderrors "gitee.com/orbit-w/meteor/modules/swift_datahub/errors"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
jsoniter "github.com/json-iterator/go"
"go.mongodb.org/mongo-driver/bson"
)
var (
json = jsoniter.ConfigCompatibleWithStandardLibrary
// Proto序列化的JSON Marshaler
protoJSONMarshaler = &jsonpb.Marshaler{
EmitDefaults: true,
OrigName: true,
}
// Proto反序列化的JSON Unmarshaler
protoJSONUnmarshaler = &jsonpb.Unmarshaler{
AllowUnknownFields: true,
}
)
// SerializeFormat 定义序列化格式
type SerializeFormat string
func ParseSerializeFormat(format string) SerializeFormat {
switch format {
case "proto", "protobuf", "pb", "Protobuf":
return FormatProto
case "json", "Json":
return FormatJSON
default:
return FormatJSON
}
}
const (
// FormatProto 使用Protocol Buffers序列化
FormatProto SerializeFormat = "proto"
// FormatJSON 使用JSON序列化
FormatJSON SerializeFormat = "json"
)
// Serializer 提供序列化和反序列化功能
type Serializer struct {
format SerializeFormat
}
// NewSerializer 创建一个新的序列化器
func NewSerializer(format SerializeFormat) *Serializer {
return &Serializer{
format: format,
}
}
func (s *Serializer) MarshalString(obj any) (string, error) {
data, err := s.Marshal(obj)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(data), nil
}
// Marshal 将对象序列化为字节数组
func (s *Serializer) Marshal(obj any) ([]byte, error) {
switch s.format {
case FormatProto:
return s.marshalProto(obj)
case FormatJSON:
return s.marshalJSON(obj)
default:
return nil, sderrors.WithDetails(sderrors.ErrInvalidConfiguration, "unsupported serialization format: %s", s.format)
}
}
func (s *Serializer) UnmarshalString(encodedStr string, obj any) error {
data, err := base64.StdEncoding.DecodeString(encodedStr)
if err != nil {
return sderrors.WithDetails(sderrors.ErrRedisOperationFailed, "failed to decode base64: %v", err)
}
return s.Unmarshal(data, obj)
}
// Unmarshal 将字节数组反序列化为对象
func (s *Serializer) Unmarshal(data []byte, obj any) error {
switch s.format {
case FormatProto:
return s.unmarshalProto(data, obj)
case FormatJSON:
return s.unmarshalJSON(data, obj)
default:
return sderrors.WithDetails(sderrors.ErrInvalidConfiguration, "unsupported serialization format: %s", s.format)
}
}
// marshalProto 使用Protocol Buffers序列化对象
func (s *Serializer) marshalProto(obj any) ([]byte, error) {
msg, ok := obj.(proto.Message)
if !ok {
return nil, sderrors.WithDetails(sderrors.ErrInvalidEntity, "object does not implement proto.Message")
}
return proto.Marshal(msg)
}
// unmarshalProto 使用Protocol Buffers反序列化对象
func (s *Serializer) unmarshalProto(data []byte, obj any) error {
msg, ok := obj.(proto.Message)
if !ok {
return sderrors.WithDetails(sderrors.ErrInvalidEntity, "object does not implement proto.Message")
}
return proto.Unmarshal(data, msg)
}
// marshalJSON 使用JSON序列化对象
func (s *Serializer) marshalJSON(obj any) ([]byte, error) {
// 如果是Proto消息,使用Proto的JSON序列化
if msg, ok := obj.(proto.Message); ok {
return s.marshalProtoToJSON(msg)
}
// 否则使用标准JSON序列化
return json.Marshal(obj)
}
// unmarshalJSON 使用JSON反序列化对象
func (s *Serializer) unmarshalJSON(data []byte, obj any) error {
// 如果是Proto消息,使用Proto的JSON反序列化
if msg, ok := obj.(proto.Message); ok {
return s.unmarshalJSONToProto(data, msg)
}
// 否则使用标准JSON反序列化
return json.Unmarshal(data, obj)
}
// marshalProtoToJSON 将Proto消息序列化为JSON
func (s *Serializer) marshalProtoToJSON(msg proto.Message) ([]byte, error) {
jsonStr, err := protoJSONMarshaler.MarshalToString(msg)
if err != nil {
return nil, sderrors.WithDetails(sderrors.ErrRedisOperationFailed, "failed to marshal proto to JSON: %v", err)
}
return []byte(jsonStr), nil
}
// unmarshalJSONToProto 将JSON反序列化为Proto消息
func (s *Serializer) unmarshalJSONToProto(data []byte, msg proto.Message) error {
err := protoJSONUnmarshaler.Unmarshal(strings.NewReader(string(data)), msg)
if err != nil {
return sderrors.WithDetails(sderrors.ErrRedisOperationFailed, "failed to unmarshal JSON to proto: %v", err)
}
return nil
}
// GetFormat 获取当前序列化格式
func (s *Serializer) GetFormat() SerializeFormat {
return s.format
}
// MarshalProto 便捷函数,直接使用Proto序列化
func MarshalProto(obj proto.Message) ([]byte, error) {
return proto.Marshal(obj)
}
// UnmarshalProto 便捷函数,直接使用Proto反序列化
func UnmarshalProto(data []byte, obj proto.Message) error {
return proto.Unmarshal(data, obj)
}
// MarshalJSON 便捷函数,直接使用JSON序列化
func MarshalJSON(obj interface{}) ([]byte, error) {
return json.Marshal(obj)
}
// UnmarshalJSON 便捷函数,直接使用JSON反序列化
func UnmarshalJSON(data []byte, obj any) error {
return json.Unmarshal(data, obj)
}
// MarshalProtoToJSON 便捷函数,将Proto消息序列化为JSON
func MarshalProtoToJSON(msg proto.Message) ([]byte, error) {
s := NewSerializer(FormatJSON)
return s.marshalProtoToJSON(msg)
}
// UnmarshalJSONToProto 便捷函数,将JSON反序列化为Proto消息
func UnmarshalJSONToProto(data []byte, msg proto.Message) error {
s := NewSerializer(FormatJSON)
return s.unmarshalJSONToProto(data, msg)
}
func (s *Serializer) MarshalBSON(obj any) ([]byte, error) {
return bson.Marshal(obj)
}
func (s *Serializer) UnmarshalBSON(data []byte, obj any) error {
return bson.Unmarshal(data, obj)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/orbit-w/meteor.git
git@gitee.com:orbit-w/meteor.git
orbit-w
meteor
meteor
8cff0bb952fe

搜索帮助