codec

package
v0.0.0-...-67c52db Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseNumber

func ParseNumber(input []byte) (int, bool)

ParseNumber reads the given []byte for a valid JSON number. If it is valid, it returns the number of bytes. Parsing logic follows the definition in https://tools.ietf.org/html/rfc7159#section-6, and is based off encoding/json.isValidNumber function.

func TokenEquals

func TokenEquals(x, y Token) bool

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

Types

type Decoder

type Decoder interface {
	Peek() (Token, error)
	Read() (Token, error)
	Position(idx int) (line int, column int)
	Clone() Decoder
}

type Encoder

type Encoder interface {
	Bytes() []byte
	WriteNull()
	WriteBool(b bool)
	WriteString(s string) error
	WriteFloat(n float64, bitSize int)
	WriteInt(n int64)
	WriteUint(n uint64)
	StartObject()
	EndObject()
	WriteName(s string) error
	StartArray()
	EndArray()
}

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

	// Comma is only for parsing in between tokens and
	// does not need to be exported.
	Comma
)

func (Kind) String

func (k Kind) String() string

type NewDecoderFun

type NewDecoderFun func(b []byte) Decoder
var NewDecoder NewDecoderFun

type NewEncoderFun

type NewEncoderFun func(indent string) (Encoder, error)
var NewEncoder NewEncoderFun

type Token

type Token struct {
	// Token Kind.
	Kind Kind
	// Pos provides the position of the token in the original input.
	Pos int
	// Raw bytes of the serialized token.
	// This is a subslice into the original input.
	Raw []byte
	// Boo is parsed boolean value.
	Boo bool
	// Str is parsed string value.
	Str 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.

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