2020-04-19 17:13:24 +03:00
|
|
|
package json_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Encoder(t *testing.T) {
|
|
|
|
t.Run("int", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(-10)
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "int", `-10`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("int8", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(int8(-11))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "int8", `-11`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("int16", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(int16(-12))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "int16", `-12`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("int32", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(int32(-13))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "int32", `-13`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("int64", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(int64(-14))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "int64", `-14`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("uint", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(uint(10))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "uint", `10`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("uint8", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(uint8(11))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "uint8", `11`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("uint16", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(uint16(12))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "uint16", `12`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("uint32", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(uint32(13))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "uint32", `13`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("uint64", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(uint64(14))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "uint64", `14`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("float32", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(float32(3.14))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "float32", `3.14`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("float64", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(float64(3.14))
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "float64", `3.14`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("bool", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(true)
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "bool", `true`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("string", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal("hello world")
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "string", `"hello world"`, string(bytes))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("struct", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal(struct {
|
|
|
|
A int `json:"a"`
|
|
|
|
B uint `json:"b"`
|
|
|
|
C string `json:"c"`
|
2020-04-21 07:38:48 +03:00
|
|
|
D int `json:"-"` // ignore field
|
|
|
|
a int `json:"aa"` // private field
|
2020-04-19 17:13:24 +03:00
|
|
|
}{
|
|
|
|
A: -1,
|
|
|
|
B: 1,
|
|
|
|
C: "hello world",
|
|
|
|
})
|
2020-04-21 07:19:53 +03:00
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "struct", `{"a":-1,"b":1,"c":"hello world"}`, string(bytes))
|
2020-05-01 07:12:01 +03:00
|
|
|
t.Run("null", func(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
A *struct{} `json:"a"`
|
|
|
|
}
|
|
|
|
var v T
|
|
|
|
bytes, err := json.Marshal(&v)
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "struct", `{"a":null}`, string(bytes))
|
|
|
|
})
|
|
|
|
t.Run("omitempty", func(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
A int `json:",omitempty"`
|
|
|
|
B int8 `json:",omitempty"`
|
|
|
|
C int16 `json:",omitempty"`
|
|
|
|
D int32 `json:",omitempty"`
|
|
|
|
E int64 `json:",omitempty"`
|
|
|
|
F uint `json:",omitempty"`
|
|
|
|
G uint8 `json:",omitempty"`
|
|
|
|
H uint16 `json:",omitempty"`
|
|
|
|
I uint32 `json:",omitempty"`
|
|
|
|
J uint64 `json:",omitempty"`
|
|
|
|
K float32 `json:",omitempty"`
|
|
|
|
L float64 `json:",omitempty"`
|
|
|
|
O string `json:",omitempty"`
|
|
|
|
P bool `json:",omitempty"`
|
|
|
|
Q []int `json:",omitempty"`
|
|
|
|
R map[string]interface{} `json:",omitempty"`
|
|
|
|
S *struct{} `json:",omitempty"`
|
|
|
|
T int `json:"t,omitempty"`
|
|
|
|
}
|
|
|
|
var v T
|
|
|
|
v.T = 1
|
|
|
|
bytes, err := json.Marshal(&v)
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "struct", `{"t":1}`, string(bytes))
|
|
|
|
})
|
|
|
|
t.Run("head_omitempty", func(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
A *struct{} `json:"a,omitempty"`
|
|
|
|
}
|
|
|
|
var v T
|
|
|
|
bytes, err := json.Marshal(&v)
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "struct", `{}`, string(bytes))
|
|
|
|
})
|
|
|
|
t.Run("pointer_head_omitempty", func(t *testing.T) {
|
|
|
|
type V struct{}
|
|
|
|
type U struct {
|
|
|
|
B *V `json:"b,omitempty"`
|
|
|
|
}
|
|
|
|
type T struct {
|
|
|
|
A *U `json:"a"`
|
|
|
|
}
|
|
|
|
bytes, err := json.Marshal(&T{A: &U{}})
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "struct", `{"a":{}}`, string(bytes))
|
|
|
|
})
|
|
|
|
t.Run("head_int_omitempty", func(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
A int `json:"a,omitempty"`
|
|
|
|
}
|
|
|
|
var v T
|
|
|
|
bytes, err := json.Marshal(&v)
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "struct", `{}`, string(bytes))
|
|
|
|
})
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
|
|
|
t.Run("slice", func(t *testing.T) {
|
2020-04-30 07:39:47 +03:00
|
|
|
t.Run("[]int", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal([]int{1, 2, 3, 4})
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "[]int", `[1,2,3,4]`, string(bytes))
|
|
|
|
})
|
|
|
|
t.Run("[]interface{}", func(t *testing.T) {
|
|
|
|
bytes, err := json.Marshal([]interface{}{1, 2.1, "hello"})
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "[]interface{}", `[1,2.1,"hello"]`, string(bytes))
|
|
|
|
})
|
2020-04-21 07:19:53 +03:00
|
|
|
})
|
2020-04-30 07:39:47 +03:00
|
|
|
|
2020-04-21 07:19:53 +03:00
|
|
|
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))
|
2020-04-19 17:13:24 +03:00
|
|
|
})
|
2020-04-20 18:06:27 +03:00
|
|
|
t.Run("map", func(t *testing.T) {
|
2020-04-28 12:25:51 +03:00
|
|
|
t.Run("map[string]int", 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)))
|
|
|
|
})
|
|
|
|
t.Run("map[string]interface{}", func(t *testing.T) {
|
|
|
|
type T struct {
|
|
|
|
A int
|
|
|
|
}
|
|
|
|
v := map[string]interface{}{
|
|
|
|
"a": 1,
|
|
|
|
"b": 2.1,
|
|
|
|
"c": &T{
|
|
|
|
A: 10,
|
|
|
|
},
|
|
|
|
"d": 4,
|
|
|
|
}
|
|
|
|
bytes, err := json.Marshal(v)
|
|
|
|
assertErr(t, err)
|
|
|
|
assertEq(t, "map[string]interface{}", len(`{"a":1,"b":2.1,"c":{"A":10},"d":4}`), len(string(bytes)))
|
2020-04-20 18:06:27 +03:00
|
|
|
})
|
|
|
|
})
|
2020-04-19 17:13:24 +03:00
|
|
|
}
|