1
0
mirror of https://github.com/json-iterator/go.git synced 2025-11-26 22:40:13 +02:00

use reflect2 for json.Marshaler

This commit is contained in:
Tao Wen
2018-02-16 15:42:37 +08:00
parent 0e2b54800a
commit a7a34507ab
5 changed files with 29 additions and 30 deletions

View File

@@ -10,6 +10,7 @@ func init() {
var pString = func(val string) *string {
return &val
}
epoch := time.Unix(0, 0)
unmarshalCases = append(unmarshalCases, unmarshalCase{
ptr: (*struct {
Field interface{}
@@ -83,13 +84,9 @@ func init() {
struct {
F *float64
}{},
// TODO: fix this
//struct {
// *time.Time
//}{},
struct {
*time.Time
}{&time.Time{}},
}{&epoch},
struct {
*StructVarious
}{&StructVarious{}},

View File

@@ -6,6 +6,7 @@ import (
"encoding/json"
"github.com/stretchr/testify/require"
"github.com/json-iterator/go"
"fmt"
)
type unmarshalCase struct {
@@ -19,6 +20,10 @@ var marshalCases = []interface{}{
nil,
}
type selectedMarshalCase struct {
marshalCase interface{}
}
func Test_unmarshal(t *testing.T) {
should := require.New(t)
for _, testCase := range unmarshalCases {
@@ -35,9 +40,16 @@ func Test_unmarshal(t *testing.T) {
func Test_marshal(t *testing.T) {
for _, testCase := range marshalCases {
selectedMarshalCase, found := testCase.(selectedMarshalCase)
if found {
marshalCases = []interface{}{selectedMarshalCase.marshalCase}
break
}
}
for i, testCase := range marshalCases {
var name string
if testCase != nil {
name = reflect.TypeOf(testCase).String()
name = fmt.Sprintf("[%v]%v/%s", i, testCase, reflect.TypeOf(testCase).String())
}
t.Run(name, func(t *testing.T) {
should := require.New(t)