1 Star 0 Fork 63

jangocheng/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
encoding.go 1.10 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2024-01-07 20:07 . Tag 结构体更名为 tag
package carbon
import (
"bytes"
"fmt"
)
// MarshalJSON implements the interface Marshaler for Carbon struct.
// 实现 Marshaler 接口
func (c Carbon) MarshalJSON() ([]byte, error) {
if c.Error != nil {
return nil, c.Error
}
key, value, tz := c.parseTag()
data := ""
if key == "layout" {
data = fmt.Sprintf(`"%s"`, c.Layout(value, tz))
}
if key == "format" {
// timestamp without double quotes in json
if value == "U" || value == "V" || value == "X" || value == "Z" {
data = fmt.Sprintf(`%s`, c.Format(value, tz))
} else {
data = fmt.Sprintf(`"%s"`, c.Format(value, tz))
}
}
return []byte(data), nil
}
// UnmarshalJSON implements the interface Unmarshaler for Carbon struct.
// 实现 Unmarshaler 接口
func (c *Carbon) UnmarshalJSON(b []byte) error {
if c.Error != nil {
return c.Error
}
key, value, tz := c.parseTag()
data := fmt.Sprintf("%s", bytes.Trim(b, `"`))
if key == "layout" {
*c = ParseByLayout(data, value, tz)
}
if key == "format" {
*c = ParseByFormat(data, value, tz)
}
c.tag = &tag{
carbon: fmt.Sprintf("%s:%s", key, value),
tz: tz,
}
return c.Error
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jangocheng/carbon.git
git@gitee.com:jangocheng/carbon.git
jangocheng
carbon
carbon
master

搜索帮助