1 Star 1 Fork 0

NovaNest / toolkit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
json.go 974 Bytes
一键复制 编辑 原始数据 按行查看 历史
雨丶漫 提交于 2024-03-13 16:37 . json
package json
import (
"encoding/json"
"time"
)
// Val json 字符串
type Val string
// MarshalJSON 格式化为json字符串的时候,会格式化成 object
func (s Val) MarshalJSON() ([]byte, error) {
var data interface{}
json.Unmarshal([]byte(s), &data)
return json.Marshal(data)
}
func (s Val) JSON(v interface{}) error {
return json.Unmarshal([]byte(s), v)
}
// JSONObj json 对象, 序列化的时候,变成纯字符串
type JsonMap map[string]interface{}
func (m JsonMap) String() Val {
b, _ := json.Marshal(m)
return Val(b)
}
func (m JsonMap) JSON(v interface{}) error {
return m.String().JSON(v)
}
// Time 时间别名,在json序列化的时候,会格式成 2006-01-02 15:04:05 这种时间格式
// 结合 gorm 使用,存在数据库的是时间类型,转到接口的是上述时间格式
type Time time.Time
func (tm Time) MarshalJSON() ([]byte, error) {
s := time.Time(tm).Format("2006-01-02 15:04:05")
return json.Marshal(s)
}
Go
1
https://gitee.com/NovaNest/toolkit.git
git@gitee.com:NovaNest/toolkit.git
NovaNest
toolkit
toolkit
v1.0.14

搜索帮助

53164aa7 5694891 3bd8fe86 5694891