jsonw

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 20, 2026 License: MIT Imports: 10 Imported by: 0

README

JSONW - yet another json writer
-------------------------------

This is a small library for producing JSON payloads.
See the Go API documentation at https://pkg.go.dev/github.com/fjl/jsonw

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendQuoted

func AppendQuoted(buf []byte, str []byte) []byte

AppendQuoted encodes a JSON string, appending it to `b`.

func AppendQuotedString

func AppendQuotedString(b []byte, str string) []byte

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 NewBuffer

func NewBuffer(cap int) *Buffer

NewBuffer creates a buffer with the given capacity.

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) BigInt

func (b *Buffer) BigInt(v *big.Int)

BigInt appends a decimal bigint.

func (*Buffer) Bool

func (b *Buffer) Bool(v bool)

Bool appends a JSON boolean.

func (*Buffer) Float64

func (b *Buffer) Float64(v 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

func (b *Buffer) HexBigInt(v *big.Int)

HexBigInt appends a bigint as a hex-encoded string. Negative values are written as "-0x...".

func (*Buffer) HexBytes

func (b *Buffer) HexBytes(v []byte)

HexBytes appends a hex-string as bytes.

func (*Buffer) HexUint64

func (b *Buffer) HexUint64(v uint64)

HexUint64 appends an integer as a hex-encoded string.

func (*Buffer) Int64

func (b *Buffer) Int64(v int64)

Int64 appends a decimal integer.

func (*Buffer) Key

func (b *Buffer) Key(s string)

Key writes an object key. This must be called in between writing object values.

func (*Buffer) MustValue

func (b *Buffer) MustValue(v any)

MustValue appends an arbitrary value marshaled by encoding/json. This panics if marshaling fails.

func (*Buffer) Null

func (b *Buffer) Null()

Null appends a JSON null.

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

func (b *Buffer) Output() []byte

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.

func (*Buffer) RawValue

func (b *Buffer) RawValue(v []byte)

RawValue appends a pre-encoded value. Note there are no validity checks on the value, except that it must not be empty.

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) String

func (b *Buffer) String(s string)

String appends a JSON string value.

func (*Buffer) Uint64

func (b *Buffer) Uint64(v uint64)

Uint64 appends a decimal integer.

func (*Buffer) Value

func (b *Buffer) Value(v any) error

Value appends an arbitrary value marshaled by encoding/json.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL