1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/feature_any_string.go

231 lines
3.9 KiB
Go
Raw Normal View History

2017-01-23 02:33:43 +02:00
package jsoniter
import (
"io"
2017-01-26 10:41:49 +02:00
"strconv"
2017-01-23 02:33:43 +02:00
)
type stringLazyAny struct{
baseAny
2017-01-23 02:33:43 +02:00
buf []byte
iter *Iterator
err error
cache string
}
2017-01-26 10:24:01 +02:00
func (any *stringLazyAny) ValueType() ValueType {
return String
}
2017-01-26 09:40:38 +02:00
func (any *stringLazyAny) Parse() *Iterator {
iter := any.iter
if iter == nil {
iter = NewIterator()
any.iter = iter
}
iter.ResetBytes(any.buf)
return iter
}
2017-01-23 02:33:43 +02:00
func (any *stringLazyAny) fillCache() {
if any.err != nil {
return
}
2017-01-26 09:40:38 +02:00
iter := any.Parse()
2017-01-23 02:33:43 +02:00
any.cache = iter.ReadString()
if iter.Error != io.EOF {
iter.reportError("stringLazyAny", "there are bytes left")
}
any.err = iter.Error
}
func (any *stringLazyAny) LastError() error {
return any.err
}
func (any *stringLazyAny) ToBool() bool {
str := any.ToString()
if str == "false" {
return false
}
for _, c := range str {
switch c {
case ' ', '\n', '\r', '\t':
default:
return true
}
}
return false
}
func (any *stringLazyAny) ToInt() int {
2017-01-26 09:40:38 +02:00
iter := any.Parse()
2017-01-23 02:33:43 +02:00
iter.head++
val := iter.ReadInt()
any.err = iter.Error
return val
}
func (any *stringLazyAny) ToInt32() int32 {
2017-01-26 09:40:38 +02:00
iter := any.Parse()
2017-01-23 02:33:43 +02:00
iter.head++
val := iter.ReadInt32()
any.err = iter.Error
return val
}
func (any *stringLazyAny) ToInt64() int64 {
2017-01-26 09:40:38 +02:00
iter := any.Parse()
2017-01-23 02:33:43 +02:00
iter.head++
val := iter.ReadInt64()
any.err = iter.Error
return val
}
2017-01-29 10:55:32 +02:00
func (any *stringLazyAny) ToUint() uint {
iter := any.Parse()
iter.head++
val := iter.ReadUint()
any.err = iter.Error
return val
}
func (any *stringLazyAny) ToUint32() uint32 {
iter := any.Parse()
iter.head++
val := iter.ReadUint32()
any.err = iter.Error
return val
}
func (any *stringLazyAny) ToUint64() uint64 {
iter := any.Parse()
iter.head++
val := iter.ReadUint64()
any.err = iter.Error
return val
}
2017-01-23 02:33:43 +02:00
func (any *stringLazyAny) ToFloat32() float32 {
2017-01-26 09:40:38 +02:00
iter := any.Parse()
2017-01-23 02:33:43 +02:00
iter.head++
val := iter.ReadFloat32()
any.err = iter.Error
return val
}
func (any *stringLazyAny) ToFloat64() float64 {
2017-01-26 09:40:38 +02:00
iter := any.Parse()
2017-01-23 02:33:43 +02:00
iter.head++
val := iter.ReadFloat64()
any.err = iter.Error
return val
}
func (any *stringLazyAny) ToString() string {
any.fillCache()
return any.cache
2017-01-26 08:56:31 +02:00
}
func (any *stringLazyAny) WriteTo(stream *Stream) {
stream.Write(any.buf)
2017-01-26 09:44:10 +02:00
}
func (any *stringLazyAny) GetInterface() interface{} {
any.fillCache()
return any.cache
2017-01-26 10:41:49 +02:00
}
type stringAny struct{
baseAny
err error
val string
}
func (any *stringAny) Parse() *Iterator {
return nil
}
func (any *stringAny) ValueType() ValueType {
return String
}
func (any *stringAny) LastError() error {
return any.err
}
func (any *stringAny) ToBool() bool {
str := any.ToString()
if str == "false" {
return false
}
for _, c := range str {
switch c {
case ' ', '\n', '\r', '\t':
default:
return true
}
}
return false
}
func (any *stringAny) ToInt() int {
parsed, err := strconv.ParseInt(any.val, 10, 64)
any.err = err
return int(parsed)
}
func (any *stringAny) ToInt32() int32 {
parsed, err := strconv.ParseInt(any.val, 10, 32)
any.err = err
return int32(parsed)
}
func (any *stringAny) ToInt64() int64 {
parsed, err := strconv.ParseInt(any.val, 10, 64)
any.err = err
return parsed
}
2017-01-29 10:55:32 +02:00
func (any *stringAny) ToUint() uint {
parsed, err := strconv.ParseUint(any.val, 10, 64)
any.err = err
return uint(parsed)
}
func (any *stringAny) ToUint32() uint32 {
parsed, err := strconv.ParseUint(any.val, 10, 32)
any.err = err
return uint32(parsed)
}
func (any *stringAny) ToUint64() uint64 {
parsed, err := strconv.ParseUint(any.val, 10, 64)
any.err = err
return parsed
}
2017-01-26 10:41:49 +02:00
func (any *stringAny) ToFloat32() float32 {
parsed, err := strconv.ParseFloat(any.val, 32)
any.err = err
return float32(parsed)
}
func (any *stringAny) ToFloat64() float64 {
parsed, err := strconv.ParseFloat(any.val, 64)
any.err = err
return parsed
}
func (any *stringAny) ToString() string {
return any.val
}
func (any *stringAny) WriteTo(stream *Stream) {
stream.WriteString(any.val)
}
func (any *stringAny) GetInterface() interface{} {
return any.val
2017-01-23 02:33:43 +02:00
}