Feat: use fast json serializer

pull/21/head
zijiren233 2 years ago
parent afa2c8d15b
commit 1a7757802a

@ -4,6 +4,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/synctv-org/synctv/internal/conf"
"github.com/synctv-org/synctv/internal/model"
_ "github.com/synctv-org/synctv/utils/fastJSONSerializer"
"gorm.io/gorm"
)

@ -23,5 +23,5 @@ type BaseMovieInfo struct {
Proxy bool `json:"proxy"`
RtmpSource bool `json:"rtmpSource"`
Type string `gorm:"varchar(32)" json:"type"`
Headers map[string]string `gorm:"serializer:json" json:"headers"`
Headers map[string]string `gorm:"serializer:fastjson" json:"headers"`
}

@ -0,0 +1,45 @@
package fastjsonserializer
import (
"context"
"fmt"
"reflect"
jsoniter "github.com/json-iterator/go"
"gorm.io/gorm/schema"
)
var json = jsoniter.ConfigCompatibleWithStandardLibrary
type JSONSerializer struct{}
func (*JSONSerializer) Scan(ctx context.Context, field *schema.Field, dst reflect.Value, dbValue interface{}) (err error) {
fieldValue := reflect.New(field.FieldType)
if dbValue != nil {
var bytes []byte
switch v := dbValue.(type) {
case []byte:
bytes = v
case string:
bytes = []byte(v)
default:
return fmt.Errorf("failed to unmarshal JSONB value: %#v", dbValue)
}
err = json.Unmarshal(bytes, fieldValue.Interface())
}
field.ReflectValueOf(ctx, dst).Set(fieldValue.Elem())
return
}
// 实现 Value 方法
func (*JSONSerializer) Value(ctx context.Context, field *schema.Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {
return json.Marshal(fieldValue)
}
func init() {
schema.RegisterSerializer("fastjson", new(JSONSerializer))
}
Loading…
Cancel
Save