type EncodeOptions … type Encoder … type encodeBuffer … // NewEncoder constructs a new streaming encoder writing to w. func NewEncoder(w io.Writer) *Encoder { … } // NewEncoder constructs a new streaming encoder writing to w // configured with the provided options. // It flushes the internal buffer when the buffer is sufficiently full or // when a top-level value has been written. // // If w is a bytes.Buffer, then the encoder appends directly into the buffer // without copying the contents from an intermediate buffer. func (o EncodeOptions) NewEncoder(w io.Writer) *Encoder { … } // ResetEncoder resets an encoder such that it is writing afresh to w and // configured with the provided options. func (o EncodeOptions) ResetEncoder(e *Encoder, w io.Writer) { … } func (e *Encoder) reset(b []byte, w io.Writer, o EncodeOptions) { … } // Reset resets an encoder such that it is writing afresh to w but // keeps any pre-existing encoder options. func (e *Encoder) Reset(w io.Writer) { … } // needFlush determines whether to flush at this point. func (e *Encoder) needFlush() bool { … } // flush flushes the buffer to the underlying io.Writer. // It may append a trailing newline after the top-level value. func (e *Encoder) flush() error { … } func (e *encodeBuffer) previousOffsetEnd() int64 { … } func (e *encodeBuffer) unflushedBuffer() []byte { … } // avoidFlush indicates whether to avoid flushing to ensure there is always // enough in the buffer to unwrite the last object member if it were empty. func (e *Encoder) avoidFlush() bool { … } // unwriteEmptyObjectMember unwrites the last object member if it is empty // and reports whether it performed an unwrite operation. func (e *Encoder) unwriteEmptyObjectMember(prevName *string) bool { … } // unwriteOnlyObjectMemberName unwrites the only object member name // and returns the unquoted name. func (e *Encoder) unwriteOnlyObjectMemberName() string { … } func trimSuffixWhitespace(b []byte) []byte { … } func trimSuffixString(b []byte) []byte { … } func hasSuffixByte(b []byte, c byte) bool { … } func trimSuffixByte(b []byte, c byte) []byte { … } // WriteToken writes the next token and advances the internal write offset. // // The provided token kind must be consistent with the JSON grammar. // For example, it is an error to provide a number when the encoder // is expecting an object name (which is always a string), or // to provide an end object delimiter when the encoder is finishing an array. // If the provided token is invalid, then it reports a SyntacticError and // the internal state remains unchanged. func (e *Encoder) WriteToken(t Token) error { … } const rawIntNumber … const rawUintNumber … // writeNumber is specialized version of WriteToken, but optimized for numbers. // As a special-case, if bits is -1 or -2, it will treat v as // the raw-encoded bits of an int64 or uint64, respectively. // It is only called from arshal_default.go. func (e *Encoder) writeNumber(v float64, bits int, quote bool) error { … } // WriteValue writes the next raw value and advances the internal write offset. // The Encoder does not simply copy the provided value verbatim, but // parses it to ensure that it is syntactically valid and reformats it // according to how the Encoder is configured to format whitespace and strings. // // The provided value kind must be consistent with the JSON grammar // (see examples on Encoder.WriteToken). If the provided value is invalid, // then it reports a SyntacticError and the internal state remains unchanged. func (e *Encoder) WriteValue(v RawValue) error { … } // appendWhitespace appends whitespace that immediately precedes the next token. func (e *Encoder) appendWhitespace(b []byte, next Kind) []byte { … } // appendIndent appends the appropriate number of indentation characters // for the current nested level, n. func (e *Encoder) appendIndent(b []byte, n int) []byte { … } // reformatValue parses a JSON value from the start of src and // appends it to the end of dst, reformatting whitespace and strings as needed. // It returns the updated versions of dst and src. func (e *Encoder) reformatValue(dst []byte, src RawValue, depth int) ([]byte, RawValue, error) { … } // reformatObject parses a JSON object from the start of src and // appends it to the end of src, reformatting whitespace and strings as needed. // It returns the updated versions of dst and src. func (e *Encoder) reformatObject(dst []byte, src RawValue, depth int) ([]byte, RawValue, error) { … } // reformatArray parses a JSON array from the start of src and // appends it to the end of dst, reformatting whitespace and strings as needed. // It returns the updated versions of dst and src. func (e *Encoder) reformatArray(dst []byte, src RawValue, depth int) ([]byte, RawValue, error) { … } // OutputOffset returns the current output byte offset. It gives the location // of the next byte immediately after the most recently written token or value. // The number of bytes actually written to the underlying io.Writer may be less // than this offset due to internal buffering effects. func (e *Encoder) OutputOffset() int64 { … } // UnusedBuffer returns a zero-length buffer with a possible non-zero capacity. // This buffer is intended to be used to populate a RawValue // being passed to an immediately succeeding WriteValue call. // // Example usage: // // b := d.UnusedBuffer() // b = append(b, '"') // b = appendString(b, v) // append the string formatting of v // b = append(b, '"') // ... := d.WriteValue(b) // // It is the user's responsibility to ensure that the value is valid JSON. func (e *Encoder) UnusedBuffer() []byte { … } // StackDepth returns the depth of the state machine for written JSON data. // Each level on the stack represents a nested JSON object or array. // It is incremented whenever an ObjectStart or ArrayStart token is encountered // and decremented whenever an ObjectEnd or ArrayEnd token is encountered. // The depth is zero-indexed, where zero represents the top-level JSON value. func (e *Encoder) StackDepth() int { … } // StackIndex returns information about the specified stack level. // It must be a number between 0 and StackDepth, inclusive. // For each level, it reports the kind: // // - 0 for a level of zero, // - '{' for a level representing a JSON object, and // - '[' for a level representing a JSON array. // // It also reports the length of that JSON object or array. // Each name and value in a JSON object is counted separately, // so the effective number of members would be half the length. // A complete JSON object must have an even length. func (e *Encoder) StackIndex(i int) (Kind, int) { … } // StackPointer returns a JSON Pointer (RFC 6901) to the most recently written value. // Object names are only present if AllowDuplicateNames is false, otherwise // object members are represented using their index within the object. func (e *Encoder) StackPointer() string { … } // appendString appends src to dst as a JSON string per RFC 7159, section 7. // // If validateUTF8 is specified, this rejects input that contains invalid UTF-8 // otherwise invalid bytes are replaced with the Unicode replacement character. // If escapeRune is provided, it specifies which runes to escape using // hexadecimal sequences. If nil, the shortest representable form is used, // which is also the canonical form for strings (RFC 8785, section 3.2.2.2). // // Note that this API allows full control over the formatting of strings // except for whether a forward solidus '/' may be formatted as '\/' and // the casing of hexadecimal Unicode escape sequences. func appendString(dst []byte, src string, validateUTF8 bool, escapeRune func(rune) bool) ([]byte, error) { … } // reformatString consumes a JSON string from src and appends it to dst, // reformatting it if necessary for the given escapeRune parameter. // It returns the appended output and the remainder of the input. func reformatString(dst, src []byte, validateUTF8, preserveRaw bool, escapeRune func(rune) bool) ([]byte, []byte, error) { … } // appendNumber appends src to dst as a JSON number per RFC 7159, section 6. // It formats numbers similar to the ES6 number-to-string conversion. // See https://go.dev/issue/14135. // // The output is identical to ECMA-262, 6th edition, section 7.1.12.1 and with // RFC 8785, section 3.2.2.3 for 64-bit floating-point numbers except for -0, // which is formatted as -0 instead of just 0. // // For 32-bit floating-point numbers, // the output is a 32-bit equivalent of the algorithm. // Note that ECMA-262 specifies no algorithm for 32-bit numbers. func appendNumber(dst []byte, src float64, bits int) []byte { … } // reformatNumber consumes a JSON string from src and appends it to dst, // canonicalizing it if specified. // It returns the appended output and the remainder of the input. func reformatNumber(dst, src []byte, canonicalize bool) ([]byte, []byte, error) { … } // appendHexUint16 appends src to dst as a 4-byte hexadecimal number. func appendHexUint16(dst []byte, src uint16) []byte { … }