Top.Mail.Ru
package json — KubFly SDK for Go

KubFly SDK for Go / codec/json

package json

import "kubfly.io/sdk/codec/json"

Package json is a streaming parser of JSON (RFC 8259), strict and without a tree — io.kubfly.sdk.codec.json of the Java SDK: Next reads the document event by event, a reader takes what it needs and skips the rest.

parser, err := json.NewParser(body, codec.DefaultLimits)
event, err := parser.Next() // json.EventStartObject
for event, err = parser.Next(); err == nil && event == json.EventName; event, err = parser.Next() {
	switch parser.String() {
	case "errors":
		event, err = parser.Next()
		errors = event == json.EventTrue
	default:
		err = parser.Skip()
	}
}

Strict as RFC 8259 says, no more and no less: one value of any kind; white space only of space, tab, line feed and carriage return; no comments, no trailing commas, no byte order mark; strings in double quotes, their control characters escaped, the escapes of JSON only, a surrogate escape only in its pair; numbers without a sign +, leading zeros, NaN or Infinity; the names of an object not repeated. The text is UTF-8, checked strictly. Every failure — not JSON, beyond the codec.Limits — is a *codec.Error with the offset, the same as in every SDK (sdk/spec/codec/json.tsv), and every call after it gives it again. Not safe for concurrent use.

Constants

const MaxNumber = 26

MaxNumber is the most bytes of a number: an int64, a uint64 or a float64.

Functions

func WriteBase64

func WriteBase64(buffer []byte, offset int, value []byte) int

WriteBase64 writes bytes as a string of Base64 (RFC 4648, with its padding).

func WriteBool

func WriteBool(buffer []byte, offset int, value bool) int

WriteBool writes a boolean.

func WriteByte

func WriteByte(buffer []byte, offset int, value byte) int

WriteByte writes a byte of the structure as it is: { } [ ] , : or a line break. The generator — WriteByte and the Write… after it, io.kubfly.sdk.codec.json.JsonGenerator of the Java SDK — writes every value straight into a buffer at an offset and gives the offset after it, nothing allocated; a nil buffer writes nothing and counts the bytes, so the same function measures and writes:

size := sample(nil, 0, name, count)
line := make([]byte, size)
sample(line, 0, name, count)

The text is the canonical JSON of KubFly (sdk/spec/codec/json-generator.tsv, the same in every SDK): UTF-8, each byte that is not UTF-8 written as U+FFFD; in a string the quote, the backslash, \n, \r and \t escaped as such, the other control characters as \u00xx (small letters), the rest as it is — the slash and DEL too. A float64 is written as Double.toString of Java writes it — 10.0, 0.001, 1.0E7 — and NaN and the infinities, which JSON has no numbers for, as the strings "NaN", "Infinity" and "-Infinity". A buffer too small panics, as every slice does: measure first, or write into a Buffer, which grows.

func WriteFloat64

func WriteFloat64(buffer []byte, offset int, value float64) int

WriteFloat64 writes a float as Double.toString of Java does (JDK 19 and later): the shortest digits that read back, two when one would do and two are closer; plain from 10^-3 to 10^7, scientific otherwise; NaN and the infinities as strings.

func WriteInt64

func WriteInt64(buffer []byte, offset int, value int64) int

WriteInt64 writes an integer.

func WriteName

func WriteName(buffer []byte, offset int, name string) int

WriteName writes the name of a member and its colon: "name":.

func WriteNull

func WriteNull(buffer []byte, offset int) int

WriteNull writes null.

func WriteString

func WriteString(buffer []byte, offset int, value string) int

WriteString writes a string in quotes with the escapes of the canonical JSON.

func WriteTime

func WriteTime(buffer []byte, offset int, millis int64) int

WriteTime writes a time as a string of ISO-8601 in UTC, as Instant.toString of Java does: the seconds always, the milliseconds when there are some (2026-01-01T00:00:00Z, 2026-01-01T00:00:00.120Z); a year past 9999 with +, one before 0 with -.

func WriteUTF8

func WriteUTF8(buffer []byte, offset int, value []byte) int

WriteUTF8 writes bytes of UTF-8 as a string, as WriteString writes one — writeUtf8 of the Java generator: a text read from the body of a record is written without becoming a string; a byte that is not UTF-8 becomes U+FFFD.

func WriteUUID

func WriteUUID(buffer []byte, offset int, value uuid.UUID) int

WriteUUID writes a UUID as a string, in small letters.

func WriteUint64

func WriteUint64(buffer []byte, offset int, value uint64) int

WriteUint64 writes an unsigned integer: the 64 bits of a span of another system.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a JSON document written with the generator into a buffer of its own that grows — JsonBuffer of the Java SDK: the commas and the colons in their places, the structure checked as it goes (a value without its name, a name outside an object, a close of what is not open panic: they are faults of the code that writes). Kept and Reset, it writes one document after another without allocating — the buffer grows to the largest of them and stays. Several documents in a row at the top, each ended by Newline, are JSON lines. Not safe for concurrent use: a buffer per goroutine. The zero value is ready.

