jsonx

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 8 Imported by: 1

README

jsonx

A simple library that offers some tooling for working with JSON in go.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Null  = RawMessage("null")
	True  = RawMessage("true")
	False = RawMessage("false")
)

Functions

func EncodeAndWriteString added in v0.0.4

func EncodeAndWriteString[S ~string](w Writer, s S)

EncodeAndWriteString encodes a string to a json string literal, escaping any characters that are not permitted. It then writes the result to w.

Logic taken from encoding/json

func EncodeAndWriteStringWithoutHTMLEscape added in v0.0.6

func EncodeAndWriteStringWithoutHTMLEscape(w Writer, s []byte)

func EncodeString added in v0.0.3

func EncodeString[S ~string](s S) []byte

EncodeString encodes a string to a json string literal, escaping any characters that are not permitted.

EncodeString calls EncodeAndWriteString with a bytes.Buffer and returns the result.

Logic taken from encoding/json

func IsArray

func IsArray(d []byte) bool

func IsBool

func IsBool(d []byte) bool

func IsEmptyArray

func IsEmptyArray(d []byte) bool

func IsEmptyObject

func IsEmptyObject(d []byte) bool

func IsFalse

func IsFalse(d []byte) bool

IsFalse reports true if data appears to be a json boolean value of false. It is possible that it will report false positives of malformed json as it only checks the first character and length.

IsFalse does not parse strings

func IsNull

func IsNull(d []byte) bool

func IsNumber

func IsNumber(data []byte) bool

IsValid reports whether s is a valid JSON number literal.

Taken from encoding/json

func IsObject

func IsObject(d []byte) bool

func IsString

func IsString(d []byte) bool

func IsTrue

func IsTrue(d []byte) bool

IsTrue reports true if data appears to be a json boolean value of true. It is possible that it will report false positives of malformed json as it only checks the first character and length.

IsTrue does not parse strings

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal calls json.Marshal

func NewDecoder

func NewDecoder(r io.Reader) *json.Decoder

NewDecoder calls json.NewDecoder

func NewEncoder

func NewEncoder(w io.Writer) *json.Encoder

NewEncoder calls json.NewEncoder

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal calls json.Unmarshal

Types

type Number

type Number json.Number

A Number represents a JSON / YAML number literal.

func (Number) BigFloat added in v0.0.2

func (n Number) BigFloat(m big.RoundingMode) (*big.Float, error)

BigFloat returns a *big.Float

func (Number) BigInt added in v0.0.2

func (n Number) BigInt() (*big.Int, bool)

BigInt returns a new *big.Int from n

func (Number) BigRat added in v0.0.2

func (n Number) BigRat() (*big.Rat, bool)

BigRat returns a *big.Rat representation of n

func (Number) Float64 added in v0.0.2

func (n Number) Float64() (float64, error)

Float64 returns the number as a float64.

func (Number) Int64 added in v0.0.2

func (n Number) Int64() (int64, error)

Int64 returns the number as an int64.

func (Number) MarshalJSON added in v0.0.2

func (n Number) MarshalJSON() ([]byte, error)

MarshalJSON marshals json

func (Number) String added in v0.0.2

func (n Number) String() string

String returns the literal text of the number.

func (*Number) UnmarshalJSON added in v0.0.2

func (n *Number) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshals json

type Object

type Object map[string]RawMessage

func (Object) MarshalJSON

func (obj Object) MarshalJSON() ([]byte, error)

func (*Object) UnmarshalJSON

func (obj *Object) UnmarshalJSON(data []byte) error

type RawMessage

type RawMessage []byte

func (RawMessage) ContainsEscapeRune

func (d RawMessage) ContainsEscapeRune() bool

ContainsEscapeRune reports whether the string value of d contains "\" It returns false if d is not a quoted string.

func (RawMessage) Equal

func (d RawMessage) Equal(data []byte) bool

func (RawMessage) IsArray

func (d RawMessage) IsArray() bool

IsArray reports whether the data is a json array. It does not check whether the json is malformed.

func (RawMessage) IsBool

func (d RawMessage) IsBool() bool

IsBool reports true if data appears to be a json boolean value. It is possible that it will report false positives of malformed json.

IsBool does not parse strings

func (RawMessage) IsEmptyArray

func (d RawMessage) IsEmptyArray() bool

func (RawMessage) IsEmptyObject

func (d RawMessage) IsEmptyObject() bool

func (RawMessage) IsFalse

func (d RawMessage) IsFalse() bool

IsFalse reports true if data appears to be a json boolean value of false. It is possible that it will report false positives of malformed json as it only checks the first character and length.

IsFalse does not parse strings

func (RawMessage) IsNull

func (d RawMessage) IsNull() bool

func (RawMessage) IsNumber

func (d RawMessage) IsNumber() bool

func (RawMessage) IsObject

func (d RawMessage) IsObject() bool

func (RawMessage) IsString

func (d RawMessage) IsString() bool

func (RawMessage) IsTrue

func (d RawMessage) IsTrue() bool

IsTrue reports true if data appears to be a json boolean value of true. It is possible that it will report false positives of malformed json as it only checks the first character and length.

IsTrue does not parse strings

func (RawMessage) Len

func (d RawMessage) Len() int

func (RawMessage) MarshalJSON

func (d RawMessage) MarshalJSON() ([]byte, error)

func (*RawMessage) UnmarshalJSON

func (d *RawMessage) UnmarshalJSON(data []byte) error

type Type

type Type uint8
const (
	TypeInvalid Type = iota
	TypeEmpty
	TypeNull
	TypeBool
	TypeNumber
	TypeString
	TypeArray
	TypeObject
)

func TypeOf

func TypeOf(d []byte) Type

func (Type) String

func (t Type) String() string

type Writer added in v0.0.3

type Writer interface {
	Write(p []byte) (n int, err error)
	WriteByte(c byte) error
	WriteString(s string) (n int, err error)
}

Jump to

Keyboard shortcuts

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