mirror of
https://github.com/json-iterator/go.git
synced 2025-02-19 19:59:49 +02:00
#90 trim spaces when UnmarshalJSON
This commit is contained in:
parent
04eae11ba5
commit
fa0965a968
@ -618,6 +618,8 @@ func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
||||
templateInterface.word = ptr
|
||||
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||
unmarshaler := (*realInterface).(json.Unmarshaler)
|
||||
iter.nextToken()
|
||||
iter.unreadByte() // skip spaces
|
||||
bytes := iter.SkipAndReturnBytes()
|
||||
err := unmarshaler.UnmarshalJSON(bytes)
|
||||
if err != nil {
|
||||
|
@ -180,26 +180,22 @@ func Test_marshaler_and_encoder(t *testing.T) {
|
||||
|
||||
type ObjectImplementedUnmarshaler int
|
||||
|
||||
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON([]byte) error {
|
||||
*obj = 100
|
||||
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON(s []byte) error {
|
||||
val, _ := strconv.ParseInt(string(s[1:len(s)-1]), 10, 64)
|
||||
*obj = ObjectImplementedUnmarshaler(val)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Test_unmarshaler(t *testing.T) {
|
||||
type TestObject struct {
|
||||
Field *ObjectImplementedUnmarshaler
|
||||
Field2 string
|
||||
}
|
||||
should := require.New(t)
|
||||
obj := TestObject{}
|
||||
val := ObjectImplementedUnmarshaler(0)
|
||||
obj.Field = &val
|
||||
err := json.Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
||||
var obj ObjectImplementedUnmarshaler
|
||||
err := json.Unmarshal([]byte(` "100" `), &obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(*obj.Field))
|
||||
err = Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
||||
should.Equal(100, int(obj))
|
||||
iter := ParseString(ConfigDefault, ` "100" `)
|
||||
iter.ReadVal(&obj)
|
||||
should.Nil(err)
|
||||
should.Equal(100, int(*obj.Field))
|
||||
should.Equal(100, int(obj))
|
||||
}
|
||||
|
||||
func Test_unmarshaler_and_decoder(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user