-
Notifications
You must be signed in to change notification settings - Fork 5
/
json_unmarshal_test.go
128 lines (117 loc) · 2.94 KB
/
json_unmarshal_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package goson
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUnmarshal(t *testing.T) {
type Slice struct {
FieldA uint16 `json:"fieldA"`
FieldB string `json:"fieldB"`
Exist string `json:"exist"`
Test float32 `json:"test"`
}
type Model struct {
ID int `json:"id"`
Name string `json:"name"`
MustFloat *float64 `json:"mustFloat"`
MustInt int `json:"mustInt"`
Uint uint `json:"uint"`
IsExist *bool `json:"isExist"`
Obj *struct {
N int `json:"n"`
Testing struct {
Ss int `json:"ss"`
} `json:"testing"`
} `json:"obj"`
Slice []Slice `json:"slice"`
Strings []*string `json:"str"`
Ints []int `json:"ints"`
Bools []bool `json:"bools"`
NoTag string
skip string // cannot set value to this field because unexported
Interface interface{} `json:"interface"`
}
t.Run("Testcase #1: Testing Unmarshal with root is JSON Object", func(t *testing.T) {
data := []byte(`{
"id": "01",
"name": "agungdp",
"mustFloat": "2.23423",
"mustInt": 2.23423,
"uint": 11,
"isExist": "true",
"obj": {
"n": 2,
"testing": {
"ss": "23840923849284"
}
},
"slice": [
{
"fieldA": "100",
"fieldB": 3000,
"exist": true,
"test": "3.14"
},
{
"fieldA": 50000,
"fieldB": "123000",
"exist": 3000,
"test": 1323.123
}
],
"str": ["a", true],
"ints": ["2", 3],
"bools": [1, "true", "0", true],
"NoTag": 19283091832,
"interface": 1
}`)
var target Model
err := Unmarshal(data, &target)
assert.NoError(t, err)
assert.NotNil(t, target.MustFloat)
assert.Equal(t, uint(11), target.Uint)
assert.Equal(t, 23840923849284, target.Obj.Testing.Ss)
assert.Equal(t, "true", target.Slice[0].Exist)
assert.Equal(t, "true", *target.Strings[1])
assert.Equal(t, 2, target.Ints[0])
assert.Equal(t, false, target.Bools[2])
assert.Equal(t, "19283091832", target.NoTag)
assert.NotNil(t, target.Interface)
fmt.Printf("%+v\n\n", target)
})
t.Run("Testcase #2: Testing Unmarshal with root is JSON Array", func(t *testing.T) {
data := []byte(`[
{
"fieldA": 100,
"fieldB": "3000",
"exist": "true",
"test": 3.14
},
{
"fieldA": 50000,
"fieldB": "123000",
"exist": "3000",
"test": 1323.123
}
]`)
var target []Slice
err := Unmarshal(data, &target)
assert.NoError(t, err)
assert.NotNil(t, target)
assert.Len(t, target, 2)
fmt.Printf("%+v\n\n", target)
})
t.Run("Testcase #3: Testing error unmarshal (target is not pointer)", func(t *testing.T) {
data := []byte(`{}`)
var target Model
err := Unmarshal(data, target)
assert.Error(t, err)
})
t.Run("Testcase #4: Testing error unmarshal (invalid json format)", func(t *testing.T) {
data := []byte(`{1: satu}`)
var target Model
err := Unmarshal(data, &target)
assert.Error(t, err)
})
}