Documentation ¶
Overview ¶
Package fastjson provides a library for fast JSON encoding, optimised for static code generation.
Fastjson functions and interfaces are structured such that all encoding appends to a buffer, enabling buffer reuse without forcing specific mechanisms such as sync.Pool. This enables zero-allocation encoding without incurring any concurrency overhead in certain applications.
Index ¶
- func Marshal(w *Writer, v interface{}) error
- type Appender
- type Marshaler
- type Writer
- func (w *Writer) Bool(v bool)
- func (w *Writer) Bytes() []byte
- func (w *Writer) Float32(n float32)
- func (w *Writer) Float64(n float64)
- func (w *Writer) Int64(n int64)
- func (w *Writer) RawByte(c byte)
- func (w *Writer) RawBytes(data []byte)
- func (w *Writer) RawString(s string)
- func (w *Writer) Reset()
- func (w *Writer) Rewind(size int)
- func (w *Writer) Size() int
- func (w *Writer) String(s string)
- func (w *Writer) StringContents(s string)
- func (w *Writer) Time(t time.Time, layout string)
- func (w *Writer) Uint64(n uint64)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal marshals v as JSON to w.
For all basic types, Marshal uses w's methods to marshal the values directly. If v implements Marshaler, its MarshalFastJSON method will be used; if v implements Appender, its AppendJSON method will be used, and it is assumed to append valid JSON. As a final resort, we use json.Marshal.
Where json.Marshal is used internally (see above), errors or panics produced by json.Marshal will be encoded as JSON objects, with special keys "__ERROR__" for errors, and "__PANIC__" for panics. e.g. if json.Marshal panics due to a broken json.Marshaler implementation or assumption, then Marshal will encode the panic as
{"__PANIC__": "panic calling MarshalJSON for type Foo: reason"}
Marshal returns the first error encountered.
Types ¶
type Appender ¶
type Appender interface { // AppendJSON appends the JSON representation of the value to the // buffer, and returns the extended buffer. // // AppendJSON is required not to panic or fail. AppendJSON([]byte) []byte }
Appender defines an interface that types can implement to append their JSON representation to a buffer.
type Marshaler ¶
type Marshaler interface { // MarshalFastJSON writes a JSON representation of the type to w. // // MarshalFastJSON is expected to suppress any panics. Depending // on the application, it may be expected that MarshalFastJSON // writes valid JSON to w, even in error cases. // // The returned error will be propagated up through to callers of // fastjson.Marshal. MarshalFastJSON(w *Writer) error }
Marshaler defines an interface that types can implement to provide fast JSON marshaling.
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
Writer is a JSON writer, appending to an internal buffer.
Writer is not safe for concurrent use. A Writer can be reset and reused, which will reuse the underlying buffer.
func (*Writer) Bytes ¶
Bytes returns the internal buffer. The result is invalidated when Reset is called.
func (*Writer) Rewind ¶
Rewind rewinds the buffer such that it has size bytes, dropping everything proceeding.
func (*Writer) Size ¶
Size returns the current size of the buffer. Size is typically used in conjunction with Rewind, to mark a position to which the writer may later be rewound.
func (*Writer) StringContents ¶
StringContents is the same as String, but without the surrounding quotes.