| 1 |
package test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/json-iterator/go" |
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
func Test_use_number_for_unmarshal(t *testing.T) { |
| 12 |
should := require.New(t) |
| 13 |
api := jsoniter.Config{UseNumber: true}.Froze() |
| 14 |
var obj interface{} |
| 15 |
should.Nil(api.UnmarshalFromString("123", &obj)) |
| 16 |
should.Equal(json.Number("123"), obj) |
| 17 |
} |
| 18 |
|
| 19 |
func Test_customize_float_marshal(t *testing.T) { |
| 20 |
should := require.New(t) |
| 21 |
json := jsoniter.Config{MarshalFloatWith6Digits: true}.Froze() |
| 22 |
str, err := json.MarshalToString(float32(1.23456789)) |
| 23 |
should.Nil(err) |
| 24 |
should.Equal("1.234568", str) |
| 25 |
} |
| 26 |
|
| 27 |
func Test_customize_tag_key(t *testing.T) { |
| 28 |
|
| 29 |
type TestObject struct { |
| 30 |
Field string `orm:"field"` |
| 31 |
} |
| 32 |
|
| 33 |
should := require.New(t) |
| 34 |
json := jsoniter.Config{TagKey: "orm"}.Froze() |
| 35 |
str, err := json.MarshalToString(TestObject{"hello"}) |
| 36 |
should.Nil(err) |
| 37 |
should.Equal(`{"field":"hello"}`, str) |
| 38 |
} |
| 39 |
|
| 40 |
func Test_read_large_number_as_interface(t *testing.T) { |
| 41 |
should := require.New(t) |
| 42 |
var val interface{} |
| 43 |
err := jsoniter.Config{UseNumber: true}.Froze().UnmarshalFromString(`123456789123456789123456789`, &val) |
| 44 |
should.Nil(err) |
| 45 |
output, err := jsoniter.MarshalToString(val) |
| 46 |
should.Nil(err) |
| 47 |
should.Equal(`123456789123456789123456789`, output) |
| 48 |
} |
| 49 |
|
| 50 |
type caseSensitiveStruct struct { |
| 51 |
A string `json:"a"` |
| 52 |
B string `json:"b,omitempty"` |
| 53 |
C *C `json:"C,omitempty"` |
| 54 |
} |
| 55 |
|
| 56 |
type C struct { |
| 57 |
D int64 `json:"D,omitempty"` |
| 58 |
E *E `json:"e,omitempty"` |
| 59 |
} |
| 60 |
|
| 61 |
type E struct { |
| 62 |
F string `json:"F,omitempty"` |
| 63 |
} |
| 64 |
|
| 65 |
func Test_CaseSensitive(t *testing.T) { |
| 66 |
should := require.New(t) |
| 67 |
|
| 68 |
testCases := []struct { |
| 69 |
input string |
| 70 |
expectedOutput string |
| 71 |
caseSensitive bool |
| 72 |
}{ |
| 73 |
{ |
| 74 |
input: `{"A":"foo","B":"bar"}`, |
| 75 |
expectedOutput: `{"a":"foo","b":"bar"}`, |
| 76 |
caseSensitive: false, |
| 77 |
}, |
| 78 |
{ |
| 79 |
input: `{"a":"foo","b":"bar"}`, |
| 80 |
expectedOutput: `{"a":"foo","b":"bar"}`, |
| 81 |
caseSensitive: true, |
| 82 |
}, |
| 83 |
{ |
| 84 |
input: `{"a":"foo","b":"bar","C":{"D":10}}`, |
| 85 |
expectedOutput: `{"a":"foo","b":"bar","C":{"D":10}}`, |
| 86 |
caseSensitive: true, |
| 87 |
}, |
| 88 |
{ |
| 89 |
input: `{"a":"foo","B":"bar","c":{"d":10}}`, |
| 90 |
expectedOutput: `{"a":"foo"}`, |
| 91 |
caseSensitive: true, |
| 92 |
}, |
| 93 |
{ |
| 94 |
input: `{"a":"foo","C":{"d":10}}`, |
| 95 |
expectedOutput: `{"a":"foo","C":{}}`, |
| 96 |
caseSensitive: true, |
| 97 |
}, |
| 98 |
{ |
| 99 |
input: `{"a":"foo","C":{"D":10,"e":{"f":"baz"}}}`, |
| 100 |
expectedOutput: `{"a":"foo","C":{"D":10,"e":{}}}`, |
| 101 |
caseSensitive: true, |
| 102 |
}, |
| 103 |
{ |
| 104 |
input: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`, |
| 105 |
expectedOutput: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`, |
| 106 |
caseSensitive: true, |
| 107 |
}, |
| 108 |
{ |
| 109 |
input: `{"A":"foo","c":{"d":10,"E":{"f":"baz"}}}`, |
| 110 |
expectedOutput: `{"a":"foo","C":{"D":10,"e":{"F":"baz"}}}`, |
| 111 |
caseSensitive: false, |
| 112 |
}, |
| 113 |
} |
| 114 |
|
| 115 |
for _, tc := range testCases { |
| 116 |
val := caseSensitiveStruct{} |
| 117 |
err := jsoniter.Config{CaseSensitive: tc.caseSensitive}.Froze().UnmarshalFromString(tc.input, &val) |
| 118 |
should.Nil(err) |
| 119 |
|
| 120 |
output, err := jsoniter.MarshalToString(val) |
| 121 |
should.Nil(err) |
| 122 |
should.Equal(tc.expectedOutput, output) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
type structWithElevenFields struct { |
| 127 |
A string `json:"A,omitempty"` |
| 128 |
B string `json:"B,omitempty"` |
| 129 |
C string `json:"C,omitempty"` |
| 130 |
D string `json:"d,omitempty"` |
| 131 |
E string `json:"e,omitempty"` |
| 132 |
F string `json:"f,omitempty"` |
| 133 |
G string `json:"g,omitempty"` |
| 134 |
H string `json:"h,omitempty"` |
| 135 |
I string `json:"i,omitempty"` |
| 136 |
J string `json:"j,omitempty"` |
| 137 |
K string `json:"k,omitempty"` |
| 138 |
} |
| 139 |
|
| 140 |
func Test_CaseSensitive_MoreThanTenFields(t *testing.T) { |
| 141 |
should := require.New(t) |
| 142 |
|
| 143 |
testCases := []struct { |
| 144 |
input string |
| 145 |
expectedOutput string |
| 146 |
caseSensitive bool |
| 147 |
}{ |
| 148 |
{ |
| 149 |
input: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`, |
| 150 |
expectedOutput: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6","g":"7","h":"8","i":"9","j":"10","k":"11"}`, |
| 151 |
caseSensitive: true, |
| 152 |
}, |
| 153 |
{ |
| 154 |
input: `{"a":"1","b":"2","c":"3","D":"4","E":"5","F":"6"}`, |
| 155 |
expectedOutput: `{"A":"1","B":"2","C":"3","d":"4","e":"5","f":"6"}`, |
| 156 |
caseSensitive: false, |
| 157 |
}, |
| 158 |
{ |
| 159 |
input: `{"A":"1","b":"2","d":"4","E":"5"}`, |
| 160 |
expectedOutput: `{"A":"1","d":"4"}`, |
| 161 |
caseSensitive: true, |
| 162 |
}, |
| 163 |
} |
| 164 |
|
| 165 |
for _, tc := range testCases { |
| 166 |
val := structWithElevenFields{} |
| 167 |
err := jsoniter.Config{CaseSensitive: tc.caseSensitive}.Froze().UnmarshalFromString(tc.input, &val) |
| 168 |
should.Nil(err) |
| 169 |
|
| 170 |
output, err := jsoniter.MarshalToString(val) |
| 171 |
should.Nil(err) |
| 172 |
should.Equal(tc.expectedOutput, output) |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
type onlyTaggedFieldStruct struct { |
| 177 |
A string `json:"a"` |
| 178 |
B string |
| 179 |
FSimpl F `json:"f_simpl"` |
| 180 |
ISimpl I |
| 181 |
FPtr *F `json:"f_ptr"` |
| 182 |
IPtr *I |
| 183 |
F |
| 184 |
*I |
| 185 |
} |
| 186 |
|
| 187 |
type F struct { |
| 188 |
G string `json:"g"` |
| 189 |
H string |
| 190 |
} |
| 191 |
|
| 192 |
type I struct { |
| 193 |
J string `json:"j"` |
| 194 |
K string |
| 195 |
} |
| 196 |
|
| 197 |
func Test_OnlyTaggedField(t *testing.T) { |
| 198 |
should := require.New(t) |
| 199 |
|
| 200 |
obj := onlyTaggedFieldStruct{ |
| 201 |
A: "a", |
| 202 |
B: "b", |
| 203 |
FSimpl: F{G: "g", H: "h"}, |
| 204 |
ISimpl: I{J: "j", K: "k"}, |
| 205 |
FPtr: &F{G: "g", H: "h"}, |
| 206 |
IPtr: &I{J: "j", K: "k"}, |
| 207 |
F: F{G: "g", H: "h"}, |
| 208 |
I: &I{J: "j", K: "k"}, |
| 209 |
} |
| 210 |
|
| 211 |
output, err := jsoniter.Config{OnlyTaggedField: true}.Froze().Marshal(obj) |
| 212 |
should.Nil(err) |
| 213 |
|
| 214 |
m := make(map[string]interface{}) |
| 215 |
err = jsoniter.Unmarshal(output, &m) |
| 216 |
should.Nil(err) |
| 217 |
|
| 218 |
should.Equal(map[string]interface{}{ |
| 219 |
"a": "a", |
| 220 |
"f_simpl": map[string]interface{}{ |
| 221 |
"g": "g", |
| 222 |
}, |
| 223 |
"f_ptr": map[string]interface{}{ |
| 224 |
"g": "g", |
| 225 |
}, |
| 226 |
"g": "g", |
| 227 |
"j": "j", |
| 228 |
}, m) |
| 229 |
} |