代码拉取完成,页面将自动刷新
同步操作将从 dromara/carbon 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package carbon
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday1 DateTime `json:"birthday1"`
Birthday2 DateTimeMilli `json:"birthday2"`
Birthday3 DateTimeMicro `json:"birthday3"`
Birthday4 DateTimeNano `json:"birthday4"`
GraduatedAt1 Date `json:"graduated_at1"`
GraduatedAt2 DateMilli `json:"graduated_at2"`
GraduatedAt3 DateMicro `json:"graduated_at3"`
GraduatedAt4 DateNano `json:"graduated_at4"`
CreatedAt1 Timestamp `json:"created_at1"`
CreatedAt2 TimestampMilli `json:"created_at2"`
CreatedAt3 TimestampMicro `json:"created_at3"`
CreatedAt4 TimestampNano `json:"created_at4"`
}
var person Person
func TestCarbon_MarshalJSON(t *testing.T) {
person := Person{
Name: "gouguoyin",
Age: 18,
Birthday1: DateTime{Now().SubYears(18)},
Birthday2: DateTimeMilli{Now().SubYears(18)},
Birthday3: DateTimeMicro{Now().SubYears(18)},
Birthday4: DateTimeNano{Now().SubYears(18)},
GraduatedAt1: Date{Parse("2020-08-05 13:14:15")},
GraduatedAt2: DateMilli{Parse("2020-08-05 13:14:15.999")},
GraduatedAt3: DateMicro{Parse("2020-08-05 13:14:15.999999")},
GraduatedAt4: DateNano{Parse("2020-08-05 13:14:15.999999999")},
CreatedAt1: Timestamp{Parse("2023-08-05 13:14:15")},
CreatedAt2: TimestampMilli{Parse("2024-08-05 13:14:15.999")},
CreatedAt3: TimestampMicro{Parse("2025-08-05 13:14:15.999999")},
CreatedAt4: TimestampNano{Parse("2025-08-05 13:14:15.999999999")},
}
data, err := json.Marshal(&person)
if err != nil {
assert.NotNil(t, err)
}
fmt.Printf("Person output by json:\n%s\n", data)
}
func TestCarbon_UnmarshalJSON(t *testing.T) {
str := `{
"name": "gouguoyin",
"age": 18,
"birthday1": "2003-07-16 16:22:02",
"birthday2": "2003-07-16 16:22:02.999",
"birthday3": "2003-07-16 16:22:02.999999",
"birthday4": "2003-07-16 16:22:02.999999999",
"graduated_at1": "2020-08-05",
"graduated_at2": "2020-08-05.999",
"graduated_at3": "2020-08-05.999999",
"graduated_at4": "2020-08-05.999999999",
"created_at1": 1596604455,
"created_at2": 1596604455999,
"created_at3": 1596604455999999,
"created_at4": 1596604455999999999
}`
err := json.Unmarshal([]byte(str), &person)
if err != nil {
assert.NotNil(t, err)
}
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
func TestError_Json(t *testing.T) {
str := `{
"name": "",
"age": 0,
"birthday1": "",
"birthday2": "",
"birthday3": "",
"birthday4": "",
"graduated_at1": "xxx",
"graduated_at2": "xxx",
"graduated_at3": "xxx",
"graduated_at4": "xxx",
"created_at1": 0,
"created_at2": 0,
"created_at3": 0,
"created_at4": 0
}`
err := json.Unmarshal([]byte(str), &person)
if err != nil {
assert.NotNil(t, err)
}
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。