1
0
mirror of https://github.com/json-iterator/go.git synced 2025-03-26 21:12:40 +02:00
json-iterator/feature_any_float.go

76 lines
1.2 KiB
Go
Raw Normal View History

2017-01-22 23:29:48 +08:00
package jsoniter
2017-01-22 23:38:55 +08:00
import (
"io"
"unsafe"
)
2017-01-22 23:29:48 +08:00
type floatLazyAny struct {
baseAny
2017-01-22 23:29:48 +08:00
buf []byte
iter *Iterator
err error
2017-01-22 23:38:55 +08:00
cache float64
}
2017-01-26 15:40:38 +08:00
func (any *floatLazyAny) Parse() *Iterator {
2017-01-22 23:38:55 +08:00
iter := any.iter
if iter == nil {
iter = NewIterator()
}
iter.ResetBytes(any.buf)
2017-01-26 15:40:38 +08:00
return iter
}
func (any *floatLazyAny) fillCache() {
if any.err != nil {
return
}
iter := any.Parse()
2017-01-22 23:38:55 +08:00
any.cache = iter.ReadFloat64()
if iter.Error != io.EOF {
iter.reportError("floatLazyAny", "there are bytes left")
}
any.err = iter.Error
2017-01-22 23:29:48 +08:00
}
func (any *floatLazyAny) LastError() error {
return any.err
}
func (any *floatLazyAny) ToBool() bool {
2017-01-22 23:38:55 +08:00
return any.ToFloat64() != 0
2017-01-22 23:29:48 +08:00
}
func (any *floatLazyAny) ToInt() int {
2017-01-22 23:38:55 +08:00
any.fillCache()
return int(any.cache)
2017-01-22 23:29:48 +08:00
}
func (any *floatLazyAny) ToInt32() int32 {
2017-01-22 23:38:55 +08:00
any.fillCache()
return int32(any.cache)
2017-01-22 23:29:48 +08:00
}
func (any *floatLazyAny) ToInt64() int64 {
2017-01-22 23:38:55 +08:00
any.fillCache()
return int64(any.cache)
2017-01-22 23:29:48 +08:00
}
func (any *floatLazyAny) ToFloat32() float32 {
2017-01-22 23:38:55 +08:00
any.fillCache()
return float32(any.cache)
2017-01-22 23:29:48 +08:00
}
func (any *floatLazyAny) ToFloat64() float64 {
2017-01-22 23:38:55 +08:00
any.fillCache()
return any.cache
2017-01-22 23:29:48 +08:00
}
func (any *floatLazyAny) ToString() string {
2017-01-22 23:38:55 +08:00
return *(*string)(unsafe.Pointer(&any.buf))
2017-01-26 14:56:31 +08:00
}
func (any *floatLazyAny) WriteTo(stream *Stream) {
stream.Write(any.buf)
2017-01-22 23:29:48 +08:00
}