github.com/goravel/framework@v1.13.9/support/carbon/json_test.go (about)

     1  package carbon
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  type Person struct {
    12  	Name         string         `json:"name"`
    13  	Age          int            `json:"age"`
    14  	Birthday1    DateTime       `json:"birthday1"`
    15  	Birthday2    DateTimeMilli  `json:"birthday2"`
    16  	Birthday3    DateTimeMicro  `json:"birthday3"`
    17  	Birthday4    DateTimeNano   `json:"birthday4"`
    18  	GraduatedAt1 Date           `json:"graduated_at1"`
    19  	GraduatedAt2 DateMilli      `json:"graduated_at2"`
    20  	GraduatedAt3 DateMicro      `json:"graduated_at3"`
    21  	GraduatedAt4 DateNano       `json:"graduated_at4"`
    22  	CreatedAt1   Timestamp      `json:"created_at1"`
    23  	CreatedAt2   TimestampMilli `json:"created_at2"`
    24  	CreatedAt3   TimestampMicro `json:"created_at3"`
    25  	CreatedAt4   TimestampNano  `json:"created_at4"`
    26  }
    27  
    28  var person Person
    29  
    30  func TestMarshalJSON(t *testing.T) {
    31  	person = Person{
    32  		Name:         "goravel",
    33  		Age:          18,
    34  		Birthday1:    DateTime{Now().SubYears(18)},
    35  		Birthday2:    DateTimeMilli{Now().SubYears(18)},
    36  		Birthday3:    DateTimeMicro{Now().SubYears(18)},
    37  		Birthday4:    DateTimeNano{Now().SubYears(18)},
    38  		GraduatedAt1: Date{Parse("2020-08-05 13:14:15")},
    39  		GraduatedAt2: DateMilli{Parse("2020-08-05 13:14:15.999")},
    40  		GraduatedAt3: DateMicro{Parse("2020-08-05 13:14:15.999999")},
    41  		GraduatedAt4: DateNano{Parse("2020-08-05 13:14:15.999999999")},
    42  		CreatedAt1:   Timestamp{Parse("2023-08-05 13:14:15")},
    43  		CreatedAt2:   TimestampMilli{Parse("2024-08-05 13:14:15.999")},
    44  		CreatedAt3:   TimestampMicro{Parse("2025-08-05 13:14:15.999999")},
    45  		CreatedAt4:   TimestampNano{Parse("2025-08-05 13:14:15.999999999")},
    46  	}
    47  	data, err := json.Marshal(&person)
    48  	assert.Nil(t, err)
    49  	fmt.Printf("Person output by json:\n%s\n", data)
    50  }
    51  
    52  func TestUnmarshalJSON(t *testing.T) {
    53  	str := `{
    54  		"name": "goravel",
    55  		"age": 18,
    56  		"birthday1": "2003-07-16 16:22:02",
    57  		"birthday2": "2003-07-16 16:22:02.999",
    58  		"birthday3": "2003-07-16 16:22:02.999999",
    59  		"birthday4": "2003-07-16 16:22:02.999999999",
    60  		"graduated_at1": "2020-08-05",
    61  		"graduated_at2": "2020-08-05.999",
    62  		"graduated_at3": "2020-08-05.999999",
    63  		"graduated_at4": "2020-08-05.999999999",
    64  		"created_at1": 1596604455,
    65  		"created_at2": 1596604455999,
    66  		"created_at3": 1596604455999999,
    67  		"created_at4": 1596604455999999999
    68  	}`
    69  
    70  	err := json.Unmarshal([]byte(str), &person)
    71  	assert.Nil(t, err)
    72  	fmt.Printf("Json string parse to person:\n%+v\n", person)
    73  }
    74  
    75  func TestErrorJson(t *testing.T) {
    76  	str := `{
    77  		"name": "",
    78  		"age": 0,
    79  		"birthday1": "",
    80  		"birthday2": "",
    81  		"birthday3": "",
    82  		"birthday4": "",
    83  		"graduated_at1": "xxx",
    84  		"graduated_at2": "xxx",
    85  		"graduated_at3": "xxx",
    86  		"graduated_at4": "xxx",
    87  		"created_at1": 0,
    88  		"created_at2": 0,
    89  		"created_at3": 0,
    90  		"created_at4": 0
    91  	}`
    92  	err := json.Unmarshal([]byte(str), &person)
    93  	assert.NotNil(t, err)
    94  	fmt.Printf("Json string parse to person:\n%+v\n", person)
    95  }