mirror of
https://github.com/json-iterator/go.git
synced 2025-03-20 20:54:55 +02:00
wrap float
This commit is contained in:
parent
85edb698c8
commit
cf4113fc22
@ -65,7 +65,11 @@ func (any *baseAny) SetObject(map[string]Any) bool {
|
||||
}
|
||||
|
||||
func WrapInt64(val int64) Any {
|
||||
return &intAny{baseAny{}, nil, val}
|
||||
return &intAny{baseAny{}, val}
|
||||
}
|
||||
|
||||
func WrapFloat64(val float64) Any {
|
||||
return &floatAny{baseAny{}, val}
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadAny() Any {
|
||||
|
@ -3,6 +3,7 @@ package jsoniter
|
||||
import (
|
||||
"io"
|
||||
"unsafe"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type floatLazyAny struct {
|
||||
@ -82,4 +83,57 @@ func (any *floatLazyAny) WriteTo(stream *Stream) {
|
||||
func (any *floatLazyAny) GetInterface() interface{} {
|
||||
any.fillCache()
|
||||
return any.cache
|
||||
}
|
||||
|
||||
type floatAny struct {
|
||||
baseAny
|
||||
val float64
|
||||
}
|
||||
|
||||
func (any *floatAny) Parse() *Iterator {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (any *floatAny) ValueType() ValueType {
|
||||
return Number
|
||||
}
|
||||
|
||||
func (any *floatAny) LastError() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (any *floatAny) ToBool() bool {
|
||||
return any.ToFloat64() != 0
|
||||
}
|
||||
|
||||
func (any *floatAny) ToInt() int {
|
||||
return int(any.val)
|
||||
}
|
||||
|
||||
func (any *floatAny) ToInt32() int32 {
|
||||
return int32(any.val)
|
||||
}
|
||||
|
||||
func (any *floatAny) ToInt64() int64 {
|
||||
return int64(any.val)
|
||||
}
|
||||
|
||||
func (any *floatAny) ToFloat32() float32 {
|
||||
return float32(any.val)
|
||||
}
|
||||
|
||||
func (any *floatAny) ToFloat64() float64 {
|
||||
return any.val
|
||||
}
|
||||
|
||||
func (any *floatAny) ToString() string {
|
||||
return strconv.FormatFloat(any.val, 'E', -1, 64)
|
||||
}
|
||||
|
||||
func (any *floatAny) WriteTo(stream *Stream) {
|
||||
stream.WriteFloat64(any.val)
|
||||
}
|
||||
|
||||
func (any *floatAny) GetInterface() interface{} {
|
||||
return any.val
|
||||
}
|
@ -87,12 +87,11 @@ func (any *intLazyAny) GetInterface() interface{} {
|
||||
|
||||
type intAny struct {
|
||||
baseAny
|
||||
err error
|
||||
val int64
|
||||
}
|
||||
|
||||
func (any *intAny) LastError() error {
|
||||
return any.err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (any *intAny) ValueType() ValueType {
|
||||
|
@ -90,6 +90,13 @@ func Test_array_lazy_any_get(t *testing.T) {
|
||||
should.Equal("[1,[2,3],4]", any.ToString())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_all(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any, err := UnmarshalAnyFromString("[[1],[2],[3,4]]")
|
||||
should.Nil(err)
|
||||
should.Equal("[1,2,3]", any.Get('*',0).ToString())
|
||||
}
|
||||
|
||||
func Test_array_lazy_any_get_invalid(t *testing.T) {
|
||||
should := require.New(t)
|
||||
any, err := UnmarshalAnyFromString("[]")
|
||||
|
@ -60,6 +60,13 @@ func Test_read_float_as_any(t *testing.T) {
|
||||
should.True(any.ToBool())
|
||||
}
|
||||
|
||||
func Test_wrap_float(t *testing.T) {
|
||||
should := require.New(t)
|
||||
str, err := MarshalToString(WrapFloat64(12.3))
|
||||
should.Nil(err)
|
||||
should.Equal("12.3", str)
|
||||
}
|
||||
|
||||
func Test_write_float32(t *testing.T) {
|
||||
vals := []float32{0, 1, -1, 99, 0xff, 0xfff, 0xffff, 0xfffff, 0xffffff, 0x4ffffff, 0xfffffff,
|
||||
-0x4ffffff, -0xfffffff, 1.2345, 1.23456, 1.234567, 1.001}
|
||||
|
Loading…
x
Reference in New Issue
Block a user