From dc2a606793340158c57ee4b014ab72428758485c Mon Sep 17 00:00:00 2001 From: Thomas Fix Date: Fri, 5 Jun 2020 18:07:52 -0400 Subject: [PATCH 1/3] Adding tests for fuzzy bool decoding --- extra/fuzzy_decoder_test.go | 58 ++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/extra/fuzzy_decoder_test.go b/extra/fuzzy_decoder_test.go index 69315adf..b6a3e5c7 100644 --- a/extra/fuzzy_decoder_test.go +++ b/extra/fuzzy_decoder_test.go @@ -3,7 +3,7 @@ package extra import ( "testing" - "github.com/json-iterator/go" + jsoniter "github.com/json-iterator/go" "github.com/stretchr/testify/require" ) @@ -325,6 +325,54 @@ func Test_any_to_float64(t *testing.T) { should.NotNil(jsoniter.UnmarshalFromString("[]", &val)) } +func Test_any_to_bool(t *testing.T) { + should := require.New(t) + var val bool + + // number to bool + should.Nil(jsoniter.UnmarshalFromString(`10`, &val)) + should.Equal(true, val) + should.Nil(jsoniter.UnmarshalFromString(`10.1`, &val)) + should.Equal(true, val) + should.Nil(jsoniter.UnmarshalFromString(`0`, &val)) + should.Equal(false, val) + should.Nil(jsoniter.UnmarshalFromString(`0.0`, &val)) + should.Equal(false, val) + should.Nil(jsoniter.UnmarshalFromString(`-10`, &val)) + should.Equal(true, val) + should.Nil(jsoniter.UnmarshalFromString(`-10.1`, &val)) + should.Equal(true, val) + + // string to bool + should.Nil(jsoniter.UnmarshalFromString(`""`, &val)) + should.Equal(false, val) + should.Nil(jsoniter.UnmarshalFromString(`"0"`, &val)) + should.Equal(false, val) + should.Nil(jsoniter.UnmarshalFromString(`"false"`, &val)) + should.Equal(true, val) + should.Nil(jsoniter.UnmarshalFromString(`"string"`, &val)) + should.Equal(true, val) + + // bool to bool + should.Nil(jsoniter.UnmarshalFromString(`false`, &val)) + should.Equal(false, val) + should.Nil(jsoniter.UnmarshalFromString(`true`, &val)) + should.Equal(true, val) + + // // TODO, according to other tests + // // object to bool + // should.Nil(jsoniter.UnmarshalFromString(`{}`, &val)) + // should.Equal(true, val) + // should.Nil(jsoniter.UnmarshalFromString(`{"key":"value"}`, &val)) + // should.Equal(true, val) + + // // array to bool + // should.Nil(jsoniter.UnmarshalFromString(`[]`, &val)) + // should.Equal(false, val) + // should.Nil(jsoniter.UnmarshalFromString(`["item"]`, &val)) + // should.Equal(true, val) +} + func Test_empty_array_as_map(t *testing.T) { should := require.New(t) var val map[string]interface{} @@ -391,3 +439,11 @@ func Test_null_to_float64(t *testing.T) { err := jsoniter.Unmarshal(body, &message) should.NoError(err) } + +func Test_null_to_bool(t *testing.T) { + should := require.New(t) + body := []byte(`null`) + var message bool + err := jsoniter.Unmarshal(body, &message) + should.NoError(err) +} From dd19387ea82f38adce8d5f674e79842b1ca8055e Mon Sep 17 00:00:00 2001 From: Thomas Fix Date: Fri, 5 Jun 2020 23:09:50 -0400 Subject: [PATCH 2/3] Adding tests for fuzzy decoding array/object into bool --- extra/fuzzy_decoder_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/extra/fuzzy_decoder_test.go b/extra/fuzzy_decoder_test.go index b6a3e5c7..194f53a3 100644 --- a/extra/fuzzy_decoder_test.go +++ b/extra/fuzzy_decoder_test.go @@ -359,18 +359,21 @@ func Test_any_to_bool(t *testing.T) { should.Nil(jsoniter.UnmarshalFromString(`true`, &val)) should.Equal(true, val) - // // TODO, according to other tests - // // object to bool + // object to bool + // TODO, according to other decoders' tests // should.Nil(jsoniter.UnmarshalFromString(`{}`, &val)) // should.Equal(true, val) // should.Nil(jsoniter.UnmarshalFromString(`{"key":"value"}`, &val)) // should.Equal(true, val) + should.NotNil(jsoniter.UnmarshalFromString(`{}`, &val)) - // // array to bool + // array to bool + // TODO, according to other decoders' tests // should.Nil(jsoniter.UnmarshalFromString(`[]`, &val)) // should.Equal(false, val) // should.Nil(jsoniter.UnmarshalFromString(`["item"]`, &val)) // should.Equal(true, val) + should.NotNil(jsoniter.UnmarshalFromString(`[]`, &val)) } func Test_empty_array_as_map(t *testing.T) { From 32c48e48984c3e720921fad473a42e5b1325c57e Mon Sep 17 00:00:00 2001 From: Thomas Fix Date: Sat, 6 Jun 2020 00:27:26 -0400 Subject: [PATCH 3/3] Adding support for fuzzy decoding into bool --- extra/fuzzy_decoder.go | 43 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/extra/fuzzy_decoder.go b/extra/fuzzy_decoder.go index 52546b11..3413d05a 100644 --- a/extra/fuzzy_decoder.go +++ b/extra/fuzzy_decoder.go @@ -8,7 +8,7 @@ import ( "strings" "unsafe" - "github.com/json-iterator/go" + jsoniter "github.com/json-iterator/go" "github.com/modern-go/reflect2" ) @@ -143,6 +143,7 @@ func RegisterFuzzyDecoders() { *((*uint64)(ptr)) = iter.ReadUint64() } }}) + jsoniter.RegisterTypeDecoder("bool", &fuzzyBoolDecoder{}) } type tolerateEmptyArrayExtension struct { @@ -292,3 +293,43 @@ func (decoder *fuzzyFloat64Decoder) Decode(ptr unsafe.Pointer, iter *jsoniter.It iter.ReportError("fuzzyFloat64Decoder", "not number or string") } } + +type fuzzyBoolDecoder struct { +} + +func (decoder *fuzzyBoolDecoder) Decode(ptr unsafe.Pointer, iter *jsoniter.Iterator) { + valueType := iter.WhatIsNext() + switch valueType { + case jsoniter.NumberValue: + *((*bool)(ptr)) = iter.ReadFloat64() != 0 + case jsoniter.StringValue: + str := iter.ReadString() + switch str { + case "", "0": + *((*bool)(ptr)) = false + default: + *((*bool)(ptr)) = true + } + case jsoniter.BoolValue: + *((*bool)(ptr)) = iter.ReadBool() + // In order to stay consistent with the other decoders here, leaving arrays and objects out for now. + // case jsoniter.ObjectValue: + // iter.Skip() + // *((*bool)(ptr)) = true + // case jsoniter.ArrayValue: + // var nonEmptyArray bool + // iter.ReadArrayCB( + // func(*jsoniter.Iterator) bool { + // iter.Skip() + // nonEmptyArray = true + // return true + // }, + // ) + // *((*bool)(ptr)) = nonEmptyArray + case jsoniter.NilValue: + iter.Skip() + *((*bool)(ptr)) = false + default: + iter.ReportError("fuzzyBoolDecoder", "not number, string or bool") + } +}