Parcourir la source

object cannot covert to int/float in php, so change covert result to zero

Xargin il y a 8 ans
Parent
commit
ac8dd56dfb
3 fichiers modifiés avec 14 ajouts et 42 suppressions
  1. 8 40
      feature_any_object.go
  2. 3 0
      jsoniter_any_int_test.go
  3. 3 2
      jsoniter_any_object_test.go

+ 8 - 40
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 {

+ 3 - 0
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,

+ 3 - 2
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())