Source File
arena.go
Belonging Package
github.com/valyala/fastjson
package fastjson
import (
)
// Arena may be used for fast creation and re-use of Values.
//
// Typical Arena lifecycle:
//
// 1) Construct Values via the Arena and Value.Set* calls.
// 2) Marshal the constructed Values with Value.MarshalTo call.
// 3) Reset all the constructed Values at once by Arena.Reset call.
// 4) Go to 1 and re-use the Arena.
//
// It is unsafe calling Arena methods from concurrent goroutines.
// Use per-goroutine Arenas or ArenaPool instead.
type Arena struct {
b []byte
c cache
}
// Reset resets all the Values allocated by a.
//
// Values previously allocated by a cannot be used after the Reset call.
func ( *Arena) () {
.b = .b[:0]
.c.reset()
}
// NewObject returns new empty object value.
//
// New entries may be added to the returned object via Set call.
//
// The returned object is valid until Reset is called on a.
func ( *Arena) () *Value {
:= .c.getValue()
.t = TypeObject
.o.reset()
return
}
// NewArray returns new empty array value.
//
// New entries may be added to the returned array via Set* calls.
//
// The returned array is valid until Reset is called on a.
func ( *Arena) () *Value {
:= .c.getValue()
.t = TypeArray
.a = .a[:0]
return
}
// NewString returns new string value containing s.
//
// The returned string is valid until Reset is called on a.
func ( *Arena) ( string) *Value {
:= .c.getValue()
.t = typeRawString
:= len(.b)
.b = escapeString(.b, )
.s = b2s(.b[+1 : len(.b)-1])
return
}
// NewStringBytes returns new string value containing b.
//
// The returned string is valid until Reset is called on a.
func ( *Arena) ( []byte) *Value {
:= .c.getValue()
.t = typeRawString
:= len(.b)
.b = escapeString(.b, b2s())
.s = b2s(.b[+1 : len(.b)-1])
return
}
// NewNumberFloat64 returns new number value containing f.
//
// The returned number is valid until Reset is called on a.
func ( *Arena) ( float64) *Value {
:= .c.getValue()
.t = TypeNumber
:= len(.b)
.b = strconv.AppendFloat(.b, , 'g', -1, 64)
.s = b2s(.b[:])
return
}
// NewNumberInt returns new number value containing n.
//
// The returned number is valid until Reset is called on a.
func ( *Arena) ( int) *Value {
:= .c.getValue()
.t = TypeNumber
:= len(.b)
.b = strconv.AppendInt(.b, int64(), 10)
.s = b2s(.b[:])
return
}
// NewNumberString returns new number value containing s.
//
// The returned number is valid until Reset is called on a.
func ( *Arena) ( string) *Value {
:= .c.getValue()
.t = TypeNumber
.s =
return
}
// NewNull returns null value.
func ( *Arena) () *Value {
return valueNull
}
// NewTrue returns true value.
func ( *Arena) () *Value {
return valueTrue
}
// NewFalse return false value.
func ( *Arena) () *Value {
return valueFalse
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)