2018-02-13 17:49:40 +02:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2018-02-14 02:48:12 +02:00
|
|
|
"bytes"
|
2018-02-24 16:04:41 +02:00
|
|
|
"encoding/json"
|
|
|
|
"time"
|
2018-02-13 17:49:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-02-14 02:58:59 +02:00
|
|
|
var pString = func(val string) *string {
|
|
|
|
return &val
|
|
|
|
}
|
2018-02-16 09:42:37 +02:00
|
|
|
epoch := time.Unix(0, 0)
|
2018-02-13 17:49:40 +02:00
|
|
|
unmarshalCases = append(unmarshalCases, unmarshalCase{
|
|
|
|
ptr: (*struct {
|
|
|
|
Field interface{}
|
|
|
|
})(nil),
|
|
|
|
input: `{"Field": "hello"}`,
|
2018-02-22 04:37:32 +02:00
|
|
|
}, unmarshalCase{
|
|
|
|
ptr: (*struct {
|
|
|
|
Field interface{}
|
|
|
|
})(nil),
|
|
|
|
input: `{"Field": "hello"} `,
|
2018-02-14 02:39:18 +02:00
|
|
|
}, unmarshalCase{
|
|
|
|
ptr: (*struct {
|
|
|
|
Field int `json:"field"`
|
|
|
|
})(nil),
|
|
|
|
input: `{"field": null}`,
|
2020-08-03 04:12:41 +02:00
|
|
|
}, unmarshalCase{
|
|
|
|
ptr: (*struct {
|
|
|
|
Field int `json:"field,string"`
|
|
|
|
})(nil),
|
|
|
|
input: `{"field": null}`,
|
2018-02-14 02:48:12 +02:00
|
|
|
}, 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"}`,
|
2018-02-14 02:58:59 +02:00
|
|
|
}, 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]}`,
|
2018-02-14 04:13:34 +02:00
|
|
|
}, 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"}}`,
|
2018-03-20 15:43:30 +02:00
|
|
|
}, 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"}`,
|
2018-03-24 16:38:32 +02:00
|
|
|
}, unmarshalCase{
|
|
|
|
ptr: (*struct {
|
|
|
|
T float64 `json:"T"`
|
|
|
|
})(nil),
|
|
|
|
input: `{"t":10.0}`,
|
|
|
|
}, unmarshalCase{
|
|
|
|
ptr: (*struct {
|
|
|
|
T float64 `json:"T"`
|
|
|
|
})(nil),
|
|
|
|
input: `{"T":10.0}`,
|
2018-04-18 10:22:47 +02:00
|
|
|
}, unmarshalCase{
|
|
|
|
ptr: (*struct {
|
|
|
|
T float64 `json:"t"`
|
|
|
|
})(nil),
|
|
|
|
input: `{"T":10.0}`,
|
2018-07-01 07:05:25 +02:00
|
|
|
}, 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]]}`,
|
2018-02-13 17:49:40 +02:00
|
|
|
})
|
|
|
|
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
|
2018-02-16 09:42:37 +02:00
|
|
|
}{&epoch},
|
2018-02-13 17:49:40 +02:00
|
|
|
struct {
|
|
|
|
*StructVarious
|
|
|
|
}{&StructVarious{}},
|
|
|
|
struct {
|
|
|
|
*StructVarious
|
2018-02-16 11:32:41 +02:00
|
|
|
Field int
|
|
|
|
}{nil, 10},
|
2018-02-13 17:49:40 +02:00
|
|
|
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")},
|
2018-02-14 02:48:12 +02:00
|
|
|
struct {
|
|
|
|
Field1 string `json:"field-1,omitempty"`
|
|
|
|
Field2 func() `json:"-"`
|
|
|
|
}{},
|
2018-02-21 11:59:41 +02:00
|
|
|
structRecursive{},
|
2018-02-14 02:48:12 +02:00
|
|
|
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{},
|
2018-02-14 02:58:59 +02:00
|
|
|
struct {
|
|
|
|
Field1 *string
|
|
|
|
Field2 *string
|
|
|
|
}{Field2: pString("world")},
|
2018-02-14 04:13:34 +02:00
|
|
|
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),
|
|
|
|
},
|
2020-03-26 08:46:47 +02:00
|
|
|
struct {
|
|
|
|
_UnderscoreField string
|
|
|
|
}{
|
|
|
|
"should not marshal",
|
|
|
|
},
|
2018-02-13 17:49:40 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
type StructVarious struct {
|
|
|
|
Field0 string
|
|
|
|
Field1 []string
|
|
|
|
Field2 map[string]interface{}
|
2018-02-14 02:48:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-02-16 11:32:41 +02:00
|
|
|
}
|