Skip to content

Commit

Permalink
Update content about json
Browse files Browse the repository at this point in the history
  • Loading branch information
gouguoyin committed Oct 11, 2024
1 parent 4235ed0 commit 58cbdee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 224 deletions.
79 changes: 4 additions & 75 deletions README.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -1209,85 +1209,11 @@ carbon.Parse("2020-08-05 13:14:15").IsWinter() // false
```

##### JSON

###### 场景一: 所有时间字段有相同的格式
```go
carbon.SetDefault(carbon.Default{
Layout: carbon.DateTimeLayout,
})

type Person struct {
Name string `json:"name"`
Age int `json:"age"`

Field1 carbon.Carbon `json:"field1"`
Field2 carbon.Carbon `json:"field2"`
Field3 carbon.Carbon `json:"field3"`
Field4 carbon.Carbon `json:"field4"`

Field5 carbon.Carbon `json:"field5"`
Field6 carbon.Carbon `json:"field6"`
Field7 carbon.Carbon `json:"field7"`
Field8 carbon.Carbon `json:"field8"`
}

now := carbon.Parse("2020-08-05 13:14:15", carbon.PRC)
person := Person {
Name: "gouguoyin",
Age: 18,

Field1: now,
Field2: now,
Field3: now,
Field4: now,
Field5: now,
Field6: now,
Field7: now,
Field8: now,
}

data, marshalErr := json.Marshal(person)
if marshalErr != nil {
// 错误处理
log.Fatal(marshalErr)
}
fmt.Printf("%s", data)
// 输出
{
"name": "gouguoyin",
"age": 18,
"field1": "2020-08-05 13:14:15",
"field2": "2020-08-05 13:14:15",
"field3": "2020-08-05 13:14:15",
"field4": "2020-08-05 13:14:15",
"field5": "2020-08-05 13:14:15",
"field6": "2020-08-05 13:14:15",
"field7": "2020-08-05 13:14:15",
"field8": "2020-08-05 13:14:15"
}

unmarshalErr := json.Unmarshal(data, &person)
if unmarshalErr != nil {
// 错误处理
log.Fatal(unmarshalErr)
}

fmt.Printf("%s", person.Field1) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field2) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field3) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field4) // 2020-08-05 13:14:15