var json json.Buffer
json.StartObject()
json.Name("name")
json.Text("orders")
json.EndObject()
json.Newline()
out.Write(json.Bytes())
json.Reset()

func (*Buffer) Base64

func (b *Buffer) Base64(value []byte)

Base64 writes bytes as a string of Base64.

func (*Buffer) Bool

func (b *Buffer) Bool(value bool)

Bool writes a boolean.

func (*Buffer) Bytes

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

Bytes are the bytes written, valid until the next write.

func (*Buffer) EndArray

func (b *Buffer) EndArray()

EndArray closes the array open.

func (*Buffer) EndObject

func (b *Buffer) EndObject()

EndObject closes the object open.

func (*Buffer) Float64

func (b *Buffer) Float64(value float64)

Float64 writes a float as Double.toString of Java does; NaN and the infinities as strings.

func (*Buffer) Int64

func (b *Buffer) Int64(value int64)

Int64 writes an integer.

func (*Buffer) Len

func (b *Buffer) Len() int

Len is the number of bytes written.

func (*Buffer) Name

func (b *Buffer) Name(name string)

Name writes the name of the next member of the object open, after a comma when it is not the first.

func (*Buffer) Newline

func (b *Buffer) Newline()

Newline writes a line break after a document at the top: JSON lines.

func (*Buffer) Null

func (b *Buffer) Null()

Null writes null.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset forgets what was written, keeps the buffer.

func (*Buffer) StartArray

func (b *Buffer) StartArray()

StartArray opens an array.

func (*Buffer) StartObject

func (b *Buffer) StartObject()

StartObject opens an object.

func (*Buffer) String

func (b *Buffer) String() string

String is the text written.

func (*Buffer) Text

func (b *Buffer) Text(value string)

Text writes a string.

func (*Buffer) Time

func (b *Buffer) Time(millis int64)

Time writes a time, milliseconds of the epoch, as a string of ISO-8601 in UTC (WriteTime).

func (*Buffer) UTF8

func (b *Buffer) UTF8(value []byte)

UTF8 writes bytes of UTF-8 as a string, without making one (WriteUTF8).

func (*Buffer) UUID

func (b *Buffer) UUID(value uuid.UUID)

UUID writes a UUID as a string.

func (*Buffer) Uint64

func (b *Buffer) Uint64(value uint64)

Uint64 writes an unsigned integer.

type Event

type Event uint8

Event is what Next read.

const (
	// EventNone is before the first event.
	EventNone Event = iota
	// EventStartObject is the beginning of an object: its members follow, each an EventName and a value.
	EventStartObject
	// EventEndObject is the end of an object.
	EventEndObject
	// EventStartArray is the beginning of an array: its values follow.
	EventStartArray
	// EventEndArray is the end of an array.
	EventEndArray
	// EventName is the name of a member of an object, String: its value follows.
	EventName
	// EventString is a string, String.
	EventString
	// EventNumber is a number: Number, Int64, Float64.
	EventNumber
	// EventTrue is true.
	EventTrue
	// EventFalse is false.
	EventFalse
	// EventNull is null.
	EventNull
	// EventEndDocument is after the value of the document: nothing more is read.
	EventEndDocument
)

The events.

func (Event) String

func (e Event) String() string

String is the name of the event, as JsonEvent of the Java SDK: START_OBJECT … END_DOCUMENT.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser is a parser of one document.

func NewParser

func NewParser(document []byte, limits codec.Limits) (*Parser, error)

NewParser is a parser of a document, before its first event: an error if the document is longer than the limit. The document is UTF-8; it is not copied and is not to be changed while it is read. Limits that are not positive are a programming error, a panic.

func (*Parser) Depth

func (p *Parser) Depth() int

Depth is the depth of the event read last: of a start, with its object or array (that of the document is 1); of an end, without it.

func (*Parser) Event

func (p *Parser) Event() Event

Event is the event read last: EventNone before the first.

func (*Parser) Float64

func (p *Parser) Float64() float64

Float64 is the EventNumber read last as a float64: the nearest one, an infinity beyond its range.

func (*Parser) Int64

func (p *Parser) Int64() (int64, error)

Int64 is the EventNumber read last as an integer of 64 bits; an error if it is written with a fraction or an exponent, or is beyond 64 bits.

func (*Parser) Integer

func (p *Parser) Integer() bool

Integer tells whether the EventNumber read last is written as an integer: without a fraction and an exponent.

func (*Parser) Next

func (p *Parser) Next() (event Event, err error)

Next reads the next event: those of the value of the document, then EventEndDocument, given again on every call after it. An error if the document is not JSON or is beyond the limits.

func (*Parser) Number

func (p *Parser) Number() string

Number is the EventNumber read last, as the document writes it: exact, for big numbers.

func (*Parser) Offset

func (p *Parser) Offset() int64

Offset is the offset of the event read last: its first byte.

func (*Parser) Skip

func (p *Parser) Skip() error

Skip reads past a value: at an EventName, the value of its member; at a start, its object or array up to its end; at anything else, nothing. An error if the document fails.

func (*Parser) String

func (p *Parser) String() string

String is the text of the EventName or the EventString read last, escapes resolved.