-
-
Notifications
You must be signed in to change notification settings - Fork 231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The current JSON serialization format is 2006-01-02 15:04:05.999999999
. Is user-defined configuration allowed?
#201
Comments
2006-01-02 15:04:05.999999999
,是否允许用户自定义配置2006-01-02 15:04:05.999999999
. Is user-defined configuration allowed?
At present, please expand the method yourself, such as: type rfc3339 carbon.Carbon
func (t rfc3339) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, t.ToRfc3339String())), nil
}
func (t *rfc3339) UnmarshalJSON(b []byte) error {
c := carbon.ParseByLayout(string(bytes.Trim(b, `"`)), carbon.RFC3339Layout, t.Location())
if c.Error == nil {
*t = carbon.DateTime{Carbon: c}
}
return c.Error
} |
是的,我目前是这样的做,只是提个意见,用户可以自定义格式 |
Yes, this is how I do it currently. I just want to give my opinion. Users can customize the format. |
The problem is type Person struct {
Birthday1 carbon.Carbon `json:"birthday1"`
Birthday2 carbon.Carbon `json:"birthday2"`
} I want How to distinguish different output formats of the same type? When should users be allowed to customize different formats? |
In v2.3.0, the tag mechanism is used for json user-defined encoding and decoding. 定义模型type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday carbon.Carbon `json:"birthday" carbon:"layout:2006-01-02"`
GraduatedAt carbon.Carbon `json:"graduated_at" carbon:"layout:15:04:05"`
CreatedAt carbon.Carbon `json:"created_at" carbon:"layout:2006-01-02 15:04:05"`
} 或 type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday carbon.Carbon `json:"birthday" carbon:"format:Y-m-d"`
GraduatedAt carbon.Carbon `json:"graduated_at" carbon:"format:H:i:s"`
CreatedAt carbon.Carbon `json:"created_at" carbon:"format:Y-m-d H:i:s"`
} 实例化模型now := Parse("2020-08-05 13:14:15", PRC)
person := Person {
Name: "gouguoyin",
Age: 18,
Birthday: now,
GraduatedAt: now,
CreatedAt: now,
} JSON 编码err1 := carbon.LoadTag(&person)
if err1 != nil {
// 错误处理
log.Fatal(err1)
}
data, err2 := json.Marshal(person)
if err2 != nil {
// 错误处理
log.Fatal(err2)
}
fmt.Printf("%s", data)
// 输出
{
"name": "gouguoyin",
"age": 18,
"birthday": "2020-08-05",
"graduated_at": "13:14:15",
"created_at": "2020-08-05 13:14:15"
} JSON 解码str := `{
"name": "gouguoyin",
"age": 18,
"birthday": "2020-08-05",
"graduated_at": "13:14:15",
"created_at": "2020-08-05 13:14:15"
}`
var person Person
err1 := carbon.LoadTag(&person)
if err1 != nil {
// 错误处理
log.Fatal(err1)
}
err2 := json.Unmarshal([]byte(str), &person)
if err2 != nil {
// 错误处理
log.Fatal(err2)
}
fmt.Sprintf("%s", person.Birthday) // 2002-08-05
fmt.Sprintf("%s", person.GraduatedAt) // 13:14:15
fmt.Sprintf("%s", person.CreatedAt) // 2002-08-05 13:14:15 |
参考:
carbon/json.go
Line 107 in 1e4801a
carbon/json.go
Line 113 in 1e4801a
格式是使用固定的常量,用户没法修改,应该改为设置不同类型的全局变量,默认值就是目前的常量模板,让用户在程序启动时,可设置 JSON 序列化的模板
对于需要使用标准库的默认模板(带有时区
carbon.ISO8601Layout
或carbon.RFC3339Layout
),目前无法实现,对于时区的掌控,我们需要交给前端部署的不同地区节点,自动转换,服务器统一使用UTC
The text was updated successfully, but these errors were encountered: