Skip to content

Commit 58cbdee

Browse files
committed
Update content about json
1 parent 4235ed0 commit 58cbdee

File tree

3 files changed

+12
-224
lines changed

3 files changed

+12
-224
lines changed

README.cn.md

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,85 +1209,11 @@ carbon.Parse("2020-08-05 13:14:15").IsWinter() // false
12091209
```
12101210

12111211
##### JSON
1212-
1213-
###### 场景一: 所有时间字段有相同的格式
1214-
```go
1215-
carbon.SetDefault(carbon.Default{
1216-
Layout: carbon.DateTimeLayout,
1217-
})
1218-
1219-
type Person struct {
1220-
Name string `json:"name"`
1221-
Age int `json:"age"`
1222-
1223-
Field1 carbon.Carbon `json:"field1"`
1224-
Field2 carbon.Carbon `json:"field2"`
1225-
Field3 carbon.Carbon `json:"field3"`
1226-
Field4 carbon.Carbon `json:"field4"`
1227-
1228-
Field5 carbon.Carbon `json:"field5"`
1229-
Field6 carbon.Carbon `json:"field6"`
1230-
Field7 carbon.Carbon `json:"field7"`
1231-
Field8 carbon.Carbon `json:"field8"`
1232-
}
1233-
1234-
now := carbon.Parse("2020-08-05 13:14:15", carbon.PRC)
1235-
person := Person {
1236-
Name: "gouguoyin",
1237-
Age: 18,
1238-
1239-
Field1: now,
1240-
Field2: now,
1241-
Field3: now,
1242-
Field4: now,
1243-
Field5: now,
1244-
Field6: now,
1245-
Field7: now,
1246-
Field8: now,
1247-
}
1248-
1249-
data, marshalErr := json.Marshal(person)
1250-
if marshalErr != nil {
1251-
// 错误处理
1252-
log.Fatal(marshalErr)
1253-
}
1254-
fmt.Printf("%s", data)
1255-
// 输出
1256-
{
1257-
"name": "gouguoyin",
1258-
"age": 18,
1259-
"field1": "2020-08-05 13:14:15",
1260-
"field2": "2020-08-05 13:14:15",
1261-
"field3": "2020-08-05 13:14:15",
1262-
"field4": "2020-08-05 13:14:15",
1263-
"field5": "2020-08-05 13:14:15",
1264-
"field6": "2020-08-05 13:14:15",
1265-
"field7": "2020-08-05 13:14:15",
1266-
"field8": "2020-08-05 13:14:15"
1267-
}
1268-
1269-
unmarshalErr := json.Unmarshal(data, &person)
1270-
if unmarshalErr != nil {
1271-
// 错误处理
1272-
log.Fatal(unmarshalErr)
1273-
}
1274-
1275-
fmt.Printf("%s", person.Field1) // 2020-08-05 13:14:15
1276-
fmt.Printf("%s", person.Field2) // 2020-08-05 13:14:15
1277-
fmt.Printf("%s", person.Field3) // 2020-08-05 13:14:15
1278-
fmt.Printf("%s", person.Field4) // 2020-08-05 13:14:15
1279-
1280-
fmt.Printf("%s", person.Field5) // 2020-08-05 13:14:15
1281-
fmt.Printf("%s", person.Field6) // 2020-08-05 13:14:15
1282-
fmt.Printf("%s", person.Field7) // 2020-08-05 13:14:15
1283-
fmt.Printf("%s", person.Field8) // 2020-08-05 13:14:15
1284-
```
1285-
1286-
###### 场景二: 不同时间字段有不同的格式
12871212
```go
12881213
type Person struct {
12891214
Name string `json:"name"`
12901215
Age int `json:"age"`
1216+
Birthday0 carbon.Carbon `json:"birthday0"`
12911217
Birthday1 carbon.DateTime `json:"birthday1"`
12921218
Birthday2 carbon.DateTimeMilli `json:"birthday2"`
12931219
Birthday3 carbon.DateTimeMicro `json:"birthday3"`
@@ -1309,6 +1235,7 @@ type Person struct {
13091235
person := Person {
13101236
Name: "gouguoyin",
13111237
Age: 18,
1238+
Birthday0: carbon.Now().SubYears(18),
13121239
Birthday1: carbon.Now().SubYears(18).ToDateTimeStruct(),
13131240
Birthday2: carbon.Now().SubYears(18).ToDateTimeMilliStruct(),
13141241
Birthday3: carbon.Now().SubYears(18).ToDateTimeMicroStruct(),
@@ -1337,6 +1264,7 @@ fmt.Printf("%s", data)
13371264
{
13381265
"name": "gouguoyin",
13391266
"age": 18,
1267+
"birthday0": "2003-07-16 13:14:15",
13401268
"birthday1": "2003-07-16 13:14:15",
13411269
"birthday2": "2003-07-16 13:14:15.999",
13421270
"birthday3": "2003-07-16 13:14:15.999999",
@@ -1361,6 +1289,7 @@ if err != nil {
13611289
log.Fatal(err)
13621290
}
13631291

1292+
person.Birthday0.String() // 2003-07-16 13:14:15
13641293
person.Birthday1.String() // 2003-07-16 13:14:15
13651294
person.Birthday2.String() // 2003-07-16 13:14:15.999
13661295
person.Birthday3.String() // 2003-07-16 13:14:15.999999

README.jp.md

Lines changed: 4 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,84 +1211,11 @@ carbon.Parse("2020-08-05 13:14:15").IsWinter() // false
12111211

12121212
##### JSON
12131213

1214-
###### シナリオ 1: すべての時刻フィールドが同じ形式である
1215-
```go
1216-
carbon.SetDefault(carbon.Default{
1217-
Layout: carbon.DateTimeLayout,
1218-
})
1219-
1220-
type Person struct {
1221-
Name string `json:"name"`
1222-
Age int `json:"age"`
1223-
1224-
Field1 carbon.Carbon `json:"field1"`
1225-
Field2 carbon.Carbon `json:"field2"`
1226-
Field3 carbon.Carbon `json:"field3"`
1227-
Field4 carbon.Carbon `json:"field4"`
1228-
1229-
Field5 carbon.Carbon `json:"field5"`
1230-
Field6 carbon.Carbon `json:"field6"`
1231-
Field7 carbon.Carbon `json:"field7"`
1232-
Field8 carbon.Carbon `json:"field8"`
1233-
}
1234-
1235-
now := carbon.Parse("2020-08-05 13:14:15", carbon.PRC)
1236-
person := Person {
1237-
Name: "gouguoyin",
1238-
Age: 18,
1239-
1240-
Field1: now,
1241-
Field2: now,
1242-
Field3: now,
1243-
Field4: now,
1244-
Field5: now,
1245-
Field6: now,
1246-
Field7: now,
1247-
Field8: now,
1248-
}
1249-
1250-
data, marshalErr := json.Marshal(person)
1251-
if marshalErr != nil {
1252-
// エラー処理...
1253-
log.Fatal(marshalErr)
1254-
}
1255-
fmt.Printf("%s", data)
1256-
// 出力
1257-
{
1258-
"name": "gouguoyin",
1259-
"age": 18,
1260-
"field1": "2020-08-05 13:14:15",
1261-
"field2": "2020-08-05 13:14:15",
1262-
"field3": "2020-08-05 13:14:15",
1263-
"field4": "2020-08-05 13:14:15",
1264-
"field5": "2020-08-05 13:14:15",
1265-
"field6": "2020-08-05 13:14:15",
1266-
"field7": "2020-08-05 13:14:15",
1267-
"field8": "2020-08-05 13:14:15"
1268-
}
1269-
1270-
unmarshalErr := json.Unmarshal(data, &person)
1271-
if unmarshalErr != nil {
1272-
// エラー処理...
1273-
log.Fatal(unmarshalErr)
1274-
}
1275-
1276-
fmt.Printf("%s", person.Field1) // 2020-08-05 13:14:15
1277-
fmt.Printf("%s", person.Field2) // 2020-08-05 13:14:15
1278-
fmt.Printf("%s", person.Field3) // 2020-08-05 13:14:15
1279-
fmt.Printf("%s", person.Field4) // 2020-08-05 13:14:15
1280-
1281-
fmt.Printf("%s", person.Field5) // 2020-08-05 13:14:15
1282-
fmt.Printf("%s", person.Field6) // 2020-08-05 13:14:15
1283-
fmt.Printf("%s", person.Field7) // 2020-08-05 13:14:15
1284-
fmt.Printf("%s", person.Field8) // 2020-08-05 13:14:15
1285-
```
1286-
1287-
###### シナリオ 2: 異なる時刻フィールドは異なる形式を持つ
12881214
```go
12891215
type Person struct {
12901216
Name string `json:"name"`
12911217
Age int `json:"age"`
1218+
Birthday0 carbon.Carbon `json:"birthday0"`
12921219
Birthday1 carbon.DateTime `json:"birthday1"`
12931220
Birthday2 carbon.DateTimeMilli `json:"birthday2"`
12941221
Birthday3 carbon.DateTimeMicro `json:"birthday3"`
@@ -1310,6 +1237,7 @@ type Person struct {
13101237
person := Person {
13111238
Name: "gouguoyin",
13121239
Age: 18,
1240+
Birthday0: carbon.Now().SubYears(18),
13131241
Birthday1: carbon.Now().SubYears(18).ToDateTimeStruct(),
13141242
Birthday2: carbon.Now().SubYears(18).ToDateTimeMilliStruct(),
13151243
Birthday3: carbon.Now().SubYears(18).ToDateTimeMicroStruct(),
@@ -1338,6 +1266,7 @@ fmt.Printf("%s", data)
13381266
{
13391267
"name": "gouguoyin",
13401268
"age": 18,
1269+
"birthday0": "2003-07-16 13:14:15",
13411270
"birthday1": "2003-07-16 13:14:15",
13421271
"birthday2": "2003-07-16 13:14:15.999",
13431272
"birthday3": "2003-07-16 13:14:15.999999",
@@ -1362,6 +1291,7 @@ if err != nil {
13621291
log.Fatal(err)
13631292
}
13641293

1294+
person.Birthday0.String() // 2003-07-16 13:14:15
13651295
person.Birthday1.String() // 2003-07-16 13:14:15
13661296
person.Birthday2.String() // 2003-07-16 13:14:15.999
13671297
person.Birthday3.String() // 2003-07-16 13:14:15.999999

README.md

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,85 +1205,11 @@ carbon.Parse("2020-08-05 13:14:15").IsWinter() // false
12051205

12061206
##### JSON
12071207

1208-
###### Scene one: all time fields have the same format
1209-
```go
1210-
carbon.SetDefault(carbon.Default{
1211-
Layout: carbon.DateTimeLayout,
1212-
})
1213-
1214-
type Person struct {
1215-
Name string `json:"name"`
1216-
Age int `json:"age"`
1217-
1218-
Field1 carbon.Carbon `json:"field1"`
1219-
Field2 carbon.Carbon `json:"field2"`
1220-
Field3 carbon.Carbon `json:"field3"`
1221-
Field4 carbon.Carbon `json:"field4"`
1222-
1223-
Field5 carbon.Carbon `json:"field5"`
1224-
Field6 carbon.Carbon `json:"field6"`
1225-
Field7 carbon.Carbon `json:"field7"`
1226-
Field8 carbon.Carbon `json:"field8"`
1227-
}
1228-
1229-
now := carbon.Parse("2020-08-05 13:14:15", carbon.PRC)
1230-
person := Person {
1231-
Name: "gouguoyin",
1232-
Age: 18,
1233-
1234-
Field1: now,
1235-
Field2: now,
1236-
Field3: now,
1237-
Field4: now,
1238-
Field5: now,
1239-
Field6: now,
1240-
Field7: now,
1241-
Field8: now,
1242-
}
1243-
1244-
data, marshalErr := json.Marshal(person)
1245-
if marshalErr != nil {
1246-
// Error handle...
1247-
log.Fatal(marshalErr)
1248-
}
1249-
fmt.Printf("%s", data)
1250-
// Output
1251-
{
1252-
"name": "gouguoyin",
1253-
"age": 18,
1254-
"field1": "2020-08-05 13:14:15",
1255-
"field2": "2020-08-05 13:14:15",
1256-
"field3": "2020-08-05 13:14:15",
1257-
"field4": "2020-08-05 13:14:15",
1258-
"field5": "2020-08-05 13:14:15",
1259-
"field6": "2020-08-05 13:14:15",
1260-
"field7": "2020-08-05 13:14:15",
1261-
"field8": "2020-08-05 13:14:15"
1262-
}
1263-
1264-
var person Person
1265-
unmarshalErr := json.Unmarshal(data, &person)
1266-
if unmarshalErr != nil {
1267-
// Error handle...
1268-
log.Fatal(unmarshalErr)
1269-
}
1270-
1271-
fmt.Printf("%s", person.Field1) // 2020-08-05 13:14:15
1272-
fmt.Printf("%s", person.Field2) // 2020-08-05 13:14:15
1273-
fmt.Printf("%s", person.Field3) // 2020-08-05 13:14:15
1274-
fmt.Printf("%s", person.Field4) // 2020-08-05 13:14:15
1275-
1276-
fmt.Printf("%s", person.Field5) // 2020-08-05 13:14:15
1277-
fmt.Printf("%s", person.Field6) // 2020-08-05 13:14:15
1278-
fmt.Printf("%s", person.Field7) // 2020-08-05 13:14:15
1279-
fmt.Printf("%s", person.Field8) // 2020-08-05 13:14:15
1280-
```
1281-
1282-
###### Scene two: different time fields have different formats
12831208
```go
12841209
type Person struct {
12851210
Name string `json:"name"`
12861211
Age int `json:"age"`
1212+
Birthday0 carbon.Carbon `json:"birthday0"`
12871213
Birthday1 carbon.DateTime `json:"birthday1"`
12881214
Birthday2 carbon.DateTimeMilli `json:"birthday2"`
12891215
Birthday3 carbon.DateTimeMicro `json:"birthday3"`
@@ -1305,6 +1231,7 @@ type Person struct {
13051231
person := Person {
13061232
Name: "gouguoyin",
13071233
Age: 18,
1234+
Birthday0: carbon.Now().SubYears(18),
13081235
Birthday1: carbon.Now().SubYears(18).ToDateTimeStruct(),
13091236
Birthday2: carbon.Now().SubYears(18).ToDateTimeMilliStruct(),
13101237
Birthday3: carbon.Now().SubYears(18).ToDateTimeMicroStruct(),
@@ -1333,6 +1260,7 @@ fmt.Printf("%s", data)
13331260
{
13341261
"name": "gouguoyin",
13351262
"age": 18,
1263+
"birthday0": "2003-07-16 13:14:15",
13361264
"birthday1": "2003-07-16 13:14:15",
13371265
"birthday2": "2003-07-16 13:14:15.999",
13381266
"birthday3": "2003-07-16 13:14:15.999999",
@@ -1357,6 +1285,7 @@ if err != nil {
13571285
log.Fatal(err)
13581286
}
13591287

1288+
person.Birthday0.String() // 2003-07-16 13:14:15
13601289
person.Birthday1.String() // 2003-07-16 13:14:15
13611290
person.Birthday2.String() // 2003-07-16 13:14:15.999
13621291
person.Birthday3.String() // 2003-07-16 13:14:15.999999

0 commit comments

Comments
 (0)