Skip to content
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

Closed
shuqingzai opened this issue Nov 18, 2023 · 5 comments
Labels
Question Indicates that an issue, pull request, or discussion needs more information

Comments

@shuqingzai
Copy link

参考:

格式是使用固定的常量,用户没法修改,应该改为设置不同类型的全局变量,默认值就是目前的常量模板,让用户在程序启动时,可设置 JSON 序列化的模板

对于需要使用标准库的默认模板(带有时区 carbon.ISO8601Layoutcarbon.RFC3339Layout ),目前无法实现,对于时区的掌控,我们需要交给前端部署的不同地区节点,自动转换,服务器统一使用 UTC

@shuqingzai shuqingzai added the Question Indicates that an issue, pull request, or discussion needs more information label Nov 18, 2023
@Issues-translate-bot Issues-translate-bot changed the title 目前 JSON 序列化格式是 2006-01-02 15:04:05.999999999 ,是否允许用户自定义配置 The current JSON serialization format is 2006-01-02 15:04:05.999999999. Is user-defined configuration allowed? Nov 18, 2023
@gouguoyin
Copy link
Member

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
}

@shuqingzai
Copy link
Author

c := carbon.ParseByLayout(string(bytes.Trim(b, `"`)), carbon.RFC3339Layout, t.Location())
	if c.Error == nil {
		*t = carbon.DateTime{Carbon: c}
	}
	return c.Error

是的,我目前是这样的做,只是提个意见,用户可以自定义格式

@Issues-translate-bot
Copy link
Collaborator

The issue body's language is not English, translate it automatically, please use English next time. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿


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.

@gouguoyin
Copy link
Member

The problem is

type Person struct {
    Birthday1 carbon.Carbon `json:"birthday1"`
    Birthday2 carbon.Carbon `json:"birthday2"`
}

I want birthday1 to display Y-m-d H:i:s format, birthday2 to display Y-m-d format.

How to distinguish different output formats of the same type? When should users be allowed to customize different formats?

@gouguoyin
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Indicates that an issue, pull request, or discussion needs more information
Development

No branches or pull requests

3 participants