ErrUnexpectedEOF means that EOF was encountered in the middle of the input.
var ErrUnexpectedEOF = errors.New("%v", io.ErrUnexpectedEOF)
func TokenEquals(x, y Token) bool
TokenEquals returns true if given Tokens are equal, else false.
Decoder is a token-based JSON decoder.
type Decoder struct {
// contains filtered or unexported fields
}
func NewDecoder(b []byte) *Decoder
NewDecoder returns a Decoder to read the given []byte.
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 (d *Decoder) Peek() (Token, error)
Peek looks ahead and returns the next token kind without advancing a read.
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 (d *Decoder) Read() (Token, error)
Read returns the next JSON token. It will return an error if there is no valid token.
Encoder provides methods to write out JSON constructs and values. The user is responsible for producing valid sequences of JSON constructs and values.
type Encoder struct {
// contains filtered or unexported fields
}
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 (e *Encoder) Bytes() []byte
Bytes returns the content of the written bytes.
func (e *Encoder) EndArray()
EndArray writes out the ']' symbol.
func (e *Encoder) EndObject()
EndObject writes out the '}' symbol.
func (e *Encoder) StartArray()
StartArray writes out the '[' symbol.
func (e *Encoder) StartObject()
StartObject writes out the '{' symbol.
func (e *Encoder) WriteBool(b bool)
WriteBool writes out the given boolean value.
func (e *Encoder) WriteFloat(n float64, bitSize int)
WriteFloat writes out the given float and bitSize in JSON number value.
func (e *Encoder) WriteInt(n int64)
WriteInt writes out the given signed integer in JSON number value.
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 (e *Encoder) WriteNull()
WriteNull writes out the null value.
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 (e *Encoder) WriteUint(n uint64)
WriteUint writes out the given unsigned integer in JSON number value.
Kind represents a token kind expressible in the JSON format.
type Kind uint16
const ( Invalid Kind = (1 << iota) / 2 EOF Null Bool Number String Name ObjectOpen ObjectClose ArrayOpen ArrayClose )
func (k Kind) String() string
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.
type Token struct {
// contains filtered or unexported fields
}
func (t Token) Bool() bool
Bool returns the bool value if token kind is Bool, else it panics.
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 (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 (t Token) Kind() Kind
Kind returns the token kind.
func (t Token) Name() string
Name returns the object name if token is Name, else it panics.
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 (t Token) Pos() int
Pos returns the token position from the input.
func (t Token) RawString() string
RawString returns the read value in string.
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.