Documentation
¶
Overview ¶
Package jsonw implements a JSON encoder.
To create a JSON output, create a Buffer and call methods on it.
Example ¶
package main
import (
"fmt"
"github.com/fjl/jsonw"
)
func main() {
// A zero value buffer is ready to use.
var b jsonw.Buffer
// Encode an object to the buffer.
b.Object(func() {
b.Key("a")
b.Uint64(67)
b.Key("b")
b.Array(func() {
b.String("hello")
})
})
fmt.Println(string(b.Output()))
}
Output: {"a":67,"b":["hello"]}
Index ¶
- func AppendQuoted(buf []byte, str []byte) []byte
- func AppendQuotedString(b []byte, str string) []byte
- type Buffer
- func (b *Buffer) Array(fn func())
- func (b *Buffer) BigInt(v *big.Int)
- func (b *Buffer) Bool(v bool)
- func (b *Buffer) Float64(v float64)
- func (b *Buffer) HexBigInt(v *big.Int)
- func (b *Buffer) HexBytes(v []byte)
- func (b *Buffer) HexUint64(v uint64)
- func (b *Buffer) Int64(v int64)
- func (b *Buffer) Key(s string)
- func (b *Buffer) MustValue(v any)
- func (b *Buffer) Null()
- func (b *Buffer) Object(fn func())
- func (b *Buffer) Output() []byte
- func (b *Buffer) RawValue(v []byte)
- func (b *Buffer) Reset()
- func (b *Buffer) String(s string)
- func (b *Buffer) Uint64(v uint64)
- func (b *Buffer) Value(v any) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AppendQuoted ¶
AppendQuoted encodes a JSON string, appending it to `b`.
func AppendQuotedString ¶
AppendQuotedString encodes a JSON string, appending it to `b`.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a JSON encoder buffer.
func (*Buffer) Array ¶
func (b *Buffer) Array(fn func())
Array writes a JSON array. It invokes the given function to write the array elements.
func (*Buffer) Float64 ¶
Float64 appends a float64.
NaN and ±Inf have no representation in standard JSON. They are written as "NaN", "Infinity", "-Infinity" (like JSON5). The caller must handle these values explicitly.
func (*Buffer) HexBigInt ¶
HexBigInt appends a bigint as a hex-encoded string. Negative values are written as "-0x...".
func (*Buffer) Key ¶
Key writes an object key. This must be called in between writing object values.
func (*Buffer) MustValue ¶
MustValue appends an arbitrary value marshaled by encoding/json. This panics if marshaling fails.
func (*Buffer) Object ¶
func (b *Buffer) Object(fn func())
Object writes an object. It invokes the given function to write the keys and values of the object.
func (*Buffer) Output ¶
Output returns the written JSON bytes.
This can only be called at the top-level encoding context. Using Output from within a call to Array or Object will panic.
The return value aliases the internal buffer, and may change content after Reset and/or future calls to encoder methods.