Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Dec 29, 2023
1 parent 0ce8c5b commit b8b6321
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ func (c Carbon) MarshalJSON() ([]byte, error) {
return nil, c.Error
}
key, value, tz := c.parseTag()
data := fmt.Sprintf(`"%s"`, c.ToDateTimeString(tz))
data := ""
if key == "layout" {
data = fmt.Sprintf(`"%s"`, c.Layout(value, tz))
}
if key == "format" {
data = fmt.Sprintf(`"%s"`, c.Format(value, tz))
// 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
}
Expand All @@ -32,11 +37,13 @@ func (c *Carbon) UnmarshalJSON(b []byte) error {
data := fmt.Sprintf("%s", bytes.Trim(b, `"`))
if key == "layout" {
*c = ParseByLayout(data, value, tz)
c.tag = fmt.Sprintf("layout:%s;tz:%s", value, tz)
}
if key == "format" {
*c = ParseByFormat(data, value, tz)
c.tag = fmt.Sprintf("format:%s;tz:%s", value, tz)
}
c.tag = tag{
carbon: fmt.Sprintf("%s:%s", key, value),
tz: tz,
}
return c.Error
}

0 comments on commit b8b6321

Please sign in to comment.