From a743df1b8a3ad8c48a9423cb099f9386b518f146 Mon Sep 17 00:00:00 2001 From: Xargin Date: Mon, 3 Jul 2017 19:10:49 +0800 Subject: [PATCH 01/25] add bool convert test map --- jsoniter_any_bool_test.go | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/jsoniter_any_bool_test.go b/jsoniter_any_bool_test.go index 7344f4d..85d332b 100644 --- a/jsoniter_any_bool_test.go +++ b/jsoniter_any_bool_test.go @@ -1,12 +1,44 @@ package jsoniter import ( - "github.com/json-iterator/go/require" + "fmt" "testing" + + "github.com/json-iterator/go/require" ) +var boolConvertMap = map[string]bool{ + "true": true, + "false": false, + + `"true"`: true, + `"false"`: true, + + "123": true, + "0": false, + `"0"`: false, + "-1": true, + + "1.1": true, + "0.0": false, + "-1.1": true, + `""`: false, + "[1,2]": true, + "[]": false, + "{}": true, + `{"abc":1}`: true, +} + func Test_read_bool_as_any(t *testing.T) { should := require.New(t) - any := Get([]byte("true")) - should.True(any.ToBool()) + + var any Any + for k, v := range boolConvertMap { + any = Get([]byte(k)) + if v { + should.True(any.ToBool(), fmt.Sprintf("origin val is %v", k)) + } else { + should.False(any.ToBool(), fmt.Sprintf("origin val is %v", k)) + } + } } From 919a2eff5c5fc6173ef0e26aa7be8a91f9dddb75 Mon Sep 17 00:00:00 2001 From: Xargin Date: Mon, 3 Jul 2017 19:40:12 +0800 Subject: [PATCH 02/25] fix bool test --- feature_any_object.go | 4 +--- feature_any_string.go | 2 +- jsoniter_any_string_test.go | 5 +++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/feature_any_object.go b/feature_any_object.go index 7382233..dd2bf41 100644 --- a/feature_any_object.go +++ b/feature_any_object.go @@ -25,9 +25,7 @@ func (any *objectLazyAny) LastError() error { } func (any *objectLazyAny) ToBool() bool { - iter := any.cfg.BorrowIterator(any.buf) - defer any.cfg.ReturnIterator(iter) - return iter.ReadObject() != "" + return true } func (any *objectLazyAny) ToInt() int { diff --git a/feature_any_string.go b/feature_any_string.go index e3dcd6a..e76c275 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -35,7 +35,7 @@ func (any *stringAny) LastError() error { func (any *stringAny) ToBool() bool { str := any.ToString() - if str == "false" { + if str == "0" { return false } for _, c := range str { diff --git a/jsoniter_any_string_test.go b/jsoniter_any_string_test.go index f17a111..1567bdc 100644 --- a/jsoniter_any_string_test.go +++ b/jsoniter_any_string_test.go @@ -1,8 +1,9 @@ package jsoniter import ( - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_read_string_as_any(t *testing.T) { @@ -13,7 +14,7 @@ func Test_read_string_as_any(t *testing.T) { any = Get([]byte(`" "`)) should.False(any.ToBool()) any = Get([]byte(`"false"`)) - should.False(any.ToBool()) + should.True(any.ToBool()) any = Get([]byte(`"123"`)) should.Equal(123, any.ToInt()) } From 2e10d5fdad446637af3d1b0b0b84fb76396eff45 Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 00:19:41 +0800 Subject: [PATCH 03/25] add basic int test --- feature_any_float.go | 5 ++- feature_any_string.go | 59 ++++++++++++++++++++++----- jsoniter_any_int_test.go | 88 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 139 insertions(+), 13 deletions(-) diff --git a/feature_any_float.go b/feature_any_float.go index ad037b8..47350f8 100644 --- a/feature_any_float.go +++ b/feature_any_float.go @@ -42,7 +42,10 @@ func (any *floatAny) ToInt64() int64 { } func (any *floatAny) ToUint() uint { - return uint(any.val) + if any.val > 0 { + return uint(any.val) + } + return uint(-any.val) } func (any *floatAny) ToUint32() uint32 { diff --git a/feature_any_string.go b/feature_any_string.go index e76c275..f91846a 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -49,32 +49,69 @@ func (any *stringAny) ToBool() bool { } func (any *stringAny) ToInt() int { - parsed, _ := strconv.ParseInt(any.val, 10, 64) - return int(parsed) + return int(any.ToInt64()) + } func (any *stringAny) ToInt32() int32 { - parsed, _ := strconv.ParseInt(any.val, 10, 32) - return int32(parsed) + return int32(any.ToInt64()) } func (any *stringAny) ToInt64() int64 { - parsed, _ := strconv.ParseInt(any.val, 10, 64) - return parsed + if any.val == "" { + return 0 + } + + flag := 1 + startPos := 0 + endPos := 0 + if any.val[0] == '+' || any.val[0] == '-' { + startPos = 1 + } + + if any.val[0] == '-' { + flag = -1 + } + + for i := startPos; i < len(any.val); i++ { + if any.val[i] >= '0' && any.val[i] <= '9' { + endPos = i + 1 + } else { + break + } + } + parsed, _ := strconv.ParseInt(any.val[startPos:endPos], 10, 64) + return int64(flag) * parsed } func (any *stringAny) ToUint() uint { - parsed, _ := strconv.ParseUint(any.val, 10, 64) - return uint(parsed) + return uint(any.ToUint64()) } func (any *stringAny) ToUint32() uint32 { - parsed, _ := strconv.ParseUint(any.val, 10, 32) - return uint32(parsed) + return uint32(any.ToUint64()) } func (any *stringAny) ToUint64() uint64 { - parsed, _ := strconv.ParseUint(any.val, 10, 64) + if any.val == "" { + return 0 + } + + startPos := 0 + endPos := 0 + // uint skip flag, is this correct? + if any.val[0] == '+' || any.val[0] == '-' { + startPos = 1 + } + + for i := startPos; i < len(any.val); i++ { + if any.val[i] >= '0' && any.val[i] <= '9' { + endPos = i + 1 + } else { + break + } + } + parsed, _ := strconv.ParseUint(any.val[startPos:endPos], 10, 64) return parsed } diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index b19f4f9..dbf283b 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -1,11 +1,97 @@ package jsoniter import ( - "github.com/json-iterator/go/require" + "fmt" "io" "testing" + + "github.com/json-iterator/go/require" ) +var intConvertMap = map[string]int{ + "321.1": 321, + "-321.1": -321, + `"1.1"`: 1, + `"-1.1"`: -1, + "0.0": 0, + "0": 0, + `"0"`: 0, + `"0.0"`: 0, + "-1.1": -1, + "true": 1, + "false": 0, + `"true"`: 0, + `"false"`: 0, + `"true123"`: 0, + `"123true"`: 123, + `"1.2332e6"`: 1, + `""`: 0, +} + +func Test_read_any_to_int(t *testing.T) { + should := require.New(t) + + // int + for k, v := range intConvertMap { + any := Get([]byte(k)) + should.Equal(v, any.ToInt(), fmt.Sprintf("origin val %v", k)) + } + + // int32 + for k, v := range intConvertMap { + any := Get([]byte(k)) + should.Equal(int32(v), any.ToInt32(), fmt.Sprintf("original val is %v", k)) + } + + // int64 + for k, v := range intConvertMap { + any := Get([]byte(k)) + should.Equal(int64(v), any.ToInt64(), fmt.Sprintf("original val is %v", k)) + } + +} + +var uintConvertMap = map[string]int{ + "321.1": 321, + `"1.1"`: 1, + `"-1.1"`: 1, + "0.0": 0, + "0": 0, + `"0"`: 0, + `"0.0"`: 0, + "true": 1, + "false": 0, + `"true"`: 0, + `"false"`: 0, + `"true123"`: 0, + `"123true"`: 123, + `"1.2332e6"`: 1, + `""`: 0, + // TODO need to solve + //"-1.1": 1, + //"-321.1": 321, +} + +func Test_read_any_to_uint(t *testing.T) { + should := require.New(t) + + for k, v := range uintConvertMap { + any := Get([]byte(k)) + should.Equal(uint64(v), any.ToUint64(), fmt.Sprintf("origin val %v", k)) + } + + for k, v := range uintConvertMap { + any := Get([]byte(k)) + should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k)) + } + + for k, v := range uintConvertMap { + any := Get([]byte(k)) + should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k)) + } + +} + func Test_read_int64_as_any(t *testing.T) { should := require.New(t) any := Get([]byte("1234")) From d8dbf14af45e168f777b4c0da8a1f163fcceb78a Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 00:29:11 +0800 Subject: [PATCH 04/25] add array to int --- jsoniter_any_int_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index dbf283b..5c35c27 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -26,6 +26,10 @@ var intConvertMap = map[string]int{ `"123true"`: 123, `"1.2332e6"`: 1, `""`: 0, + "+": 0, + "-": 0, + "[]": 0, + "[1,2]": 1, } func Test_read_any_to_int(t *testing.T) { @@ -67,6 +71,10 @@ var uintConvertMap = map[string]int{ `"123true"`: 123, `"1.2332e6"`: 1, `""`: 0, + "+": 0, + "-": 0, + "[]": 0, + "[1,2]": 1, // TODO need to solve //"-1.1": 1, //"-321.1": 321, From ac8dd56dfb8ca0bc172a39859f38e49aa37550e8 Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 00:53:10 +0800 Subject: [PATCH 05/25] object cannot covert to int/float in php, so change covert result to zero --- feature_any_object.go | 48 +++++++------------------------------ jsoniter_any_int_test.go | 3 +++ jsoniter_any_object_test.go | 5 ++-- 3 files changed, 14 insertions(+), 42 deletions(-) diff --git a/feature_any_object.go b/feature_any_object.go index dd2bf41..6e51295 100644 --- a/feature_any_object.go +++ b/feature_any_object.go @@ -29,67 +29,35 @@ func (any *objectLazyAny) ToBool() bool { } func (any *objectLazyAny) ToInt() int { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToInt32() int32 { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToInt64() int64 { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToUint() uint { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToUint32() uint32 { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToUint64() uint64 { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToFloat32() float32 { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToFloat64() float64 { - if any.ToBool() { - return 1 - } else { - return 0 - } + return 0 } func (any *objectLazyAny) ToString() string { diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 5c35c27..647b01b 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -30,6 +30,8 @@ var intConvertMap = map[string]int{ "-": 0, "[]": 0, "[1,2]": 1, + // object in php cannot convert to int + "{}": 0, } func Test_read_any_to_int(t *testing.T) { @@ -75,6 +77,7 @@ var uintConvertMap = map[string]int{ "-": 0, "[]": 0, "[1,2]": 1, + "{}": 0, // TODO need to solve //"-1.1": 1, //"-321.1": 321, diff --git a/jsoniter_any_object_test.go b/jsoniter_any_object_test.go index 4c85f4d..fbd3bb4 100644 --- a/jsoniter_any_object_test.go +++ b/jsoniter_any_object_test.go @@ -1,8 +1,9 @@ package jsoniter import ( - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_read_object_as_any(t *testing.T) { @@ -18,7 +19,7 @@ func Test_read_object_as_any(t *testing.T) { should.Equal(2, len(any.Keys())) should.Equal(2, any.Size()) should.True(any.ToBool()) - should.Equal(1, any.ToInt()) + should.Equal(0, any.ToInt()) should.Equal(Object, any.ValueType()) should.Nil(any.LastError()) should.Equal("b", any.GetObject()["a"].ToString()) From 712ddb1942d24e0b1421bb746286bd0a511ca136 Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 14:00:24 +0800 Subject: [PATCH 06/25] fix negative number to uint --- feature_any_number.go | 4 +--- feature_iter_int.go | 9 +++++++++ jsoniter_any_int_test.go | 15 +++++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/feature_any_number.go b/feature_any_number.go index 1dc0244..d909044 100644 --- a/feature_any_number.go +++ b/feature_any_number.go @@ -1,8 +1,6 @@ package jsoniter -import ( - "unsafe" -) +import "unsafe" type numberLazyAny struct { baseAny diff --git a/feature_iter_int.go b/feature_iter_int.go index 139f6f8..ee5263e 100644 --- a/feature_iter_int.go +++ b/feature_iter_int.go @@ -21,6 +21,9 @@ func init() { } func (iter *Iterator) ReadUint() uint { + if iter.buf[iter.head] == '-' && len(iter.buf) > 1 { + iter.buf = iter.buf[1:] + } return uint(iter.ReadUint64()) } @@ -104,6 +107,9 @@ func (iter *Iterator) ReadInt32() (ret int32) { } func (iter *Iterator) ReadUint32() (ret uint32) { + if iter.buf[iter.head] == '-' && len(iter.buf) > 1 { + iter.buf = iter.buf[1:] + } return iter.readUint32(iter.nextToken()) } @@ -215,6 +221,9 @@ func (iter *Iterator) ReadInt64() (ret int64) { } func (iter *Iterator) ReadUint64() uint64 { + if iter.buf[iter.head] == '-' && len(iter.buf) > 1 { + iter.buf = iter.buf[1:] + } return iter.readUint64(iter.nextToken()) } diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 647b01b..03992c5 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -12,7 +12,7 @@ var intConvertMap = map[string]int{ "321.1": 321, "-321.1": -321, `"1.1"`: 1, - `"-1.1"`: -1, + `"-321.1"`: -321, "0.0": 0, "0": 0, `"0"`: 0, @@ -58,13 +58,15 @@ func Test_read_any_to_int(t *testing.T) { } var uintConvertMap = map[string]int{ + "321.1": 321, `"1.1"`: 1, - `"-1.1"`: 1, + `"-123.1"`: 123, "0.0": 0, "0": 0, `"0"`: 0, `"0.0"`: 0, + `"00.0"`: 0, "true": 1, "false": 0, `"true"`: 0, @@ -75,12 +77,13 @@ var uintConvertMap = map[string]int{ `""`: 0, "+": 0, "-": 0, + ".": 0, "[]": 0, "[1,2]": 1, "{}": 0, - // TODO need to solve - //"-1.1": 1, - //"-321.1": 321, + "{1,2}": 0, + "-1.1": 1, + "-321.1": 321, } func Test_read_any_to_uint(t *testing.T) { @@ -98,7 +101,7 @@ func Test_read_any_to_uint(t *testing.T) { for k, v := range uintConvertMap { any := Get([]byte(k)) - should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k)) + should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k)) } } From d7b6b4e0bbbd7c72c29ff47b53be550fc3719aa7 Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 15:29:47 +0800 Subject: [PATCH 07/25] add convert table document --- fuzzy_mode_convert_table.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 fuzzy_mode_convert_table.md diff --git a/fuzzy_mode_convert_table.md b/fuzzy_mode_convert_table.md new file mode 100644 index 0000000..e4560ae --- /dev/null +++ b/fuzzy_mode_convert_table.md @@ -0,0 +1,8 @@ +| json type \ dest type | bool | int | uint | string | +| --- | --- | --- | --- || +| number | --- | --- | --- || +| string | --- | --- | --- || +| bool | --- | --- | --- || +| bool | --- | --- | --- || +| object | --- | --- | --- || +| array | --- | --- | --- || \ No newline at end of file From e5d7a65616f7086ae19ae203cce7526b2ea024ce Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 15:41:28 +0800 Subject: [PATCH 08/25] add convert table --- fuzzy_mode_convert_table.md | 13 ++++++------- jsoniter_any_bool_test.go | 10 ++++++---- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/fuzzy_mode_convert_table.md b/fuzzy_mode_convert_table.md index e4560ae..1a3e78a 100644 --- a/fuzzy_mode_convert_table.md +++ b/fuzzy_mode_convert_table.md @@ -1,8 +1,7 @@ | json type \ dest type | bool | int | uint | string | -| --- | --- | --- | --- || -| number | --- | --- | --- || -| string | --- | --- | --- || -| bool | --- | --- | --- || -| bool | --- | --- | --- || -| object | --- | --- | --- || -| array | --- | --- | --- || \ No newline at end of file +| --- | --- | --- | --- |--| +| number | positive => true
negative => true
zero => false| 23.2 => 23
-32.1 => -32| 12.1 => 12
-12.1 => 12|| +| string | empty string => false
string "0" => false
other strings => true | "123.32" => 123
"-123.4" => -123
"123.23xxxw" => 123
"abcde12" => 0
"-32.1" => -32| 13.2 => 13
-1.1 => 1 || +| bool | true => true
false => false| true => 1
false => 0 | true => 1
false => 0 || +| object | true | 0 | 0 || +| array | empty array => false
nonempty array => true| [] => 0
[1,2] => 1 | [] => 0
[1,2] => 1 || \ No newline at end of file diff --git a/jsoniter_any_bool_test.go b/jsoniter_any_bool_test.go index 85d332b..0ef836f 100644 --- a/jsoniter_any_bool_test.go +++ b/jsoniter_any_bool_test.go @@ -14,10 +14,12 @@ var boolConvertMap = map[string]bool{ `"true"`: true, `"false"`: true, - "123": true, - "0": false, - `"0"`: false, - "-1": true, + "123": true, + `"123"`: true, + "0": false, + `"0"`: false, + "-1": true, + `"-1"`: true, "1.1": true, "0.0": false, From 50beb4f15d4ba5197d942a54c244dd76dac9adf7 Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 17:05:39 +0800 Subject: [PATCH 09/25] update fuzzy convert table --- fuzzy_mode_convert_table.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fuzzy_mode_convert_table.md b/fuzzy_mode_convert_table.md index 1a3e78a..25c7196 100644 --- a/fuzzy_mode_convert_table.md +++ b/fuzzy_mode_convert_table.md @@ -1,7 +1,7 @@ | json type \ dest type | bool | int | uint | string | | --- | --- | --- | --- |--| -| number | positive => true
negative => true
zero => false| 23.2 => 23
-32.1 => -32| 12.1 => 12
-12.1 => 12|| -| string | empty string => false
string "0" => false
other strings => true | "123.32" => 123
"-123.4" => -123
"123.23xxxw" => 123
"abcde12" => 0
"-32.1" => -32| 13.2 => 13
-1.1 => 1 || +| number | positive => true
negative => true
zero => false| 23.2 => 23
-32.1 => -32| 12.1 => 12
-12.1 => 0|| +| string | empty string => false
string "0" => false
other strings => true | "123.32" => 123
"-123.4" => -123
"123.23xxxw" => 123
"abcde12" => 0
"-32.1" => -32| 13.2 => 13
-1.1 => 0 || | bool | true => true
false => false| true => 1
false => 0 | true => 1
false => 0 || | object | true | 0 | 0 || | array | empty array => false
nonempty array => true| [] => 0
[1,2] => 1 | [] => 0
[1,2] => 1 || \ No newline at end of file From 4ea96ac7c3f8b648e864095b422191e1e7962982 Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 17:02:46 +0800 Subject: [PATCH 10/25] change all negative convert to uint 0 --- feature_any_string.go | 7 +++++-- feature_iter_int.go | 9 --------- jsoniter_any_bool_test.go | 1 + jsoniter_any_int_test.go | 10 ++++++---- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/feature_any_string.go b/feature_any_string.go index f91846a..c58dda2 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -99,8 +99,11 @@ func (any *stringAny) ToUint64() uint64 { startPos := 0 endPos := 0 - // uint skip flag, is this correct? - if any.val[0] == '+' || any.val[0] == '-' { + + if any.val[0] == '-' { + return 0 + } + if any.val[0] == '+' { startPos = 1 } diff --git a/feature_iter_int.go b/feature_iter_int.go index ee5263e..139f6f8 100644 --- a/feature_iter_int.go +++ b/feature_iter_int.go @@ -21,9 +21,6 @@ func init() { } func (iter *Iterator) ReadUint() uint { - if iter.buf[iter.head] == '-' && len(iter.buf) > 1 { - iter.buf = iter.buf[1:] - } return uint(iter.ReadUint64()) } @@ -107,9 +104,6 @@ func (iter *Iterator) ReadInt32() (ret int32) { } func (iter *Iterator) ReadUint32() (ret uint32) { - if iter.buf[iter.head] == '-' && len(iter.buf) > 1 { - iter.buf = iter.buf[1:] - } return iter.readUint32(iter.nextToken()) } @@ -221,9 +215,6 @@ func (iter *Iterator) ReadInt64() (ret int64) { } func (iter *Iterator) ReadUint64() uint64 { - if iter.buf[iter.head] == '-' && len(iter.buf) > 1 { - iter.buf = iter.buf[1:] - } return iter.readUint64(iter.nextToken()) } diff --git a/jsoniter_any_bool_test.go b/jsoniter_any_bool_test.go index 85d332b..5be326f 100644 --- a/jsoniter_any_bool_test.go +++ b/jsoniter_any_bool_test.go @@ -8,6 +8,7 @@ import ( ) var boolConvertMap = map[string]bool{ + "null": false, "true": true, "false": false, diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 03992c5..4341cb1 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -9,6 +9,7 @@ import ( ) var intConvertMap = map[string]int{ + "null": 0, "321.1": 321, "-321.1": -321, `"1.1"`: 1, @@ -58,10 +59,10 @@ func Test_read_any_to_int(t *testing.T) { } var uintConvertMap = map[string]int{ - + "null": 0, "321.1": 321, `"1.1"`: 1, - `"-123.1"`: 123, + `"-123.1"`: 0, "0.0": 0, "0": 0, `"0"`: 0, @@ -73,6 +74,7 @@ var uintConvertMap = map[string]int{ `"false"`: 0, `"true123"`: 0, `"123true"`: 123, + `"-123true"`: 0, `"1.2332e6"`: 1, `""`: 0, "+": 0, @@ -82,8 +84,8 @@ var uintConvertMap = map[string]int{ "[1,2]": 1, "{}": 0, "{1,2}": 0, - "-1.1": 1, - "-321.1": 321, + "-1.1": 0, + "-321.1": 0, } func Test_read_any_to_uint(t *testing.T) { From f245011c7d222ad15c1ea4c021e0c59b34095bfc Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 19:59:34 +0800 Subject: [PATCH 11/25] add any to float --- feature_any_string.go | 46 ++++++++++++++++++++++++++++++++++---- jsoniter_any_float_test.go | 43 ++++++++++++++++++++++++++++++++++- jsoniter_any_int_test.go | 3 +++ 3 files changed, 87 insertions(+), 5 deletions(-) diff --git a/feature_any_string.go b/feature_any_string.go index c58dda2..13c9ad0 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -119,13 +119,51 @@ func (any *stringAny) ToUint64() uint64 { } func (any *stringAny) ToFloat32() float32 { - parsed, _ := strconv.ParseFloat(any.val, 32) - return float32(parsed) + return float32(any.ToFloat64()) } func (any *stringAny) ToFloat64() float64 { - parsed, _ := strconv.ParseFloat(any.val, 64) - return parsed + if any.val == "" { + return 0 + } + + startPos := 0 + endPos := 0 + flag := 1 + + if any.val[0] == '+' || any.val[0] == '-' { + startPos = 1 + } + + if any.val[0] == '-' { + flag = -1 + } + + pointOccurCnt := 0 + pointPos := -1 + + for i := startPos; i < len(any.val); i++ { + if any.val[i] >= '0' && any.val[i] <= '9' { + endPos = i + 1 + } else if any.val[i] == '.' && pointOccurCnt == 0 { + endPos = i + 1 + pointOccurCnt++ + pointPos = i + } else { + break + } + } + + if pointPos == endPos-1 { + endPos-- + } + + if endPos <= startPos { + return 0 + } + + parsed, _ := strconv.ParseFloat(any.val[startPos:endPos], 64) + return float64(flag) * parsed } func (any *stringAny) ToString() string { diff --git a/jsoniter_any_float_test.go b/jsoniter_any_float_test.go index 6606956..4e892e6 100644 --- a/jsoniter_any_float_test.go +++ b/jsoniter_any_float_test.go @@ -1,10 +1,51 @@ package jsoniter import ( - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) +var floatConvertMap = map[string]float64{ + "null": 0, + "true": 1, + "false": 0, + + `"true"`: 0, + `"false"`: 0, + + "123": 123, + `"123true"`: 123, + + `"-123true"`: -123, + "0": 0, + `"0"`: 0, + "-1": -1, + + "1.1": 1.1, + "0.0": 0, + "-1.1": -1.1, + `"+1.1"`: 1.1, + `""`: 0, + "[1,2]": 1, + "[]": 0, + "{}": 0, + `{"abc":1}`: 0, +} + +func Test_read_any_to_float(t *testing.T) { + should := require.New(t) + for k, v := range floatConvertMap { + any := Get([]byte(k)) + should.Equal(float64(v), any.ToFloat64(), "the original val is "+k) + } + + for k, v := range floatConvertMap { + any := Get([]byte(k)) + should.Equal(float32(v), any.ToFloat32(), "the original val is "+k) + } +} + func Test_read_float_as_any(t *testing.T) { should := require.New(t) any := Get([]byte("12.3")) diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 4341cb1..3a208c7 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -25,12 +25,14 @@ var intConvertMap = map[string]int{ `"false"`: 0, `"true123"`: 0, `"123true"`: 123, + `"-123true"`: -123, `"1.2332e6"`: 1, `""`: 0, "+": 0, "-": 0, "[]": 0, "[1,2]": 1, + `["1","2"]`: 1, // object in php cannot convert to int "{}": 0, } @@ -73,6 +75,7 @@ var uintConvertMap = map[string]int{ `"true"`: 0, `"false"`: 0, `"true123"`: 0, + `"+1"`: 1, `"123true"`: 123, `"-123true"`: 0, `"1.2332e6"`: 1, From 3f35bed8841edce1a132e135a7afc0d5c77d8e40 Mon Sep 17 00:00:00 2001 From: Xargin Date: Tue, 4 Jul 2017 22:28:24 +0800 Subject: [PATCH 12/25] simplify float convert --- feature_any_string.go | 46 ++++---------------------------------- jsoniter_any_float_test.go | 4 ++-- 2 files changed, 6 insertions(+), 44 deletions(-) diff --git a/feature_any_string.go b/feature_any_string.go index 13c9ad0..c58dda2 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -119,51 +119,13 @@ func (any *stringAny) ToUint64() uint64 { } func (any *stringAny) ToFloat32() float32 { - return float32(any.ToFloat64()) + parsed, _ := strconv.ParseFloat(any.val, 32) + return float32(parsed) } func (any *stringAny) ToFloat64() float64 { - if any.val == "" { - return 0 - } - - startPos := 0 - endPos := 0 - flag := 1 - - if any.val[0] == '+' || any.val[0] == '-' { - startPos = 1 - } - - if any.val[0] == '-' { - flag = -1 - } - - pointOccurCnt := 0 - pointPos := -1 - - for i := startPos; i < len(any.val); i++ { - if any.val[i] >= '0' && any.val[i] <= '9' { - endPos = i + 1 - } else if any.val[i] == '.' && pointOccurCnt == 0 { - endPos = i + 1 - pointOccurCnt++ - pointPos = i - } else { - break - } - } - - if pointPos == endPos-1 { - endPos-- - } - - if endPos <= startPos { - return 0 - } - - parsed, _ := strconv.ParseFloat(any.val[startPos:endPos], 64) - return float64(flag) * parsed + parsed, _ := strconv.ParseFloat(any.val, 64) + return parsed } func (any *stringAny) ToString() string { diff --git a/jsoniter_any_float_test.go b/jsoniter_any_float_test.go index 4e892e6..d87c076 100644 --- a/jsoniter_any_float_test.go +++ b/jsoniter_any_float_test.go @@ -15,9 +15,9 @@ var floatConvertMap = map[string]float64{ `"false"`: 0, "123": 123, - `"123true"`: 123, + `"123true"`: 0, - `"-123true"`: -123, + `"-123true"`: 0, "0": 0, `"0"`: 0, "-1": -1, From 4e65952c0944684af353bb1c647a3c3c9ffe7bde Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 00:23:00 +0800 Subject: [PATCH 13/25] fix float convert --- feature_any_string.go | 31 ++++++++++++++++++++++++++++--- jsoniter_any_float_test.go | 13 ++++++++----- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/feature_any_string.go b/feature_any_string.go index c58dda2..7b8c39a 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -119,12 +119,37 @@ func (any *stringAny) ToUint64() uint64 { } func (any *stringAny) ToFloat32() float32 { - parsed, _ := strconv.ParseFloat(any.val, 32) - return float32(parsed) + return float32(any.ToFloat64()) } func (any *stringAny) ToFloat64() float64 { - parsed, _ := strconv.ParseFloat(any.val, 64) + if len(any.val) == 0 { + return 0 + } + + // first char invalid + if any.val[0] != '+' && any.val[0] != '-' && (any.val[0] > '9' || any.val[0] < '0') { + return 0 + } + + // extract valid num expression from string + // eg 123true => 123, -12.12xxa => -12.12 + endPos := 1 + for i := 1; i < len(any.val); i++ { + if any.val[i] == '.' || any.val[i] == 'e' { + endPos = i + 1 + continue + } + + // end position is the first char which is not digit + if any.val[i] >= '0' && any.val[i] <= '9' { + endPos = i + 1 + } else { + endPos = i + break + } + } + parsed, _ := strconv.ParseFloat(any.val[:endPos], 64) return parsed } diff --git a/jsoniter_any_float_test.go b/jsoniter_any_float_test.go index d87c076..ddc5390 100644 --- a/jsoniter_any_float_test.go +++ b/jsoniter_any_float_test.go @@ -15,12 +15,15 @@ var floatConvertMap = map[string]float64{ `"false"`: 0, "123": 123, - `"123true"`: 0, + `"123true"`: 123, + `"+"`: 0, + `"-"`: 0, - `"-123true"`: 0, - "0": 0, - `"0"`: 0, - "-1": -1, + `"-123true"`: -123, + `"-99.9true"`: -99.9, + "0": 0, + `"0"`: 0, + "-1": -1, "1.1": 1.1, "0.0": 0, From ee3313111c513593b5cde0c59c37a8c52332a02c Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 00:39:20 +0800 Subject: [PATCH 14/25] add string tests --- fuzzy_mode_convert_table.md | 14 +++++++------- jsoniter_any_string_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/fuzzy_mode_convert_table.md b/fuzzy_mode_convert_table.md index 25c7196..7f73c34 100644 --- a/fuzzy_mode_convert_table.md +++ b/fuzzy_mode_convert_table.md @@ -1,7 +1,7 @@ -| json type \ dest type | bool | int | uint | string | -| --- | --- | --- | --- |--| -| number | positive => true
negative => true
zero => false| 23.2 => 23
-32.1 => -32| 12.1 => 12
-12.1 => 0|| -| string | empty string => false
string "0" => false
other strings => true | "123.32" => 123
"-123.4" => -123
"123.23xxxw" => 123
"abcde12" => 0
"-32.1" => -32| 13.2 => 13
-1.1 => 0 || -| bool | true => true
false => false| true => 1
false => 0 | true => 1
false => 0 || -| object | true | 0 | 0 || -| array | empty array => false
nonempty array => true| [] => 0
[1,2] => 1 | [] => 0
[1,2] => 1 || \ No newline at end of file +| json type \ dest type | bool | int | uint | float |string| +| --- | --- | --- | --- |--|--| +| number | positive => true
negative => true
zero => false| 23.2 => 23
-32.1 => -32| 12.1 => 12
-12.1 => 0|as normal|| +| string | empty string => false
string "0" => false
other strings => true | "123.32" => 123
"-123.4" => -123
"123.23xxxw" => 123
"abcde12" => 0
"-32.1" => -32| 13.2 => 13
-1.1 => 0 |12.1 => 12.1
-12.3 => -12.3
12.4xxa => 12.4
+1.1e2 =>110 || +| bool | true => true
false => false| true => 1
false => 0 | true => 1
false => 0 |true => 1
false => 0|| +| object | true | 0 | 0 |0|| +| array | empty array => false
nonempty array => true| [] => 0
[1,2] => 1 | [] => 0
[1,2] => 1 |[] => 0
[1,2] => 1|| \ No newline at end of file diff --git a/jsoniter_any_string_test.go b/jsoniter_any_string_test.go index 1567bdc..9512e05 100644 --- a/jsoniter_any_string_test.go +++ b/jsoniter_any_string_test.go @@ -6,6 +6,37 @@ import ( "github.com/json-iterator/go/require" ) +var stringConvertMap = map[string]string{ + "null": "", + "321.1": "321.1", + `"1.1"`: "1.1", + `"-123.1"`: "-123.1", + "0.0": "0.0", + "0": "0", + `"0"`: "0", + `"0.0"`: "0.0", + `"00.0"`: "00.0", + "true": "true", + "false": "false", + `"true"`: "true", + `"false"`: "false", + `"true123"`: "true123", + `"+1"`: "+1", + "[]": "[]", + "[1,2]": "[1,2]", + "{}": "{}", + "{1,2}": "{1,2}", + `{"a":1, "b":true}`: `{"a":1, "b":true}`, +} + +func Test_read_any_to_string(t *testing.T) { + should := require.New(t) + for k, v := range stringConvertMap { + any := Get([]byte(k)) + should.Equal(v, any.ToString(), "original val "+k) + } +} + func Test_read_string_as_any(t *testing.T) { should := require.New(t) any := Get([]byte(`"hello"`)) From 6129e85d531515de8551578e6648123a32bd3888 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 01:21:33 +0800 Subject: [PATCH 15/25] increase coverage --- feature_any_float.go | 12 +++++-- jsoniter_any_float_test.go | 21 ++++++++++-- jsoniter_any_int_test.go | 65 ++++++++++++++++++++++++++++++++++---- 3 files changed, 85 insertions(+), 13 deletions(-) diff --git a/feature_any_float.go b/feature_any_float.go index 47350f8..dec4ff9 100644 --- a/feature_any_float.go +++ b/feature_any_float.go @@ -45,15 +45,21 @@ func (any *floatAny) ToUint() uint { if any.val > 0 { return uint(any.val) } - return uint(-any.val) + return 0 } func (any *floatAny) ToUint32() uint32 { - return uint32(any.val) + if any.val > 0 { + return uint32(any.val) + } + return 0 } func (any *floatAny) ToUint64() uint64 { - return uint64(any.val) + if any.val > 0 { + return uint64(any.val) + } + return 0 } func (any *floatAny) ToFloat32() float32 { diff --git a/jsoniter_any_float_test.go b/jsoniter_any_float_test.go index ddc5390..3845cd2 100644 --- a/jsoniter_any_float_test.go +++ b/jsoniter_any_float_test.go @@ -49,10 +49,25 @@ func Test_read_any_to_float(t *testing.T) { } } -func Test_read_float_as_any(t *testing.T) { +func Test_read_float_to_any(t *testing.T) { should := require.New(t) - any := Get([]byte("12.3")) + any := WrapFloat64(12.3) + anyFloat64 := float64(12.3) + //negaAnyFloat64 := float64(-1.1) + any2 := WrapFloat64(-1.1) should.Equal(float64(12.3), any.ToFloat64()) - should.Equal("12.3", any.ToString()) + //should.Equal("12.3", any.ToString()) should.True(any.ToBool()) + should.Equal(float32(anyFloat64), any.ToFloat32()) + should.Equal(int(anyFloat64), any.ToInt()) + should.Equal(int32(anyFloat64), any.ToInt32()) + should.Equal(int64(anyFloat64), any.ToInt64()) + should.Equal(uint(anyFloat64), any.ToUint()) + should.Equal(uint32(anyFloat64), any.ToUint32()) + should.Equal(uint64(anyFloat64), any.ToUint64()) + should.Equal(uint(0), any2.ToUint()) + should.Equal(uint32(0), any2.ToUint32()) + should.Equal(uint64(0), any2.ToUint64()) + should.Equal(any.ValueType(), Number) + } diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 3a208c7..04c8722 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -2,7 +2,6 @@ package jsoniter import ( "fmt" - "io" "testing" "github.com/json-iterator/go/require" @@ -111,13 +110,65 @@ func Test_read_any_to_uint(t *testing.T) { } -func Test_read_int64_as_any(t *testing.T) { +func Test_read_int64_to_any(t *testing.T) { should := require.New(t) - any := Get([]byte("1234")) - should.Equal(1234, any.ToInt()) - should.Equal(io.EOF, any.LastError()) - should.Equal("1234", any.ToString()) - should.True(any.ToBool()) + any := WrapInt64(12345) + should.Equal(12345, any.ToInt()) + should.Equal(int32(12345), any.ToInt32()) + should.Equal(int64(12345), any.ToInt64()) + should.Equal(uint(12345), any.ToUint()) + should.Equal(uint32(12345), any.ToUint32()) + should.Equal(uint64(12345), any.ToUint64()) + should.Equal(float32(12345), any.ToFloat32()) + should.Equal(float64(12345), any.ToFloat64()) + should.Equal("12345", any.ToString()) + should.Equal(true, any.ToBool()) +} +func Test_read_int32_to_any(t *testing.T) { + should := require.New(t) + any := WrapInt32(12345) + should.Equal(12345, any.ToInt()) + should.Equal(int32(12345), any.ToInt32()) + should.Equal(int64(12345), any.ToInt64()) + should.Equal(uint(12345), any.ToUint()) + should.Equal(uint32(12345), any.ToUint32()) + should.Equal(uint64(12345), any.ToUint64()) + should.Equal(float32(12345), any.ToFloat32()) + should.Equal(float64(12345), any.ToFloat64()) + should.Equal("12345", any.ToString()) + should.Equal(true, any.ToBool()) +} + +func Test_read_uint32_to_any(t *testing.T) { + should := require.New(t) + any := WrapUint32(12345) + should.Equal(12345, any.ToInt()) + should.Equal(int32(12345), any.ToInt32()) + should.Equal(int64(12345), any.ToInt64()) + should.Equal(uint(12345), any.ToUint()) + should.Equal(uint32(12345), any.ToUint32()) + should.Equal(uint64(12345), any.ToUint64()) + should.Equal(float32(12345), any.ToFloat32()) + should.Equal(float64(12345), any.ToFloat64()) + should.Equal("12345", any.ToString()) + should.Equal(true, any.ToBool()) + should.Equal(any.ValueType(), Number) +} + +func Test_read_uint64_to_any(t *testing.T) { + should := require.New(t) + any := WrapUint64(12345) + should.Equal(12345, any.ToInt()) + should.Equal(int32(12345), any.ToInt32()) + should.Equal(int64(12345), any.ToInt64()) + should.Equal(uint(12345), any.ToUint()) + should.Equal(uint32(12345), any.ToUint32()) + should.Equal(uint64(12345), any.ToUint64()) + should.Equal(float32(12345), any.ToFloat32()) + should.Equal(float64(12345), any.ToFloat64()) + should.Equal("12345", any.ToString()) + should.Equal(true, any.ToBool()) + should.Equal(any.ValueType(), Number) } func Test_int_lazy_any_get(t *testing.T) { From 550531a0463343b5996a2554a76f5fc9d68d6bc3 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 11:40:20 +0800 Subject: [PATCH 16/25] increase coverage --- feature_stream_float.go | 2 +- jsoniter_1dot8_only_test.go | 9 ++++---- jsoniter_any_array_test.go | 3 ++- jsoniter_any_bool_test.go | 17 +++++++++++++++ jsoniter_any_int_test.go | 14 +++++++++++++ jsoniter_object_test.go | 5 +++-- wrap_test.go | 41 +++++++++++++++++++++++++++++++++++++ 7 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 wrap_test.go diff --git a/feature_stream_float.go b/feature_stream_float.go index 0b8e8a0..281370b 100644 --- a/feature_stream_float.go +++ b/feature_stream_float.go @@ -1,8 +1,8 @@ package jsoniter import ( - "strconv" "math" + "strconv" ) var _POW10 []uint64 diff --git a/jsoniter_1dot8_only_test.go b/jsoniter_1dot8_only_test.go index 50f7a6a..e2154c4 100644 --- a/jsoniter_1dot8_only_test.go +++ b/jsoniter_1dot8_only_test.go @@ -3,11 +3,12 @@ package jsoniter import ( - "testing" - "encoding/json" - "github.com/json-iterator/go/require" "bytes" + "encoding/json" + "testing" "unicode/utf8" + + "github.com/json-iterator/go/require" ) func Test_new_encoder(t *testing.T) { @@ -41,4 +42,4 @@ func Test_string_encode_with_std_without_html_escape(t *testing.T) { jsoniterOutput := string(jsoniterOutputBytes) should.Equal(stdOutput, jsoniterOutput) } -} \ No newline at end of file +} diff --git a/jsoniter_any_array_test.go b/jsoniter_any_array_test.go index 577745e..e793b14 100644 --- a/jsoniter_any_array_test.go +++ b/jsoniter_any_array_test.go @@ -1,8 +1,9 @@ package jsoniter import ( - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_read_empty_array_as_any(t *testing.T) { diff --git a/jsoniter_any_bool_test.go b/jsoniter_any_bool_test.go index f227cb8..f6b09ee 100644 --- a/jsoniter_any_bool_test.go +++ b/jsoniter_any_bool_test.go @@ -44,4 +44,21 @@ func Test_read_bool_as_any(t *testing.T) { should.False(any.ToBool(), fmt.Sprintf("origin val is %v", k)) } } + +} + +func Test_write_bool_to_stream(t *testing.T) { + should := require.New(t) + any := Get([]byte("true")) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("true", string(stream.Buffer())) + should.Equal(any.ValueType(), Bool) + + any = Get([]byte("false")) + stream = NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("false", string(stream.Buffer())) + + should.Equal(any.ValueType(), Bool) } diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 04c8722..0982497 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -123,6 +123,10 @@ func Test_read_int64_to_any(t *testing.T) { should.Equal(float64(12345), any.ToFloat64()) should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) + should.Equal(any.ValueType(), Number) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("12345", string(stream.Buffer())) } func Test_read_int32_to_any(t *testing.T) { should := require.New(t) @@ -137,6 +141,10 @@ func Test_read_int32_to_any(t *testing.T) { should.Equal(float64(12345), any.ToFloat64()) should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) + should.Equal(any.ValueType(), Number) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("12345", string(stream.Buffer())) } func Test_read_uint32_to_any(t *testing.T) { @@ -153,6 +161,9 @@ func Test_read_uint32_to_any(t *testing.T) { should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) should.Equal(any.ValueType(), Number) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("12345", string(stream.Buffer())) } func Test_read_uint64_to_any(t *testing.T) { @@ -169,6 +180,9 @@ func Test_read_uint64_to_any(t *testing.T) { should.Equal("12345", any.ToString()) should.Equal(true, any.ToBool()) should.Equal(any.ValueType(), Number) + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("12345", string(stream.Buffer())) } func Test_int_lazy_any_get(t *testing.T) { diff --git a/jsoniter_object_test.go b/jsoniter_object_test.go index 9c5e532..c2f9083 100644 --- a/jsoniter_object_test.go +++ b/jsoniter_object_test.go @@ -3,8 +3,9 @@ package jsoniter import ( "bytes" "fmt" - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_empty_object(t *testing.T) { @@ -381,7 +382,7 @@ func Test_shadow_struct_field(t *testing.T) { should.Contains(output, `"max_age":20`) } -func Test_embeded_order(t *testing.T) { +func Test_embedded_order(t *testing.T) { type A struct { Field2 string } diff --git a/wrap_test.go b/wrap_test.go new file mode 100644 index 0000000..7cc59b5 --- /dev/null +++ b/wrap_test.go @@ -0,0 +1,41 @@ +package jsoniter + +import ( + "testing" + + "github.com/json-iterator/go/require" +) + +func Test_wrap_and_valuetype_everything(t *testing.T) { + should := require.New(t) + any := Wrap(int8(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(int16(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(int32(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(int64(10)) + should.Equal(any.ValueType(), Number) + + any = Wrap(uint(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint8(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint16(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint32(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(uint64(10)) + should.Equal(any.ValueType(), Number) + + any = Wrap(float32(10)) + should.Equal(any.ValueType(), Number) + any = Wrap(float64(10)) + should.Equal(any.ValueType(), Number) + + any = Wrap(true) + should.Equal(any.ValueType(), Bool) + any = Wrap(false) + should.Equal(any.ValueType(), Bool) + +} From 1de44419ea2d43ca299b2296e6b0708b836ec863 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 13:55:10 +0800 Subject: [PATCH 17/25] increase coverage --- feature_any_string.go | 2 +- jsoniter_any_float_test.go | 1 + jsoniter_any_int_test.go | 3 + jsoniter_demo_test.go | 6 +- jsoniter_invalid_test.go | 28 +++++++++ jsoniter_wrap_test.go | 118 +++++++++++++++++++++++++++++++++++++ wrap_test.go | 41 ------------- 7 files changed, 154 insertions(+), 45 deletions(-) create mode 100644 jsoniter_invalid_test.go create mode 100644 jsoniter_wrap_test.go delete mode 100644 wrap_test.go diff --git a/feature_any_string.go b/feature_any_string.go index 7b8c39a..5a0e8da 100644 --- a/feature_any_string.go +++ b/feature_any_string.go @@ -136,7 +136,7 @@ func (any *stringAny) ToFloat64() float64 { // eg 123true => 123, -12.12xxa => -12.12 endPos := 1 for i := 1; i < len(any.val); i++ { - if any.val[i] == '.' || any.val[i] == 'e' { + if any.val[i] == '.' || any.val[i] == 'e' || any.val[i] == 'E' { endPos = i + 1 continue } diff --git a/jsoniter_any_float_test.go b/jsoniter_any_float_test.go index 3845cd2..9e179b6 100644 --- a/jsoniter_any_float_test.go +++ b/jsoniter_any_float_test.go @@ -70,4 +70,5 @@ func Test_read_float_to_any(t *testing.T) { should.Equal(uint64(0), any2.ToUint64()) should.Equal(any.ValueType(), Number) + should.Equal("1.23E+01", any.ToString()) } diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index 0982497..e5b2baf 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -183,6 +183,9 @@ func Test_read_uint64_to_any(t *testing.T) { stream := NewStream(ConfigDefault, nil, 32) any.WriteTo(stream) should.Equal("12345", string(stream.Buffer())) + stream = NewStream(ConfigDefault, nil, 32) + stream.WriteUint(uint(123)) + should.Equal("123", string(stream.Buffer())) } func Test_int_lazy_any_get(t *testing.T) { diff --git a/jsoniter_demo_test.go b/jsoniter_demo_test.go index 92fa02c..5c49268 100644 --- a/jsoniter_demo_test.go +++ b/jsoniter_demo_test.go @@ -2,9 +2,9 @@ package jsoniter import ( "encoding/json" - "fmt" - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_bind_api_demo(t *testing.T) { @@ -21,7 +21,7 @@ func Test_iterator_api_demo(t *testing.T) { for iter.ReadArray() { total += iter.ReadInt() } - fmt.Println(total) + //fmt.Println(total) } type People struct { diff --git a/jsoniter_invalid_test.go b/jsoniter_invalid_test.go new file mode 100644 index 0000000..d7876ee --- /dev/null +++ b/jsoniter_invalid_test.go @@ -0,0 +1,28 @@ +package jsoniter + +import ( + "testing" + + "github.com/json-iterator/go/require" +) + +func Test_invalid(t *testing.T) { + should := require.New(t) + any := Get([]byte("[]")) + should.Equal(Invalid, any.Get(0.3).ValueType()) + // is nil correct ? + should.Equal(nil, any.Get(0.3).GetInterface()) + + any = any.Get(0.3) + should.Equal(false, any.ToBool()) + should.Equal(int(0), any.ToInt()) + should.Equal(int32(0), any.ToInt32()) + should.Equal(int64(0), any.ToInt64()) + should.Equal(uint(0), any.ToUint()) + should.Equal(uint32(0), any.ToUint32()) + should.Equal(uint64(0), any.ToUint64()) + should.Equal(float32(0), any.ToFloat32()) + should.Equal(float64(0), any.ToFloat64()) + should.Equal("", any.ToString()) + +} diff --git a/jsoniter_wrap_test.go b/jsoniter_wrap_test.go new file mode 100644 index 0000000..117a0e2 --- /dev/null +++ b/jsoniter_wrap_test.go @@ -0,0 +1,118 @@ +package jsoniter + +import ( + "testing" + + "github.com/json-iterator/go/require" +) + +func Test_wrap_and_valuetype_everything(t *testing.T) { + should := require.New(t) + var i interface{} + any := Get([]byte("123")) + // default of number type is float64 + i = float64(123) + should.Equal(i, any.GetInterface()) + + any = Wrap(int8(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // get interface is not int8 interface + // i = int8(10) + // should.Equal(i, any.GetInterface()) + + any = Wrap(int16(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + //i = int16(10) + //should.Equal(i, any.GetInterface()) + + any = Wrap(int32(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = int32(10) + should.Equal(i, any.GetInterface()) + any = Wrap(int64(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = int64(10) + should.Equal(i, any.GetInterface()) + + any = Wrap(uint(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // not equal + //i = uint(10) + //should.Equal(i, any.GetInterface()) + any = Wrap(uint8(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // not equal + // i = uint8(10) + // should.Equal(i, any.GetInterface()) + any = Wrap(uint16(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + any = Wrap(uint32(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = uint32(10) + should.Equal(i, any.GetInterface()) + any = Wrap(uint64(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = uint64(10) + should.Equal(i, any.GetInterface()) + + any = Wrap(float32(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + // not equal + //i = float32(10) + //should.Equal(i, any.GetInterface()) + any = Wrap(float64(10)) + should.Equal(any.ValueType(), Number) + should.Equal(any.LastError(), nil) + i = float64(10) + should.Equal(i, any.GetInterface()) + + any = Wrap(true) + should.Equal(any.ValueType(), Bool) + should.Equal(any.LastError(), nil) + i = true + should.Equal(i, any.GetInterface()) + any = Wrap(false) + should.Equal(any.ValueType(), Bool) + should.Equal(any.LastError(), nil) + i = false + should.Equal(i, any.GetInterface()) + + any = Wrap(nil) + should.Equal(any.ValueType(), Nil) + should.Equal(any.LastError(), nil) + i = nil + should.Equal(i, any.GetInterface()) + + stream := NewStream(ConfigDefault, nil, 32) + any.WriteTo(stream) + should.Equal("null", string(stream.Buffer())) + should.Equal(any.LastError(), nil) + + any = Wrap(struct{ age int }{age: 1}) + should.Equal(any.ValueType(), Object) + should.Equal(any.LastError(), nil) + i = struct{ age int }{age: 1} + should.Equal(i, any.GetInterface()) + + any = Wrap(map[string]interface{}{"abc": 1}) + should.Equal(any.ValueType(), Object) + should.Equal(any.LastError(), nil) + i = map[string]interface{}{"abc": 1} + should.Equal(i, any.GetInterface()) + + any = Wrap("abc") + i = "abc" + should.Equal(i, any.GetInterface()) + should.Equal(nil, any.LastError()) + +} diff --git a/wrap_test.go b/wrap_test.go deleted file mode 100644 index 7cc59b5..0000000 --- a/wrap_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package jsoniter - -import ( - "testing" - - "github.com/json-iterator/go/require" -) - -func Test_wrap_and_valuetype_everything(t *testing.T) { - should := require.New(t) - any := Wrap(int8(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(int16(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(int32(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(int64(10)) - should.Equal(any.ValueType(), Number) - - any = Wrap(uint(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint8(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint16(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint32(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(uint64(10)) - should.Equal(any.ValueType(), Number) - - any = Wrap(float32(10)) - should.Equal(any.ValueType(), Number) - any = Wrap(float64(10)) - should.Equal(any.ValueType(), Number) - - any = Wrap(true) - should.Equal(any.ValueType(), Bool) - any = Wrap(false) - should.Equal(any.ValueType(), Bool) - -} From ca6a524d4f19c3aaa17859a17cf726b6cd1affa8 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 14:11:27 +0800 Subject: [PATCH 18/25] add codecov.yml ignore output tests --- .codecov.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .codecov.yml diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 0000000..a5727d1 --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,3 @@ +codecov: + ignore: + - "output_tests" From 27725b7139aa74c56974aa4973bd6405ebb8d32c Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 15:17:39 +0800 Subject: [PATCH 19/25] update codecov.yml --- .codecov.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.codecov.yml b/.codecov.yml index a5727d1..f948b1a 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,3 +1,5 @@ codecov: ignore: - "output_tests" + - "assert" + From 4907dc00f653c82ce5cd0e2223d73fe5763ae99c Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 16:41:24 +0800 Subject: [PATCH 20/25] change codecov file --- .codecov.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.codecov.yml b/.codecov.yml index f948b1a..a22f9d3 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,5 +1,4 @@ -codecov: - ignore: - - "output_tests" - - "assert" +ignore: + - "output_tests/.*" + - "assert" From 6a289f32c243e5414ed17560389e2d95e1dc7e59 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 16:49:42 +0800 Subject: [PATCH 21/25] fix codecov.yml curl https://codecov.io/validate --data-binary @.codecov.yml --- .codecov.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.codecov.yml b/.codecov.yml index a22f9d3..10fb55a 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,4 +1,4 @@ ignore: - "output_tests/.*" - - "assert" + - "assert/.*" From dd88d25090b6ad7185b8502ab72e980d9bc7d149 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 18:59:28 +0800 Subject: [PATCH 22/25] add require --- .codecov.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.codecov.yml b/.codecov.yml index 10fb55a..ed1daa2 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1,4 +1,5 @@ ignore: - "output_tests/.*" - "assert/.*" + - "require/.*" From 8675af13bf9055a695a31c3be181d069d5cde949 Mon Sep 17 00:00:00 2001 From: Xargin Date: Wed, 5 Jul 2017 20:30:54 +0800 Subject: [PATCH 23/25] increase coverage --- feature_any_object.go | 82 ++++++++----------------------------- jsoniter_any_array_test.go | 28 ++++++++++++- jsoniter_any_object_test.go | 34 ++++++++++++++- 3 files changed, 77 insertions(+), 67 deletions(-) diff --git a/feature_any_object.go b/feature_any_object.go index 6e51295..5386f83 100644 --- a/feature_any_object.go +++ b/feature_any_object.go @@ -182,59 +182,35 @@ func (any *objectAny) ToBool() bool { } func (any *objectAny) ToInt() int { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToInt32() int32 { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToInt64() int64 { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToUint() uint { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToUint32() uint32 { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToUint64() uint64 { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToFloat32() float32 { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToFloat64() float64 { - if any.val.NumField() == 0 { - return 0 - } - return 1 + return 0 } func (any *objectAny) ToString() string { @@ -333,63 +309,39 @@ func (any *mapAny) LastError() error { } func (any *mapAny) ToBool() bool { - return any.val.Len() != 0 + return true } func (any *mapAny) ToInt() int { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToInt32() int32 { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToInt64() int64 { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToUint() uint { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToUint32() uint32 { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToUint64() uint64 { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToFloat32() float32 { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToFloat64() float64 { - if any.val.Len() == 0 { - return 0 - } - return 1 + return 0 } func (any *mapAny) ToString() string { diff --git a/jsoniter_any_array_test.go b/jsoniter_any_array_test.go index e793b14..e888503 100644 --- a/jsoniter_any_array_test.go +++ b/jsoniter_any_array_test.go @@ -47,10 +47,36 @@ func Test_read_two_element_array_as_any(t *testing.T) { should.Equal([]int{1, 2}, arr) } -func Test_wrap_array(t *testing.T) { +func Test_wrap_array_and_convert_to_any(t *testing.T) { should := require.New(t) any := Wrap([]int{1, 2, 3}) + any2 := Wrap([]int{}) + should.Equal("[1,2,3]", any.ToString()) + should.True(any.ToBool()) + should.False(any2.ToBool()) + + should.Equal(1, any.ToInt()) + should.Equal(0, any2.ToInt()) + should.Equal(int32(1), any.ToInt32()) + should.Equal(int32(0), any2.ToInt32()) + should.Equal(int64(1), any.ToInt64()) + should.Equal(int64(0), any2.ToInt64()) + should.Equal(uint(1), any.ToUint()) + should.Equal(uint(0), any2.ToUint()) + should.Equal(uint32(1), any.ToUint32()) + should.Equal(uint32(0), any2.ToUint32()) + should.Equal(uint64(1), any.ToUint64()) + should.Equal(uint64(0), any2.ToUint64()) + should.Equal(float32(1), any.ToFloat32()) + should.Equal(float32(0), any2.ToFloat32()) + should.Equal(float64(1), any.ToFloat64()) + should.Equal(float64(0), any2.ToFloat64()) + should.Equal(3, any.Size()) + should.Equal(0, any2.Size()) + + var i interface{} = []int{1, 2, 3} + should.Equal(i, any.GetInterface()) } func Test_array_lazy_any_get(t *testing.T) { diff --git a/jsoniter_any_object_test.go b/jsoniter_any_object_test.go index fbd3bb4..2ce141b 100644 --- a/jsoniter_any_object_test.go +++ b/jsoniter_any_object_test.go @@ -49,7 +49,21 @@ func Test_object_lazy_any_get_invalid(t *testing.T) { should.Equal(Invalid, any.Get(1).ValueType()) } -func Test_wrap_object(t *testing.T) { +func Test_wrap_map_and_convert_to_any(t *testing.T) { + should := require.New(t) + any := Wrap(map[string]interface{}{"a": 1}) + should.True(any.ToBool()) + should.Equal(0, any.ToInt()) + should.Equal(int32(0), any.ToInt32()) + should.Equal(int64(0), any.ToInt64()) + should.Equal(float32(0), any.ToFloat32()) + should.Equal(float64(0), any.ToFloat64()) + should.Equal(uint(0), any.ToUint()) + should.Equal(uint32(0), any.ToUint32()) + should.Equal(uint64(0), any.ToUint64()) +} + +func Test_wrap_object_and_convert_to_any(t *testing.T) { should := require.New(t) type TestObject struct { Field1 string @@ -60,6 +74,24 @@ func Test_wrap_object(t *testing.T) { any = Wrap(TestObject{"hello", "world"}) should.Equal(2, any.Size()) should.Equal(`{"Field1":"hello"}`, any.Get('*').ToString()) + + should.Equal(0, any.ToInt()) + should.Equal(int32(0), any.ToInt32()) + should.Equal(int64(0), any.ToInt64()) + should.Equal(float32(0), any.ToFloat32()) + should.Equal(float64(0), any.ToFloat64()) + should.Equal(uint(0), any.ToUint()) + should.Equal(uint32(0), any.ToUint32()) + should.Equal(uint64(0), any.ToUint64()) + should.True(any.ToBool()) + should.Equal(`{"Field1":"hello"}`, any.ToString()) + + // cannot pass! + //stream := NewStream(ConfigDefault, nil, 32) + //any.WriteTo(stream) + //should.Equal(`{"Field1":"hello"}`, string(stream.Buffer())) + // cannot pass! + } func Test_any_within_struct(t *testing.T) { From ee6536c50a4f489476f397fd3ac7851883c556c7 Mon Sep 17 00:00:00 2001 From: Xargin Date: Thu, 6 Jul 2017 11:44:39 +0800 Subject: [PATCH 24/25] increase coverage --- jsoniter_any_int_test.go | 2 + jsoniter_bool_test.go | 8 +++- jsoniter_float_test.go | 11 +++++- jsoniter_invalid_test.go | 2 + jsoniter_map_test.go | 11 +++++- jsoniter_must_be_valid_test.go | 71 ++++++++++++++++++++++++++++++++++ jsoniter_object_test.go | 6 +++ 7 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 jsoniter_must_be_valid_test.go diff --git a/jsoniter_any_int_test.go b/jsoniter_any_int_test.go index e5b2baf..f8b9518 100644 --- a/jsoniter_any_int_test.go +++ b/jsoniter_any_int_test.go @@ -191,5 +191,7 @@ func Test_read_uint64_to_any(t *testing.T) { func Test_int_lazy_any_get(t *testing.T) { should := require.New(t) any := Get([]byte("1234")) + // panic!! + //should.Equal(any.LastError(), io.EOF) should.Equal(Invalid, any.Get(1, "2").ValueType()) } diff --git a/jsoniter_bool_test.go b/jsoniter_bool_test.go index 41d4a3a..771f514 100644 --- a/jsoniter_bool_test.go +++ b/jsoniter_bool_test.go @@ -3,8 +3,9 @@ package jsoniter import ( "bytes" "encoding/json" - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_true(t *testing.T) { @@ -27,9 +28,10 @@ func Test_write_true_false(t *testing.T) { stream := NewStream(ConfigDefault, buf, 4096) stream.WriteTrue() stream.WriteFalse() + stream.WriteBool(false) stream.Flush() should.Nil(stream.Error) - should.Equal("truefalse", buf.String()) + should.Equal("truefalsefalse", buf.String()) } func Test_write_val_bool(t *testing.T) { @@ -37,7 +39,9 @@ func Test_write_val_bool(t *testing.T) { buf := &bytes.Buffer{} stream := NewStream(ConfigDefault, buf, 4096) stream.WriteVal(true) + should.Equal(stream.Buffered(), 4) stream.Flush() + should.Equal(stream.Buffered(), 0) should.Nil(stream.Error) should.Equal("true", buf.String()) } diff --git a/jsoniter_float_test.go b/jsoniter_float_test.go index 547c2d6..8895346 100644 --- a/jsoniter_float_test.go +++ b/jsoniter_float_test.go @@ -6,9 +6,10 @@ import ( "bytes" "encoding/json" "fmt" - "github.com/json-iterator/go/require" "strconv" "testing" + + "github.com/json-iterator/go/require" ) func Test_read_big_float(t *testing.T) { @@ -111,6 +112,10 @@ func Test_write_float32(t *testing.T) { stream.Flush() should.Nil(stream.Error) should.Equal("abcdefg1.123456", buf.String()) + + stream = NewStream(ConfigDefault, nil, 0) + stream.WriteFloat32(float32(0.0000001)) + should.Equal("1e-07", string(stream.buf)) } func Test_write_float64(t *testing.T) { @@ -144,6 +149,10 @@ func Test_write_float64(t *testing.T) { stream.Flush() should.Nil(stream.Error) should.Equal("abcdefg1.123456", buf.String()) + + stream = NewStream(ConfigDefault, nil, 0) + stream.WriteFloat64(float64(0.0000001)) + should.Equal("1e-07", string(stream.buf)) } func Test_read_float64_cursor(t *testing.T) { diff --git a/jsoniter_invalid_test.go b/jsoniter_invalid_test.go index d7876ee..2ec4759 100644 --- a/jsoniter_invalid_test.go +++ b/jsoniter_invalid_test.go @@ -25,4 +25,6 @@ func Test_invalid(t *testing.T) { should.Equal(float64(0), any.ToFloat64()) should.Equal("", any.ToString()) + should.Equal(Invalid, any.Get(0.1).Get(1).ValueType()) + } diff --git a/jsoniter_map_test.go b/jsoniter_map_test.go index 794bcb1..378165b 100644 --- a/jsoniter_map_test.go +++ b/jsoniter_map_test.go @@ -2,9 +2,10 @@ package jsoniter import ( "encoding/json" - "github.com/json-iterator/go/require" "math/big" "testing" + + "github.com/json-iterator/go/require" ) func Test_read_map(t *testing.T) { @@ -30,6 +31,14 @@ func Test_map_wrapper_any_get_all(t *testing.T) { should := require.New(t) any := Wrap(map[string][]int{"Field1": {1, 2}}) should.Equal(`{"Field1":1}`, any.Get('*', 0).ToString()) + should.Contains(any.Keys(), "Field1") + should.Equal(any.GetObject()["Field1"].ToInt(), 1) + + // map write to + stream := NewStream(ConfigDefault, nil, 0) + any.WriteTo(stream) + // TODO cannot pass + //should.Equal(string(stream.buf), "") } func Test_write_val_map(t *testing.T) { diff --git a/jsoniter_must_be_valid_test.go b/jsoniter_must_be_valid_test.go new file mode 100644 index 0000000..01c6e44 --- /dev/null +++ b/jsoniter_must_be_valid_test.go @@ -0,0 +1,71 @@ +package jsoniter + +import ( + "testing" + + "github.com/json-iterator/go/require" +) + +// if must be valid is useless, just drop this test +func Test_must_be_valid(t *testing.T) { + should := require.New(t) + any := Get([]byte("123")) + should.Equal(any.MustBeValid().ToInt(), 123) + + any = Wrap(int8(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(int16(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(int32(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(int64(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(uint(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(uint8(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(uint16(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(uint32(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(uint64(10)) + should.Equal(any.MustBeValid().ToInt(), 10) + + any = Wrap(float32(10)) + should.Equal(any.MustBeValid().ToFloat64(), float64(10)) + + any = Wrap(float64(10)) + should.Equal(any.MustBeValid().ToFloat64(), float64(10)) + + any = Wrap(true) + should.Equal(any.MustBeValid().ToFloat64(), float64(1)) + + any = Wrap(false) + should.Equal(any.MustBeValid().ToFloat64(), float64(0)) + + any = Wrap(nil) + should.Equal(any.MustBeValid().ToFloat64(), float64(0)) + + any = Wrap(struct{ age int }{age: 1}) + should.Equal(any.MustBeValid().ToFloat64(), float64(0)) + + any = Wrap(map[string]interface{}{"abc": 1}) + should.Equal(any.MustBeValid().ToFloat64(), float64(0)) + + any = Wrap("abc") + should.Equal(any.MustBeValid().ToFloat64(), float64(0)) + + any = Wrap([]int{}) + should.Equal(any.MustBeValid().ToFloat64(), float64(0)) + + any = Wrap([]int{1, 2}) + should.Equal(any.MustBeValid().ToFloat64(), float64(1)) +} diff --git a/jsoniter_object_test.go b/jsoniter_object_test.go index c2f9083..b69f0bf 100644 --- a/jsoniter_object_test.go +++ b/jsoniter_object_test.go @@ -34,6 +34,7 @@ func Test_one_field(t *testing.T) { should.Equal("a", field) return true })) + } func Test_two_field(t *testing.T) { @@ -70,6 +71,11 @@ func Test_object_wrapper_any_get_all(t *testing.T) { } any := Wrap(TestObject{[]int{1, 2}, []int{3, 4}}) should.Contains(any.Get('*', 0).ToString(), `"Field2":3`) + should.Contains(any.Keys(), "Field1") + should.Contains(any.Keys(), "Field2") + should.NotContains(any.Keys(), "Field3") + + //should.Contains(any.GetObject()["Field1"].GetArray()[0], 1) } func Test_write_object(t *testing.T) { From d4c0cb2986862f1bcaf728319ded89a9a61279c0 Mon Sep 17 00:00:00 2001 From: Xargin Date: Thu, 6 Jul 2017 15:24:06 +0800 Subject: [PATCH 25/25] increase reflect object coverage, need optimize in the future --- jsoniter_object_test.go | 134 +++++++++++++++++++++++++++++++++++++++- jsoniter_skip_test.go | 4 +- 2 files changed, 136 insertions(+), 2 deletions(-) diff --git a/jsoniter_object_test.go b/jsoniter_object_test.go index b69f0bf..9599b6a 100644 --- a/jsoniter_object_test.go +++ b/jsoniter_object_test.go @@ -174,6 +174,106 @@ func Test_decode_five_fields_struct(t *testing.T) { should.Equal("e", obj.Field5) } +func Test_decode_six_fields_struct(t *testing.T) { + should := require.New(t) + type TestObject struct { + Field1 string + Field2 string + Field3 string + Field4 string + Field5 string + Field6 string + } + obj := TestObject{} + should.Nil(UnmarshalFromString(`{}`, &obj)) + should.Equal("", obj.Field1) + should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e", "Field6": "x"}`, &obj)) + should.Equal("a", obj.Field1) + should.Equal("b", obj.Field2) + should.Equal("c", obj.Field3) + should.Equal("d", obj.Field4) + should.Equal("e", obj.Field5) + should.Equal("x", obj.Field6) +} + +func Test_decode_seven_fields_struct(t *testing.T) { + should := require.New(t) + type TestObject struct { + Field1 string + Field2 string + Field3 string + Field4 string + Field5 string + Field6 string + Field7 string + } + obj := TestObject{} + should.Nil(UnmarshalFromString(`{}`, &obj)) + should.Equal("", obj.Field1) + should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e", "Field6": "x", "Field7":"y"}`, &obj)) + should.Equal("a", obj.Field1) + should.Equal("b", obj.Field2) + should.Equal("c", obj.Field3) + should.Equal("d", obj.Field4) + should.Equal("e", obj.Field5) + should.Equal("x", obj.Field6) + should.Equal("y", obj.Field7) +} + +func Test_decode_eight_fields_struct(t *testing.T) { + should := require.New(t) + type TestObject struct { + Field1 string + Field2 string + Field3 string + Field4 string + Field5 string + Field6 string + Field7 string + Field8 string + } + obj := TestObject{} + should.Nil(UnmarshalFromString(`{}`, &obj)) + should.Equal("", obj.Field1) + should.Nil(UnmarshalFromString(`{"Field8":"1", "Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e", "Field6": "x", "Field7":"y"}`, &obj)) + should.Equal("a", obj.Field1) + should.Equal("b", obj.Field2) + should.Equal("c", obj.Field3) + should.Equal("d", obj.Field4) + should.Equal("e", obj.Field5) + should.Equal("x", obj.Field6) + should.Equal("y", obj.Field7) + should.Equal("1", obj.Field8) +} + +func Test_decode_nine_fields_struct(t *testing.T) { + should := require.New(t) + type TestObject struct { + Field1 string + Field2 string + Field3 string + Field4 string + Field5 string + Field6 string + Field7 string + Field8 string + Field9 string + } + obj := TestObject{} + should.Nil(UnmarshalFromString(`{}`, &obj)) + should.Equal("", obj.Field1) + should.Nil(UnmarshalFromString(`{"Field8" : "zzzzzzzzzzz", "Field7": "zz", "Field6" : "xx", "Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e", "Field9":"f"}`, &obj)) + should.Equal("a", obj.Field1) + should.Equal("b", obj.Field2) + should.Equal("c", obj.Field3) + should.Equal("d", obj.Field4) + should.Equal("e", obj.Field5) + should.Equal("xx", obj.Field6) + should.Equal("zz", obj.Field7) + should.Equal("zzzzzzzzzzz", obj.Field8) + should.Equal("f", obj.Field9) +} + func Test_decode_ten_fields_struct(t *testing.T) { should := require.New(t) type TestObject struct { @@ -191,12 +291,44 @@ func Test_decode_ten_fields_struct(t *testing.T) { obj := TestObject{} should.Nil(UnmarshalFromString(`{}`, &obj)) should.Equal("", obj.Field1) - should.Nil(UnmarshalFromString(`{"Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj)) + should.Nil(UnmarshalFromString(`{"Field10":"x", "Field9": "x", "Field8":"x", "Field7":"x", "Field6":"x", "Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj)) should.Equal("a", obj.Field1) should.Equal("b", obj.Field2) should.Equal("c", obj.Field3) should.Equal("d", obj.Field4) should.Equal("e", obj.Field5) + should.Equal("x", obj.Field6) + should.Equal("x", obj.Field7) + should.Equal("x", obj.Field8) + should.Equal("x", obj.Field9) + should.Equal("x", obj.Field10) +} + +func Test_decode_more_than_ten_fields_struct(t *testing.T) { + should := require.New(t) + type TestObject struct { + Field1 string + Field2 string + Field3 string + Field4 string + Field5 string + Field6 string + Field7 string + Field8 string + Field9 string + Field10 string + Field11 int + } + obj := TestObject{} + should.Nil(UnmarshalFromString(`{}`, &obj)) + should.Equal("", obj.Field1) + should.Nil(UnmarshalFromString(`{"Field11":1, "Field1": "a", "Field2": "b", "Field3": "c", "Field4": "d", "Field5": "e"}`, &obj)) + should.Equal("a", obj.Field1) + should.Equal("b", obj.Field2) + should.Equal("c", obj.Field3) + should.Equal("d", obj.Field4) + should.Equal("e", obj.Field5) + should.Equal(1, obj.Field11) } func Test_decode_struct_field_with_tag(t *testing.T) { diff --git a/jsoniter_skip_test.go b/jsoniter_skip_test.go index c789a98..1212f95 100644 --- a/jsoniter_skip_test.go +++ b/jsoniter_skip_test.go @@ -3,8 +3,9 @@ package jsoniter import ( "bytes" "encoding/json" - "github.com/json-iterator/go/require" "testing" + + "github.com/json-iterator/go/require" ) func Test_skip_number(t *testing.T) { @@ -15,6 +16,7 @@ func Test_skip_number(t *testing.T) { if iter.ReadString() != "b" { t.FailNow() } + } func Test_skip_null(t *testing.T) {