重构 json handler
This commit is contained in:
parent
c622cdaf89
commit
24ea0249fc
|
@ -14,10 +14,10 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/contexts"
|
"xorm.io/xorm/contexts"
|
||||||
"xorm.io/xorm/convert"
|
"xorm.io/xorm/convert"
|
||||||
"xorm.io/xorm/dialects"
|
"xorm.io/xorm/dialects"
|
||||||
"xorm.io/xorm/internal/json"
|
|
||||||
"xorm.io/xorm/internal/utils"
|
"xorm.io/xorm/internal/utils"
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
"xorm.io/xorm/tags"
|
"xorm.io/xorm/tags"
|
||||||
|
@ -434,7 +434,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
|
||||||
} else {
|
} else {
|
||||||
if col.IsJSON {
|
if col.IsJSON {
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
@ -442,7 +442,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
|
||||||
} else if col.SQLType.IsBlob() {
|
} else if col.SQLType.IsBlob() {
|
||||||
var bytes []byte
|
var bytes []byte
|
||||||
var err error
|
var err error
|
||||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
@ -477,7 +477,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
@ -492,7 +492,7 @@ func (statement *Statement) asDBCond(fieldValue reflect.Value, fieldType reflect
|
||||||
}
|
}
|
||||||
return nil, false, nil
|
return nil, false, nil
|
||||||
}
|
}
|
||||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +12,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/convert"
|
"xorm.io/xorm/convert"
|
||||||
"xorm.io/xorm/dialects"
|
"xorm.io/xorm/dialects"
|
||||||
"xorm.io/xorm/internal/json"
|
|
||||||
"xorm.io/xorm/internal/utils"
|
"xorm.io/xorm/internal/utils"
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
)
|
)
|
||||||
|
@ -241,7 +241,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
|
||||||
} else {
|
} else {
|
||||||
// Blank struct could not be as update data
|
// Blank struct could not be as update data
|
||||||
if requiredField || !utils.IsStructZero(fieldValue) {
|
if requiredField || !utils.IsStructZero(fieldValue) {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("mashal %v failed", fieldValue.Interface())
|
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() {
|
if col.SQLType.IsText() {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -289,7 +289,7 @@ func (statement *Statement) BuildUpdates(tableValue reflect.Value,
|
||||||
fieldType.Elem().Kind() == reflect.Uint8 {
|
fieldType.Elem().Kind() == reflect.Uint8 {
|
||||||
val = fieldValue.Slice(0, 0).Interface()
|
val = fieldValue.Slice(0, 0).Interface()
|
||||||
} else {
|
} else {
|
||||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,9 +12,9 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/convert"
|
"xorm.io/xorm/convert"
|
||||||
"xorm.io/xorm/dialects"
|
"xorm.io/xorm/dialects"
|
||||||
"xorm.io/xorm/internal/json"
|
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -118,13 +118,13 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return string(bytes), nil
|
return string(bytes), nil
|
||||||
} else if col.SQLType.IsBlob() {
|
} else if col.SQLType.IsBlob() {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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())
|
return nil, fmt.Errorf("Unsupported type %v", fieldValue.Type())
|
||||||
case reflect.Complex64, reflect.Complex128:
|
case reflect.Complex64, reflect.Complex128:
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
|
||||||
}
|
}
|
||||||
|
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
bytes, err := json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err := xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -155,7 +155,7 @@ func (statement *Statement) Value2Interface(col *schemas.Column, fieldValue refl
|
||||||
(fieldValue.Type().Elem().Kind() == reflect.Uint8) {
|
(fieldValue.Type().Elem().Kind() == reflect.Uint8) {
|
||||||
bytes = fieldValue.Bytes()
|
bytes = fieldValue.Bytes()
|
||||||
} else {
|
} else {
|
||||||
bytes, err = json.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
bytes, err = xorm.DefaultJSONHandler.Marshal(fieldValue.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,19 +2,21 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// 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
|
// JSONHandler represents an interface to handle json data
|
||||||
type Interface interface {
|
type JSONHandler interface {
|
||||||
Marshal(v interface{}) ([]byte, error)
|
Marshal(v interface{}) ([]byte, error)
|
||||||
Unmarshal(data []byte, v interface{}) error
|
Unmarshal(data []byte, v interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// DefaultJSONHandler default json handler
|
// DefaultJSONHandler default json handler
|
||||||
DefaultJSONHandler Interface = StdJSON{}
|
DefaultJSONHandler JSONHandler = StdJSON{}
|
||||||
)
|
)
|
||||||
|
|
||||||
// StdJSON implements JSONInterface via encoding/json
|
// StdJSON implements JSONInterface via encoding/json
|
||||||
|
@ -22,10 +24,10 @@ type StdJSON struct{}
|
||||||
|
|
||||||
// Marshal implements JSONInterface
|
// Marshal implements JSONInterface
|
||||||
func (StdJSON) Marshal(v interface{}) ([]byte, error) {
|
func (StdJSON) Marshal(v interface{}) ([]byte, error) {
|
||||||
return json.Marshal(v)
|
return stdjson.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unmarshal implements JSONInterface
|
// Unmarshal implements JSONInterface
|
||||||
func (StdJSON) Unmarshal(data []byte, v interface{}) error {
|
func (StdJSON) Unmarshal(data []byte, v interface{}) error {
|
||||||
return json.Unmarshal(data, v)
|
return stdjson.Unmarshal(data, v)
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@
|
||||||
//go:build gojson
|
//go:build gojson
|
||||||
// +build gojson
|
// +build gojson
|
||||||
|
|
||||||
package json
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
gojson "github.com/goccy/go-json"
|
gojson "github.com/goccy/go-json"
|
|
@ -5,7 +5,7 @@
|
||||||
//go:build jsoniter
|
//go:build jsoniter
|
||||||
// +build jsoniter
|
// +build jsoniter
|
||||||
|
|
||||||
package json
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
14
session.go
14
session.go
|
@ -16,10 +16,10 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"xorm.io/xorm/contexts"
|
"xorm.io/xorm/contexts"
|
||||||
"xorm.io/xorm/convert"
|
"xorm.io/xorm/convert"
|
||||||
"xorm.io/xorm/core"
|
"xorm.io/xorm/core"
|
||||||
"xorm.io/xorm/internal/json"
|
|
||||||
"xorm.io/xorm/internal/statements"
|
"xorm.io/xorm/internal/statements"
|
||||||
"xorm.io/xorm/log"
|
"xorm.io/xorm/log"
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
|
@ -479,13 +479,13 @@ func setJSON(fieldValue *reflect.Value, fieldType reflect.Type, scanResult inter
|
||||||
}
|
}
|
||||||
|
|
||||||
if fieldValue.CanAddr() {
|
if fieldValue.CanAddr() {
|
||||||
err := json.DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
|
err := DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
x := reflect.New(fieldType)
|
x := reflect.New(fieldType)
|
||||||
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -603,7 +603,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
|
||||||
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
|
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
x := reflect.New(fieldType)
|
x := reflect.New(fieldType)
|
||||||
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -618,7 +618,7 @@ func (session *Session) convertBeanField(col *schemas.Column, fieldValue *reflec
|
||||||
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
|
if ok && fieldType.Elem().Kind() == reflect.Uint8 {
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
x := reflect.New(fieldType)
|
x := reflect.New(fieldType)
|
||||||
err := json.DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -786,7 +786,3 @@ func (session *Session) NoVersionCheck() *Session {
|
||||||
session.statement.CheckVersion = false
|
session.statement.CheckVersion = false
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetDefaultJSONHandler(jsonHandler json.Interface) {
|
|
||||||
json.DefaultJSONHandler = jsonHandler
|
|
||||||
}
|
|
||||||
|
|
|
@ -12,12 +12,10 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
"xorm.io/xorm/convert"
|
"xorm.io/xorm/convert"
|
||||||
"xorm.io/xorm/internal/json"
|
|
||||||
"xorm.io/xorm/schemas"
|
"xorm.io/xorm/schemas"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestArrayField(t *testing.T) {
|
func TestArrayField(t *testing.T) {
|
||||||
|
@ -127,24 +125,24 @@ func (s *ConvConfig) FromDB(data []byte) error {
|
||||||
s = nil
|
s = nil
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return json.DefaultJSONHandler.Unmarshal(data, s)
|
return xorm.DefaultJSONHandler.Unmarshal(data, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ConvConfig) ToDB() ([]byte, error) {
|
func (s *ConvConfig) ToDB() ([]byte, error) {
|
||||||
if s == nil {
|
if s == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return json.DefaultJSONHandler.Marshal(s)
|
return xorm.DefaultJSONHandler.Marshal(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
type SliceType []*ConvConfig
|
type SliceType []*ConvConfig
|
||||||
|
|
||||||
func (s *SliceType) FromDB(data []byte) error {
|
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) {
|
func (s *SliceType) ToDB() ([]byte, error) {
|
||||||
return json.DefaultJSONHandler.Marshal(s)
|
return xorm.DefaultJSONHandler.Marshal(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Nullable struct {
|
type Nullable struct {
|
||||||
|
|
Loading…
Reference in New Issue