package json_test import ( "testing" "github.com/goccy/go-json" ) func assertErr(t *testing.T, err error) { t.Helper() if err != nil { t.Fatalf("%+v", err) } } func assertEq(t *testing.T, msg string, exp interface{}, act interface{}) { t.Helper() if exp != act { t.Fatalf("failed to encode %s. exp=[%v] but act=[%v]", msg, exp, act) } } func Test_Encoder(t *testing.T) { t.Run("int", func(t *testing.T) { bytes, err := json.Marshal(-10) assertErr(t, err) assertEq(t, "int", `-10`, string(bytes)) }) t.Run("int8", func(t *testing.T) { bytes, err := json.Marshal(int8(-11)) assertErr(t, err) assertEq(t, "int8", `-11`, string(bytes)) }) t.Run("int16", func(t *testing.T) { bytes, err := json.Marshal(int16(-12)) assertErr(t, err) assertEq(t, "int16", `-12`, string(bytes)) }) t.Run("int32", func(t *testing.T) { bytes, err := json.Marshal(int32(-13)) assertErr(t, err) assertEq(t, "int32", `-13`, string(bytes)) }) t.Run("int64", func(t *testing.T) { bytes, err := json.Marshal(int64(-14)) assertErr(t, err) assertEq(t, "int64", `-14`, string(bytes)) }) t.Run("uint", func(t *testing.T) { bytes, err := json.Marshal(uint(10)) assertErr(t, err) assertEq(t, "uint", `10`, string(bytes)) }) t.Run("uint8", func(t *testing.T) { bytes, err := json.Marshal(uint8(11)) assertErr(t, err) assertEq(t, "uint8", `11`, string(bytes)) }) t.Run("uint16", func(t *testing.T) { bytes, err := json.Marshal(uint16(12)) assertErr(t, err) assertEq(t, "uint16", `12`, string(bytes)) }) t.Run("uint32", func(t *testing.T) { bytes, err := json.Marshal(uint32(13)) assertErr(t, err) assertEq(t, "uint32", `13`, string(bytes)) }) t.Run("uint64", func(t *testing.T) { bytes, err := json.Marshal(uint64(14)) assertErr(t, err) assertEq(t, "uint64", `14`, string(bytes)) }) t.Run("float32", func(t *testing.T) { bytes, err := json.Marshal(float32(3.14)) assertErr(t, err) assertEq(t, "float32", `3.14`, string(bytes)) }) t.Run("float64", func(t *testing.T) { bytes, err := json.Marshal(float64(3.14)) assertErr(t, err) assertEq(t, "float64", `3.14`, string(bytes)) }) t.Run("bool", func(t *testing.T) { bytes, err := json.Marshal(true) assertErr(t, err) assertEq(t, "bool", `true`, string(bytes)) }) t.Run("string", func(t *testing.T) { bytes, err := json.Marshal("hello world") assertErr(t, err) assertEq(t, "string", `"hello world"`, string(bytes)) }) t.Run("struct", func(t *testing.T) { bytes, err := json.Marshal(struct { A int `json:"a"` B uint `json:"b"` C string `json:"c"` }{ A: -1, B: 1, C: "hello world", }) assertErr(t, err) assertEq(t, "struct", `{"a":-1,"b":1,"c":"hello world"}`, string(bytes)) }) t.Run("slice", func(t *testing.T) { bytes, err := json.Marshal([]int{1, 2, 3, 4}) assertErr(t, err) assertEq(t, "slice", `[1,2,3,4]`, string(bytes)) }) t.Run("array", func(t *testing.T) { bytes, err := json.Marshal([4]int{1, 2, 3, 4}) assertErr(t, err) assertEq(t, "array", `[1,2,3,4]`, string(bytes)) }) t.Run("map", func(t *testing.T) { bytes, err := json.Marshal(map[string]int{ "a": 1, "b": 2, "c": 3, "d": 4, }) assertErr(t, err) assertEq(t, "map", len(`{"a":1,"b":2,"c":3,"d":4}`), len(string(bytes))) }) }