1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-20 18:48:32 +02:00

implement Any WriteTo

This commit is contained in:
Tao Wen 2017-01-26 14:56:31 +08:00
parent 9b587c0f22
commit 4d7e181f9f
10 changed files with 38 additions and 9 deletions

View File

@ -59,9 +59,6 @@ func (any *baseAny) SetObject(map[string]Any) bool {
return false
}
func (any *baseAny) WriteTo(stream *Stream) {
}
func WrapInt64(val int64) Any {
return &intAny{baseAny{}, nil, val}
}

View File

@ -95,7 +95,7 @@ func (any *arrayLazyAny) fillCache() {
}
func (any *arrayLazyAny) LastError() error {
return nil
return any.err
}
func (any *arrayLazyAny) ToBool() bool {
@ -156,7 +156,15 @@ func (any *arrayLazyAny) ToFloat64() float64 {
}
func (any *arrayLazyAny) ToString() string {
return ""
if len(any.remaining) == len(any.buf) {
// nothing has been parsed yet
return *(*string)(unsafe.Pointer(&any.buf))
} else {
any.fillCache()
str, err := MarshalToString(any.cache)
any.err = err
return str
}
}
func (any *arrayLazyAny) Get(path ...interface{}) Any {
@ -252,7 +260,7 @@ func (any *arrayLazyAny) SetArray(newList []Any) bool {
func (any *arrayLazyAny) WriteTo(stream *Stream) {
if len(any.remaining) == len(any.buf) {
// nothing has been parsed yet
stream.WriteRaw(*(*string)(unsafe.Pointer(&any.buf)))
stream.Write(any.buf)
} else {
any.fillCache()
stream.WriteVal(any.cache)

View File

@ -36,6 +36,10 @@ func (any *trueAny) ToString() string {
return "true"
}
func (any *trueAny) WriteTo(stream *Stream) {
stream.WriteTrue()
}
type falseAny struct {
baseAny
}
@ -71,3 +75,7 @@ func (any *falseAny) ToFloat64() float64 {
func (any *falseAny) ToString() string {
return "false"
}
func (any *falseAny) WriteTo(stream *Stream) {
stream.WriteFalse()
}

View File

@ -64,4 +64,8 @@ func (any *floatLazyAny) ToFloat64() float64 {
func (any *floatLazyAny) ToString() string {
return *(*string)(unsafe.Pointer(&any.buf))
}
func (any *floatLazyAny) WriteTo(stream *Stream) {
stream.Write(any.buf)
}

View File

@ -68,7 +68,7 @@ func (any *intLazyAny) ToString() string {
}
func (any *intLazyAny) WriteTo(stream *Stream) {
stream.WriteRaw(*(*string)(unsafe.Pointer(&any.buf)))
stream.Write(any.buf)
}
type intAny struct {

View File

@ -36,6 +36,9 @@ func (any *invalidAny) ToString() string {
return ""
}
func (any *invalidAny) WriteTo(stream *Stream) {
}
func (any *invalidAny) Get(path ...interface{}) Any {
return any
}

View File

@ -33,5 +33,9 @@ func (any *nilAny) ToFloat64() float64 {
}
func (any *nilAny) ToString() string {
return ""
return "nil"
}
func (any *nilAny) WriteTo(stream *Stream) {
stream.WriteNil()
}

View File

@ -277,7 +277,7 @@ func (any *objectLazyAny) SetObject(val map[string]Any) bool {
func (any *objectLazyAny) WriteTo(stream *Stream) {
if len(any.remaining) == len(any.buf) {
// nothing has been parsed yet
stream.WriteRaw(*(*string)(unsafe.Pointer(&any.buf)))
stream.Write(any.buf)
} else {
any.fillCache()
stream.WriteVal(any.cache)

View File

@ -96,4 +96,8 @@ func (any *stringLazyAny) ToFloat64() float64 {
func (any *stringLazyAny) ToString() string {
any.fillCache()
return any.cache
}
func (any *stringLazyAny) WriteTo(stream *Stream) {
stream.Write(any.buf)
}

View File

@ -87,6 +87,7 @@ func Test_array_lazy_any_get(t *testing.T) {
any, err := UnmarshalAnyFromString("[1,[2,3],4]")
should.Nil(err)
should.Equal(3, any.Get(1,1).ToInt())
should.Equal("[1,[2,3],4]", any.ToString())
}
func Test_array_lazy_any_set(t *testing.T) {