json

package
v2.0.0-...-d24bc72 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2019 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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

Peek looks ahead and returns the next JSON type without advancing a read.

func (*Decoder) Read

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

Read returns the next JSON value. It will return an error if there is no valid value. For String types containing invalid UTF8 characters, a non-fatal error is returned and caller can call Read for the next value.

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(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 ':'.

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.

func (*Encoder) WriteUint

func (e *Encoder) WriteUint(n uint64)

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

type Type

type Type uint

Type represents a type expressible in the JSON format.

const (
	Invalid Type = (1 << iota) / 2
	EOF
	Null
	Bool
	Number
	String
	StartObject
	EndObject
	Name
	StartArray
	EndArray
)

func (Type) String

func (t Type) String() string

type Value

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

Value contains a JSON type and value parsed from calling Decoder.Read. For JSON boolean and string, it holds the converted value in boo and str fields respectively. For JSON number, input field holds a valid number which is converted only in Int or Float. Other JSON types do not require any additional data.

func (Value) Bool

func (v Value) Bool() (bool, error)

Bool returns the bool value if token is Bool, else it will return an error.

func (Value) Float

func (v Value) Float(bitSize int) (float64, error)

Float returns the floating-point number if token is Number, else it will return an error.

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 an error if the number exceeds the floating point limits for given bitSize.

func (Value) Int

func (v Value) Int(bitSize int) (int64, error)

Int returns the signed integer number if token is Number, else it will return an error.

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

func (Value) Name

func (v Value) Name() (string, error)

Name returns the object name if token is Name, else it will return an error.

func (Value) Position

func (v Value) Position() (int, int)

Position returns the line and column of the value.

func (Value) Raw

func (v Value) Raw() string

Raw returns the read value in string.

func (Value) String

func (v Value) String() string

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

func (Value) Type

func (v Value) Type() Type

Type returns the JSON type.

func (Value) Uint

func (v Value) Uint(bitSize int) (uint64, error)

Uint returns the signed integer number if token is Number, else it will return an error.

The given bitSize specifies the unsigned integer type that the result must fit into. It returns an error 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