重构 json handler

This commit is contained in:
Notealot 2023-07-31 15:34:44 +08:00
parent c622cdaf89
commit 24ea0249fc
8 changed files with 36 additions and 40 deletions

View File

@ -14,10 +14,10 @@ import (
"time"
"xorm.io/builder"
"xorm.io/xorm"
"xorm.io/xorm/contexts"
"xorm.io/xorm/convert"
"xorm.io/xorm/dialects"
"xorm.io/xorm/internal/json"
"xorm.io/xorm/internal/utils"
"xorm.io/xorm/schemas"
"xorm.io/xorm/tags"
@ -434,7 +434,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
} else {
if col.IsJSON {
if col.SQLType.IsText() {
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, false, err
}
@ -442,7 +442,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
} else if col.SQLType.IsBlob() {
var bytes []byte
var err error
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, false, err
}
@ -477,7 +477,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
}
if col.SQLType.IsText() {
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, false, err
}
@ -492,7 +492,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
}
return nil, false, nil
}
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, false, err
}

View File

@ -12,9 +12,9 @@ import (
"time"
"xorm.io/builder"
"xorm.io/xorm"
"xorm.io/xorm/convert"
"xorm.io/xorm/dialects"
"xorm.io/xorm/internal/json"
"xorm.io/xorm/internal/utils"
"xorm.io/xorm/schemas"
)
@ -241,7 +241,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
} else {
// Blank struct could not be as update data
if requiredField || !utils.IsStructZero(fieldValue) {
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, nil, fmt.Errorf("mashal %v failed", fieldValue.Interface())
}
@ -270,7 +270,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
}
if col.SQLType.IsText() {
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, nil, err
}
@ -289,7 +289,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
fieldType.Elem().Kind() == reflect.Uint8 {
val = fieldValue.Slice(0, 0).Interface()
} else {
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, nil, err
}

View File

@ -12,9 +12,9 @@ import (
"reflect"
"time"
"xorm.io/xorm"
"xorm.io/xorm/convert"
"xorm.io/xorm/dialects"
"xorm.io/xorm/internal/json"
"xorm.io/xorm/schemas"
)
@ -118,13 +118,13 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
}
if col.SQLType.IsText() {
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, err
}
return string(bytes), nil
} else if col.SQLType.IsBlob() {
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, err
}
@ -132,7 +132,7 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
}
return nil, fmt.Errorf("Unsupported type %v", fieldValue.Type())
case reflect.Complex64, reflect.Complex128:
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, err
}
@ -143,7 +143,7 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
}
if col.SQLType.IsText() {
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, err
}
@ -155,7 +155,7 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
(fieldValue.Type().Elem().Kind() == reflect.Uint8) {
bytes = fieldValue.Bytes()
} else {
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
return nil, err
}

View File

@ -2,19 +2,21 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package json
package xorm
import "encoding/json"
import (
stdjson "encoding/json"
)
// Interface represents an interface to handle json data
type Interface interface {
// JSONHandler represents an interface to handle json data
type JSONHandler interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}
var (
// DefaultJSONHandler default json handler
DefaultJSONHandler Interface = StdJSON{}
DefaultJSONHandler JSONHandler = StdJSON{}
)
// StdJSON implements JSONInterface via encoding/json
@ -22,10 +24,10 @@ type StdJSON struct{}
// Marshal implements JSONInterface
func (StdJSON) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
return stdjson.Marshal(v)
}
// Unmarshal implements JSONInterface
func (StdJSON) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
return stdjson.Unmarshal(data, v)
}

View File

@ -5,7 +5,7 @@
//go:build gojson
// +build gojson
package json
package xorm
import (
gojson "github.com/goccy/go-json"

View File

@ -5,7 +5,7 @@
//go:build jsoniter
// +build jsoniter
package json
package xorm
import (
jsoniter "github.com/json-iterator/go"

View File

@ -16,10 +16,10 @@ import (
"io"
"reflect"
"strconv"
"xorm.io/xorm/contexts"
"xorm.io/xorm/convert"
"xorm.io/xorm/core"
"xorm.io/xorm/internal/json"
"xorm.io/xorm/internal/statements"
"xorm.io/xorm/log"
"xorm.io/xorm/schemas"
@ -479,13 +479,13 @@ func setJSON(fieldValue *reflect.Value, fieldType reflect.Type, scanResult inter
}
if fieldValue.CanAddr() {
err := json.DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
err := DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
if err != nil {
return err
}
} else {
x := reflect.New(fieldType)
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
if err != nil {
return err
}
@ -603,7 +603,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
if col.SQLType.IsText() {
x := reflect.New(fieldType)
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
if err != nil {
return err
}
@ -618,7 +618,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
if col.SQLType.IsText() {
x := reflect.New(fieldType)
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
if err != nil {
return err
}
@ -786,7 +786,3 @@ func (session *Session) NoVersionCheck() *Session {
session.statement.CheckVersion = false
return session
}
func SetDefaultJSONHandler(jsonHandler json.Interface) {
json.DefaultJSONHandler = jsonHandler
}

View File

@ -12,12 +12,10 @@ import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"xorm.io/xorm"
"xorm.io/xorm/convert"
"xorm.io/xorm/internal/json"
"xorm.io/xorm/schemas"
"github.com/stretchr/testify/assert"
)
func TestArrayField(t *testing.T) {
@ -127,24 +125,24 @@ func (s *ConvConfig) FromDB(data []byte) error {
s = nil
return nil
}
return json.DefaultJSONHandler.Unmarshal(data, s)
return xorm.DefaultJSONHandler.Unmarshal(data, s)
}
func (s *ConvConfig) ToDB() ([]byte, error) {
if s == nil {
return nil, nil
}
return json.DefaultJSONHandler.Marshal(s)
return xorm.DefaultJSONHandler.Marshal(s)
}
type SliceType []*ConvConfig
func (s *SliceType) FromDB(data []byte) error {
return json.DefaultJSONHandler.Unmarshal(data, s)
return xorm.DefaultJSONHandler.Unmarshal(data, s)
}
func (s *SliceType) ToDB() ([]byte, error) {
return json.DefaultJSONHandler.Marshal(s)
return xorm.DefaultJSONHandler.Marshal(s)
}
type Nullable struct {