1 Star 0 Fork 1

winie/sq

forked from unsafe-rust/sq 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
12_json.go 2.35 KB
一键复制 编辑 原始数据 按行查看 历史
unsafe-rust 提交于 2021-03-28 10:11 . update
package sq
import (
"database/sql/driver"
"encoding/json"
"errors"
)
var emptyJSON = json.RawMessage("{}")
func JsonObject(value interface{}) (json.RawMessage, error) {
var source []byte
switch t := value.(type) {
case string:
source = []byte(t)
case []byte:
source = t
case nil:
source = emptyJSON
default:
return nil, errors.New("incompatible type for json.RawMessage")
}
if len(source) == 0 {
source = emptyJSON
}
return source, nil
}
// JSONText is a json.RawMessage, which is a []byte underneath.
// Value() validates the json format in the source, and returns an error if
// the json is not valid. Scan does no validation. JSONText additionally
// implements `Unmarshal`, which unmarshals the json within to an interface{}
type JSONText json.RawMessage
// MarshalJSON returns the *j as the JSON encoding of j.
func (j JSONText) MarshalJSON() ([]byte, error) {
if len(j) == 0 {
return emptyJSON, nil
}
return j, nil
}
// UnmarshalJSON sets *j to a copy of data
func (j *JSONText) UnmarshalJSON(data []byte) error {
if j == nil {
return errors.New("JSONText: UnmarshalJSON on nil pointer")
}
*j = append((*j)[0:0], data...)
return nil
}
// Value returns j as a value. This does a validating unmarshal into another
// RawMessage. If j is invalid json, it returns an error.
func (j JSONText) Value() (driver.Value, error) {
var m json.RawMessage
var err = j.Unmarshal(&m)
if err != nil {
return []byte{}, err
}
return []byte(j), nil
}
// Scan stores the src in *j. No validation is done.
func (j *JSONText) Scan(src interface{}) error {
var source []byte
switch t := src.(type) {
case string:
source = []byte(t)
case []byte:
if len(t) == 0 {
source = emptyJSON
} else {
source = t
}
case nil:
*j = JSONText(emptyJSON)
default:
return errors.New("incompatible type for JSONText")
}
*j = append((*j)[0:0], source...)
return nil
}
// Unmarshal unmarshal's the json in j to v, as in json.Unmarshal.
func (j *JSONText) Unmarshal(v interface{}) error {
if len(*j) == 0 {
*j = JSONText(emptyJSON)
}
return json.Unmarshal([]byte(*j), v)
}
// String supports pretty printing for JSONText types.
func (j JSONText) String() string {
return string(j)
}
func (j *JSONText) UnmarshalBinary(data []byte) error {
return j.UnmarshalJSON(data)
}
func (j JSONText) MarshalBinary() ([]byte, error) {
return j.MarshalJSON()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/winie_admin/sq.git
git@gitee.com:winie_admin/sq.git
winie_admin
sq
sq
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385