fmt.Printf("%s", person.Field5) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field6) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field7) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field8) // 2020-08-05 13:14:15
```

###### 场景二: 不同时间字段有不同的格式
```go
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday0 carbon.Carbon `json:"birthday0"`
Birthday1 carbon.DateTime `json:"birthday1"`
Birthday2 carbon.DateTimeMilli `json:"birthday2"`
Birthday3 carbon.DateTimeMicro `json:"birthday3"`
Expand All @@ -1309,6 +1235,7 @@ type Person struct {
person := Person {
Name: "gouguoyin",
Age: 18,
Birthday0: carbon.Now().SubYears(18),
Birthday1: carbon.Now().SubYears(18).ToDateTimeStruct(),
Birthday2: carbon.Now().SubYears(18).ToDateTimeMilliStruct(),
Birthday3: carbon.Now().SubYears(18).ToDateTimeMicroStruct(),
Expand Down Expand Up @@ -1337,6 +1264,7 @@ fmt.Printf("%s", data)
{
"name": "gouguoyin",
"age": 18,
"birthday0": "2003-07-16 13:14:15",
"birthday1": "2003-07-16 13:14:15",
"birthday2": "2003-07-16 13:14:15.999",
"birthday3": "2003-07-16 13:14:15.999999",
Expand All @@ -1361,6 +1289,7 @@ if err != nil {
log.Fatal(err)
}

person.Birthday0.String() // 2003-07-16 13:14:15
person.Birthday1.String() // 2003-07-16 13:14:15
person.Birthday2.String() // 2003-07-16 13:14:15.999
person.Birthday3.String() // 2003-07-16 13:14:15.999999
Expand Down
78 changes: 4 additions & 74 deletions README.jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -1211,84 +1211,11 @@ carbon.Parse("2020-08-05 13:14:15").IsWinter() // false

##### JSON

###### シナリオ 1: すべての時刻フィールドが同じ形式である
```go
carbon.SetDefault(carbon.Default{
Layout: carbon.DateTimeLayout,
})

type Person struct {
Name string `json:"name"`
Age int `json:"age"`

Field1 carbon.Carbon `json:"field1"`
Field2 carbon.Carbon `json:"field2"`
Field3 carbon.Carbon `json:"field3"`
Field4 carbon.Carbon `json:"field4"`

Field5 carbon.Carbon `json:"field5"`
Field6 carbon.Carbon `json:"field6"`
Field7 carbon.Carbon `json:"field7"`
Field8 carbon.Carbon `json:"field8"`
}

now := carbon.Parse("2020-08-05 13:14:15", carbon.PRC)
person := Person {
Name: "gouguoyin",
Age: 18,

Field1: now,
Field2: now,
Field3: now,
Field4: now,
Field5: now,
Field6: now,
Field7: now,
Field8: now,
}

data, marshalErr := json.Marshal(person)
if marshalErr != nil {
// エラー処理...
log.Fatal(marshalErr)
}
fmt.Printf("%s", data)
// 出力
{
"name": "gouguoyin",
"age": 18,
"field1": "2020-08-05 13:14:15",
"field2": "2020-08-05 13:14:15",
"field3": "2020-08-05 13:14:15",
"field4": "2020-08-05 13:14:15",
"field5": "2020-08-05 13:14:15",
"field6": "2020-08-05 13:14:15",
"field7": "2020-08-05 13:14:15",
"field8": "2020-08-05 13:14:15"
}

unmarshalErr := json.Unmarshal(data, &person)
if unmarshalErr != nil {
// エラー処理...
log.Fatal(unmarshalErr)
}

fmt.Printf("%s", person.Field1) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field2) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field3) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field4) // 2020-08-05 13:14:15

fmt.Printf("%s", person.Field5) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field6) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field7) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field8) // 2020-08-05 13:14:15
```

###### シナリオ 2: 異なる時刻フィールドは異なる形式を持つ
```go
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday0 carbon.Carbon `json:"birthday0"`
Birthday1 carbon.DateTime `json:"birthday1"`
Birthday2 carbon.DateTimeMilli `json:"birthday2"`
Birthday3 carbon.DateTimeMicro `json:"birthday3"`
Expand All @@ -1310,6 +1237,7 @@ type Person struct {
person := Person {
Name: "gouguoyin",
Age: 18,
Birthday0: carbon.Now().SubYears(18),
Birthday1: carbon.Now().SubYears(18).ToDateTimeStruct(),
Birthday2: carbon.Now().SubYears(18).ToDateTimeMilliStruct(),
Birthday3: carbon.Now().SubYears(18).ToDateTimeMicroStruct(),
Expand Down Expand Up @@ -1338,6 +1266,7 @@ fmt.Printf("%s", data)
{
"name": "gouguoyin",
"age": 18,
"birthday0": "2003-07-16 13:14:15",
"birthday1": "2003-07-16 13:14:15",
"birthday2": "2003-07-16 13:14:15.999",
"birthday3": "2003-07-16 13:14:15.999999",
Expand All @@ -1362,6 +1291,7 @@ if err != nil {
log.Fatal(err)
}

person.Birthday0.String() // 2003-07-16 13:14:15
person.Birthday1.String() // 2003-07-16 13:14:15
person.Birthday2.String() // 2003-07-16 13:14:15.999
person.Birthday3.String() // 2003-07-16 13:14:15.999999
Expand Down
79 changes: 4 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1205,85 +1205,11 @@ carbon.Parse("2020-08-05 13:14:15").IsWinter() // false

##### JSON

###### Scene one: all time fields have the same format
```go
carbon.SetDefault(carbon.Default{
Layout: carbon.DateTimeLayout,
})

type Person struct {
Name string `json:"name"`
Age int `json:"age"`

Field1 carbon.Carbon `json:"field1"`
Field2 carbon.Carbon `json:"field2"`
Field3 carbon.Carbon `json:"field3"`
Field4 carbon.Carbon `json:"field4"`

Field5 carbon.Carbon `json:"field5"`
Field6 carbon.Carbon `json:"field6"`
Field7 carbon.Carbon `json:"field7"`
Field8 carbon.Carbon `json:"field8"`
}

now := carbon.Parse("2020-08-05 13:14:15", carbon.PRC)
person := Person {
Name: "gouguoyin",
Age: 18,

Field1: now,
Field2: now,
Field3: now,
Field4: now,
Field5: now,
Field6: now,
Field7: now,
Field8: now,
}

data, marshalErr := json.Marshal(person)
if marshalErr != nil {
// Error handle...
log.Fatal(marshalErr)
}
fmt.Printf("%s", data)
// Output
{
"name": "gouguoyin",
"age": 18,
"field1": "2020-08-05 13:14:15",
"field2": "2020-08-05 13:14:15",
"field3": "2020-08-05 13:14:15",
"field4": "2020-08-05 13:14:15",
"field5": "2020-08-05 13:14:15",
"field6": "2020-08-05 13:14:15",
"field7": "2020-08-05 13:14:15",
"field8": "2020-08-05 13:14:15"
}

var person Person
unmarshalErr := json.Unmarshal(data, &person)
if unmarshalErr != nil {
// Error handle...
log.Fatal(unmarshalErr)
}

fmt.Printf("%s", person.Field1) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field2) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field3) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field4) // 2020-08-05 13:14:15

fmt.Printf("%s", person.Field5) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field6) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field7) // 2020-08-05 13:14:15
fmt.Printf("%s", person.Field8) // 2020-08-05 13:14:15
```

###### Scene two: different time fields have different formats
```go
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday0 carbon.Carbon `json:"birthday0"`
Birthday1 carbon.DateTime `json:"birthday1"`
Birthday2 carbon.DateTimeMilli `json:"birthday2"`
Birthday3 carbon.DateTimeMicro `json:"birthday3"`
Expand All @@ -1305,6 +1231,7 @@ type Person struct {
person := Person {
Name: "gouguoyin",
Age: 18,
Birthday0: carbon.Now().SubYears(18),
Birthday1: carbon.Now().SubYears(18).ToDateTimeStruct(),
Birthday2: carbon.Now().SubYears(18).ToDateTimeMilliStruct(),
Birthday3: carbon.Now().SubYears(18).ToDateTimeMicroStruct(),
Expand Down Expand Up @@ -1333,6 +1260,7 @@ fmt.Printf("%s", data)
{
"name": "gouguoyin",
"age": 18,
"birthday0": "2003-07-16 13:14:15",
"birthday1": "2003-07-16 13:14:15",
"birthday2": "2003-07-16 13:14:15.999",
"birthday3": "2003-07-16 13:14:15.999999",
Expand All @@ -1357,6 +1285,7 @@ if err != nil {
log.Fatal(err)
}

person.Birthday0.String() // 2003-07-16 13:14:15
person.Birthday1.String() // 2003-07-16 13:14:15
person.Birthday2.String() // 2003-07-16 13:14:15.999
person.Birthday3.String() // 2003-07-16 13:14:15.999999
Expand Down

0 comments on commit 58cbdee

Please sign in to comment.