json

package
v0.0.0-...-bb96f36 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedEOF = errors.New("%v", io.ErrUnexpectedEOF)

ErrUnexpectedEOF means that EOF was encountered in the middle of the input.

Functions

func TokenEquals

func TokenEquals(x, y Token) bool

TokenEquals returns true if given Tokens are equal, else false.

Types

type Decoder

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

Decoder is a token-based JSON decoder.

func NewDecoder

func NewDecoder(b []byte) *Decoder

NewDecoder returns a Decoder to read the given []byte.

func (*Decoder) Clone

func (d *Decoder) Clone() *Decoder

Clone returns a copy of the Decoder for use in reading ahead the next JSON object, array or other values without affecting current Decoder.

func (*Decoder) Peek

func (d *Decoder) Peek() (Token, error)

Peek looks ahead and returns the next token kind without advancing a read.

func (*Decoder) Position

func (d *Decoder) Position(idx int) (line int, column int)

Position returns line and column number of given index of the original input. It will panic if index is out of range.

func (*Decoder) Read

func (d *Decoder) Read() (Token, error)

Read returns the next JSON token. It will return an error if there is no valid token.

type Encoder

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

Encoder provides methods to write out JSON constructs and values. The user is responsible for producing valid sequences of JSON constructs and values.

func NewEncoder

func NewEncoder(buf []byte, indent string) (*Encoder, error)

NewEncoder returns an Encoder.

If indent is a non-empty string, it causes every entry for an Array or Object to be preceded by the indent and trailed by a newline.

func (*Encoder) Bytes

func (e *Encoder) Bytes() []byte

Bytes returns the content of the written bytes.

func (*Encoder) EndArray

func (e *Encoder) EndArray()

EndArray writes out the ']' symbol.

func (*Encoder) EndObject

func (e *Encoder) EndObject()

EndObject writes out the '}' symbol.

func (*Encoder) StartArray

func (e *Encoder) StartArray()

StartArray writes out the '[' symbol.

func (*Encoder) StartObject

func (e *Encoder) StartObject()

StartObject writes out the '{' symbol.

func (*Encoder) WriteBool

func (e *Encoder) WriteBool(b bool)

WriteBool writes out the given boolean value.

func (*Encoder) WriteFloat

func (e *Encoder) WriteFloat(n float64, bitSize int)

WriteFloat writes out the given float and bitSize in JSON number value.

func (*Encoder) WriteInt

func (e *Encoder) WriteInt(n int64)

WriteInt writes out the given signed integer in JSON number value.

func (*Encoder) WriteName

func (e *Encoder) WriteName(s string) error

WriteName writes out the given string in JSON string value and the name separator ':'. Returns error if input string contains invalid UTF-8, which should not be likely as protobuf field names should be valid.

func (*Encoder) WriteNull

func (e *Encoder) WriteNull()

WriteNull writes out the null value.

func (*Encoder) WriteString

func (e *Encoder) WriteString(s string) error

WriteString writes out the given string in JSON string value. Returns error if input string contains invalid UTF-8.

func (*Encoder) WriteUint

func (e *Encoder) WriteUint(n uint64)

WriteUint writes out the given unsigned integer in JSON number value.

type Kind

type Kind uint16

Kind represents a token kind expressible in the JSON format.

const (
	Invalid Kind = (1 << iota) / 2
	EOF
	Null
	Bool
	Number
	String
	Name
	ObjectOpen
	ObjectClose
	ArrayOpen
	ArrayClose
)

func (Kind) String

func (k Kind) String() string

type Token

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

Token provides a parsed token kind and value.

Values are provided by the difference accessor methods. The accessor methods Name, Bool, and ParsedString will panic if called on the wrong kind. There are different accessor methods for the Number kind for converting to the appropriate Go numeric type and those methods have the ok return value.

func (Token) Bool

func (t Token) Bool() bool

Bool returns the bool value if token kind is Bool, else it panics.

func (Token) Float

func (t Token) Float(bitSize int) (float64, bool)

Float returns the floating-point number if token kind is Number.

The floating-point precision is specified by the bitSize parameter: 32 for float32 or 64 for float64. If bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value. It will return false if the number exceeds the floating point limits for given bitSize.

func (Token) Int

func (t Token) Int(bitSize int) (int64, bool)

Int returns the signed integer number if token is Number.

The given bitSize specifies the integer type that the result must fit into. It returns false if the number is not an integer value or if the result exceeds the limits for given bitSize.

func (Token) Kind

func (t Token) Kind() Kind

Kind returns the token kind.

func (Token) Name

func (t Token) Name() string

Name returns the object name if token is Name, else it panics.

func (Token) ParsedString

func (t Token) ParsedString() string

ParsedString returns the string value for a JSON string token or the read value in string if token is not a string.

func (Token) Pos

func (t Token) Pos() int

Pos returns the token position from the input.

func (Token) RawString

func (t Token) RawString() string

RawString returns the read value in string.

func (Token) Uint

func (t Token) Uint(bitSize int) (uint64, bool)

Uint returns the signed integer number if token is Number.

The given bitSize specifies the unsigned integer type that the result must fit into. It returns false if the number is not an unsigned integer value or if the result exceeds the limits for given bitSize.

Jump to

Keyboard shortcuts

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