diff --git a/internal/statements/statement.go b/internal/statements/statement.go index 017f40a5..083c3f47 100644 --- a/internal/statements/statement.go +++ b/internal/statements/statement.go @@ -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 } diff --git a/internal/statements/update.go b/internal/statements/update.go index 5d71f34d..6da1843a 100644 --- a/internal/statements/update.go +++ b/internal/statements/update.go @@ -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 } diff --git a/internal/statements/values.go b/internal/statements/values.go index 4c1360ed..b308ca1b 100644 --- a/internal/statements/values.go +++ b/internal/statements/values.go @@ -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 } diff --git a/internal/json/json.go b/json.go similarity index 70% rename from internal/json/json.go rename to json.go index ef52f51f..e6467d2d 100644 --- a/internal/json/json.go +++ b/json.go @@ -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) } diff --git a/internal/json/gojson.go b/json_gojson.go similarity index 97% rename from internal/json/gojson.go rename to json_gojson.go index 9bfa5c29..7804685f 100644 --- a/internal/json/gojson.go +++ b/json_gojson.go @@ -5,7 +5,7 @@ //go:build gojson // +build gojson -package json +package xorm import ( gojson "github.com/goccy/go-json" diff --git a/internal/json/jsoniter.go b/json_jsoniter.go similarity index 98% rename from internal/json/jsoniter.go rename to json_jsoniter.go index be93ac4e..3e0f0455 100644 --- a/internal/json/jsoniter.go +++ b/json_jsoniter.go @@ -5,7 +5,7 @@ //go:build jsoniter // +build jsoniter -package json +package xorm import ( jsoniter "github.com/json-iterator/go" diff --git a/session.go b/session.go index 14d0781e..31ba2295 100644 --- a/session.go +++ b/session.go @@ -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 -} diff --git a/tests/types_test.go b/tests/types_test.go index dfdb4766..8abbbbbe 100644 --- a/tests/types_test.go +++ b/tests/types_test.go @@ -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 {