diff --git a/decoder/decoder_compat.go b/decoder/decoder_compat.go index 466d842e8..84bae4387 100644 --- a/decoder/decoder_compat.go +++ b/decoder/decoder_compat.go @@ -112,10 +112,10 @@ func (self *Decoder) CheckTrailings() error { func (self *Decoder) Decode(val interface{}) error { r := bytes.NewBufferString(self.s) dec := json.NewDecoder(r) - if (self.f | uint64(OptionUseNumber)) != 0 { + if (self.f & uint64(OptionUseNumber)) != 0 { dec.UseNumber() } - if (self.f | uint64(OptionDisableUnknown)) != 0 { + if (self.f & uint64(OptionDisableUnknown)) != 0 { dec.DisallowUnknownFields() } return dec.Decode(val) diff --git a/decoder/decoder_test.go b/decoder/decoder_test.go index 550147511..882aa7990 100644 --- a/decoder/decoder_test.go +++ b/decoder/decoder_test.go @@ -224,6 +224,36 @@ func TestDecodeCorrupt(t *testing.T) { } } +func TestDecodeOption(t *testing.T) { + var s string + var d *Decoder + var out interface{} + var out2 struct {} + var err error + + s = "123" + d = NewDecoder(s) + d.SetOptions(OptionUseNumber); + err = d.Decode(&out) + assert.NoError(t, err) + assert.Equal(t, out.(json.Number), json.Number("123")) + + d = NewDecoder(s) + err = d.Decode(&out) + assert.NoError(t, err) + assert.Equal(t, out.(float64), float64(123)) + + s = `{"un": 123}` + d = NewDecoder(s) + d.SetOptions(OptionDisableUnknown); + err = d.Decode(&out2) + assert.Error(t, err) + + d = NewDecoder(s) + err = d.Decode(&out2) + assert.NoError(t, err) +} + func decode(s string, v interface{}, copy bool) (int, error) { d := NewDecoder(s) if copy {