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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package test
import (
"bytes"
"encoding/json"
"time"
)
func init() {
var pString = func(val string) *string {
return &val
}
epoch := time.Unix(0, 0)
unmarshalCases = append(unmarshalCases, unmarshalCase{
ptr: (*struct {
Field interface{}
})(nil),
input: `{"Field": "hello"}`,
}, unmarshalCase{
ptr: (*struct {
Field interface{}
})(nil),
input: `{"Field": "hello"} `,
}, unmarshalCase{
ptr: (*struct {
Field int `json:"field"`
})(nil),
input: `{"field": null}`,
}, unmarshalCase{
ptr: (*struct {
Field int `json:"field,string"`
})(nil),
input: `{"field": null}`,
}, unmarshalCase{
ptr: (*struct {
ID int `json:"id"`
Payload map[string]interface{} `json:"payload"`
buf *bytes.Buffer
})(nil),
input: ` {"id":1, "payload":{"account":"123","password":"456"}}`,
}, unmarshalCase{
ptr: (*struct {
Field1 string
})(nil),
input: `{"Field\"1":"hello"}`,
}, unmarshalCase{
ptr: (*struct {
Field1 string
})(nil),
input: `{"\u0046ield1":"hello"}`,
}, unmarshalCase{
ptr: (*struct {
Field1 *string
Field2 *string
})(nil),
input: `{"field1": null, "field2": "world"}`,
}, unmarshalCase{
ptr: (*struct {
Field1 string
Field2 json.RawMessage
})(nil),
input: `{"field1": "hello", "field2":[1,2,3]}`,
}, unmarshalCase{
ptr: (*struct {
a int
b <-chan int
C int
d *time.Timer
})(nil),
input: `{"a": 444, "b":"bad", "C":256, "d":{"not":"a timer"}}`,
}, unmarshalCase{
ptr: (*struct {
A string
B string
C string
D string
E string
F string
G string
H string
I string
J string
K string
})(nil),
input: `{"a":"1","b":"2","c":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`,
}, unmarshalCase{
ptr: (*struct {
T float64 `json:"T"`
})(nil),
input: `{"t":10.0}`,
}, unmarshalCase{
ptr: (*struct {
T float64 `json:"T"`
})(nil),
input: `{"T":10.0}`,
}, unmarshalCase{
ptr: (*struct {
T float64 `json:"t"`
})(nil),
input: `{"T":10.0}`,
}, unmarshalCase{
ptr: (*struct {
KeyString string `json:"key_string"`
Type string `json:"type"`
Asks [][2]float64 `json:"asks"`
})(nil),
input: `{"key_string": "KEYSTRING","type": "TYPE","asks": [[1e+66,1]]}`,
})
marshalCases = append(marshalCases,
struct {
Field map[string]interface{}
}{
map[string]interface{}{"hello": "world"},
},
struct {
Field map[string]interface{}
Field2 string
}{
map[string]interface{}{"hello": "world"}, "",
},
struct {
Field interface{}
}{
1024,
},
struct {
Field MyInterface
}{
MyString("hello"),
},
struct {
F *float64
}{},
struct {
*time.Time
}{&epoch},
struct {
*StructVarious
}{&StructVarious{}},
struct {
*StructVarious
Field int
}{nil, 10},
struct {
Field1 int
Field2 [1]*float64
}{},
struct {
Field interface{} `json:"field,omitempty"`
}{},
struct {
Field MyInterface `json:"field,omitempty"`
}{},
struct {
Field MyInterface `json:"field,omitempty"`
}{MyString("hello")},
struct {
Field json.Marshaler `json:"field"`
}{},
struct {
Field MyInterface `json:"field"`
}{},
struct {
Field MyInterface `json:"field"`
}{MyString("hello")},
struct {
Field1 string `json:"field-1,omitempty"`
Field2 func() `json:"-"`
}{},
structRecursive{},
struct {
*CacheItem
// Omit bad keys
OmitMaxAge omit `json:"cacheAge,omitempty"`
// Add nice keys
MaxAge int `json:"max_age"`
}{
CacheItem: &CacheItem{
Key: "value",
MaxAge: 100,
},
MaxAge: 20,
},
structOrder{},
struct {
Field1 *string
Field2 *string
}{Field2: pString("world")},
struct {
a int
b <-chan int
C int
d *time.Timer
}{
a: 42,
b: make(<-chan int, 10),
C: 21,
d: time.NewTimer(10 * time.Second),
},
struct {
_UnderscoreField string
}{
"should not marshal",
},
)
}
type StructVarious struct {
Field0 string
Field1 []string
Field2 map[string]interface{}
}
type structRecursive struct {
Field1 string
Me *structRecursive
}
type omit *struct{}
type CacheItem struct {
Key string `json:"key"`
MaxAge int `json:"cacheAge"`
}
type orderA struct {
Field2 string
}
type orderC struct {
Field5 string
}
type orderB struct {
Field4 string
orderC
Field6 string
}
type structOrder struct {
Field1 string
orderA
Field3 string
orderB
Field7 string
}
|