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
|
templateInterface.word = ptr
|
||||||
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
|
||||||
unmarshaler := (*realInterface).(json.Unmarshaler)
|
unmarshaler := (*realInterface).(json.Unmarshaler)
|
||||||
|
iter.nextToken()
|
||||||
|
iter.unreadByte() // skip spaces
|
||||||
bytes := iter.SkipAndReturnBytes()
|
bytes := iter.SkipAndReturnBytes()
|
||||||
err := unmarshaler.UnmarshalJSON(bytes)
|
err := unmarshaler.UnmarshalJSON(bytes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -180,26 +180,22 @@ func Test_marshaler_and_encoder(t *testing.T) {
|
|||||||
|
|
||||||
type ObjectImplementedUnmarshaler int
|
type ObjectImplementedUnmarshaler int
|
||||||
|
|
||||||
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON([]byte) error {
|
func (obj *ObjectImplementedUnmarshaler) UnmarshalJSON(s []byte) error {
|
||||||
*obj = 100
|
val, _ := strconv.ParseInt(string(s[1:len(s)-1]), 10, 64)
|
||||||
|
*obj = ObjectImplementedUnmarshaler(val)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_unmarshaler(t *testing.T) {
|
func Test_unmarshaler(t *testing.T) {
|
||||||
type TestObject struct {
|
|
||||||
Field *ObjectImplementedUnmarshaler
|
|
||||||
Field2 string
|
|
||||||
}
|
|
||||||
should := require.New(t)
|
should := require.New(t)
|
||||||
obj := TestObject{}
|
var obj ObjectImplementedUnmarshaler
|
||||||
val := ObjectImplementedUnmarshaler(0)
|
err := json.Unmarshal([]byte(` "100" `), &obj)
|
||||||
obj.Field = &val
|
|
||||||
err := json.Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(100, int(*obj.Field))
|
should.Equal(100, int(obj))
|
||||||
err = Unmarshal([]byte(`{"Field":"hello"}`), &obj)
|
iter := ParseString(ConfigDefault, ` "100" `)
|
||||||
|
iter.ReadVal(&obj)
|
||||||
should.Nil(err)
|
should.Nil(err)
|
||||||
should.Equal(100, int(*obj.Field))
|
should.Equal(100, int(obj))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_unmarshaler_and_decoder(t *testing.T) {
|
func Test_unmarshaler_and_decoder(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user