1 Star 0 Fork 1

go-genie/sqlx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
json_scanner.go 805 Bytes
一键复制 编辑 原始数据 按行查看 历史
lijun 提交于 2024-02-19 15:56 +08:00 . init: initialization project
package datatypes
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
func JSONScan(dbValue interface{}, value interface{}) error {
switch v := dbValue.(type) {
case []byte:
bytes := v
if len(bytes) > 0 {
return json.Unmarshal(bytes, value)
}
return nil
case string:
str := v
if str == "" {
return nil
}
return json.Unmarshal([]byte(str), value)
case nil:
return nil
default:
return fmt.Errorf("cannot sql.Scan() from: %#v", value)
}
}
func JSONValue(value interface{}) (driver.Value, error) {
if zeroCheck, ok := value.(interface {
IsZero() bool
}); ok {
if zeroCheck.IsZero() {
return "", nil
}
}
bytes, err := json.Marshal(value)
if err != nil {
return "", err
}
str := string(bytes)
if str == "null" {
return "", nil
}
return str, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/go-genie/sqlx.git
git@gitee.com:go-genie/sqlx.git
go-genie
sqlx
sqlx
v1.1.3

搜索帮助