json

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2022 License: MIT Imports: 13 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Module = map[string]ugo.Object{

	"Marshal": &ugo.Function{
		Name: "Marshal",
		Value: stdlib.FuncPORO(
			func(o ugo.Object) ugo.Object {
				b, err := Marshal(o)
				if err != nil {
					return &ugo.Error{Message: err.Error(), Cause: err}
				}
				return ugo.Bytes(b)
			},
		),
	},

	"MarshalIndent": &ugo.Function{
		Name: "MarshalIndent",
		Value: stdlib.FuncPOssRO(
			func(o ugo.Object, prefix, indent string) ugo.Object {
				b, err := MarshalIndent(o, prefix, indent)
				if err != nil {
					return &ugo.Error{Message: err.Error(), Cause: err}
				}
				return ugo.Bytes(b)
			},
		),
	},

	"Indent": &ugo.Function{
		Name: "Indent",
		Value: stdlib.FuncPb2ssRO(
			func(src []byte, prefix, indent string) ugo.Object {
				var buf bytes.Buffer
				err := indentBuffer(&buf, src, prefix, indent)
				if err != nil {
					return &ugo.Error{Message: err.Error(), Cause: err}
				}
				return ugo.Bytes(buf.Bytes())
			},
		),
	},

	"RawMessage": &ugo.Function{
		Name: "RawMessage",
		Value: stdlib.FuncPb2RO(func(b []byte) ugo.Object {
			return &RawMessage{Value: b}
		}),
	},

	"Compact": &ugo.Function{
		Name: "Compact",
		Value: stdlib.FuncPb2bRO(func(data []byte, escape bool) ugo.Object {
			var buf bytes.Buffer
			err := compact(&buf, data, escape)
			if err != nil {
				return &ugo.Error{Message: err.Error(), Cause: err}
			}
			return ugo.Bytes(buf.Bytes())
		}),
	},

	"Quote": &ugo.Function{
		Name: "Quote",
		Value: stdlib.FuncPORO(func(o ugo.Object) ugo.Object {
			if v, ok := o.(*EncoderOptions); ok {
				v.Quote = true
				return v
			}
			return &EncoderOptions{Value: o, Quote: true, EscapeHTML: true}
		}),
	},

	"NoQuote": &ugo.Function{
		Name: "NoQuote",
		Value: stdlib.FuncPORO(func(o ugo.Object) ugo.Object {
			if v, ok := o.(*EncoderOptions); ok {
				v.Quote = false
				return v
			}
			return &EncoderOptions{Value: o, Quote: false, EscapeHTML: true}
		}),
	},

	"NoEscape": &ugo.Function{
		Name: "NoEscape",
		Value: stdlib.FuncPORO(func(o ugo.Object) ugo.Object {
			if v, ok := o.(*EncoderOptions); ok {
				v.EscapeHTML = false
				return v
			}
			return &EncoderOptions{Value: o}
		}),
	},

	"Unmarshal": &ugo.Function{
		Name: "Unmarshal",
		Value: stdlib.FuncPb2RO(func(b []byte) ugo.Object {
			v, err := Unmarshal(b)
			if err != nil {
				return &ugo.Error{Message: err.Error(), Cause: err}
			}
			return v
		}),
	},

	"Valid": &ugo.Function{
		Name: "Valid",
		Value: stdlib.FuncPb2RO(func(b []byte) ugo.Object {
			return ugo.Bool(valid(b))
		}),
	},
}

Module represents json module.

Functions

func Marshal

func Marshal(v ugo.Object) ([]byte, error)

Marshal returns the JSON encoding of v.

func MarshalIndent

func MarshalIndent(v ugo.Object, prefix, indent string) ([]byte, error)

MarshalIndent is like Marshal but applies Indent to format the output. Each JSON element in the output will begin on a new line beginning with prefix followed by one or more copies of indent according to the indentation nesting.

func Unmarshal

func Unmarshal(data []byte) (ugo.Object, error)

Unmarshal parses the JSON-encoded data and stores the result in the value pointed to by v. If v is nil or not a pointer, Unmarshal returns an InvalidUnmarshalError.

Types

type EncoderOptions

type EncoderOptions struct {
	ugo.ObjectImpl
	Value      ugo.Object
	Quote      bool
	EscapeHTML bool
}

EncoderOptions represents the encoding options (quote, html escape) to Marshal any Object.

func (*EncoderOptions) IndexGet

func (eo *EncoderOptions) IndexGet(index ugo.Object) (ret ugo.Object, err error)

IndexGet implements ugo.Object interface.

func (*EncoderOptions) IndexSet

func (eo *EncoderOptions) IndexSet(index, value ugo.Object) error

IndexSet implements ugo.Object interface.

func (*EncoderOptions) String

func (eo *EncoderOptions) String() string

String implements ugo.Object interface.

func (*EncoderOptions) TypeName

func (eo *EncoderOptions) TypeName() string

TypeName implements ugo.Object interface.

type Marshaler

type Marshaler interface {
	MarshalJSON() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves into valid JSON.

type MarshalerError

type MarshalerError struct {
	Object ugo.Object
	Err    error
	// contains filtered or unexported fields
}

A MarshalerError represents an error from calling a MarshalJSON or MarshalText method.

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

func (*MarshalerError) Unwrap

func (e *MarshalerError) Unwrap() error

Unwrap returns the underlying error.

type RawMessage

type RawMessage struct {
	ugo.ObjectImpl
	Value []byte
}

RawMessage represents raw encoded json message to directly use value of MarshalJSON without encoding.

func (*RawMessage) IndexGet

func (rm *RawMessage) IndexGet(index ugo.Object) (ret ugo.Object, err error)

IndexGet implements ugo.Object interface.

func (*RawMessage) IndexSet

func (rm *RawMessage) IndexSet(index, value ugo.Object) error

IndexSet implements ugo.Object interface.

func (*RawMessage) MarshalJSON

func (rm *RawMessage) MarshalJSON() ([]byte, error)

MarshalJSON implements Marshaler interface and returns rm as the JSON encoding of rm.Value.

func (*RawMessage) String

func (rm *RawMessage) String() string

String implements ugo.Object interface.

func (*RawMessage) TypeName

func (rm *RawMessage) TypeName() string

TypeName implements ugo.Object interface.

type SyntaxError

type SyntaxError struct {
	Offset int64 // error occurred after reading Offset bytes
	// contains filtered or unexported fields
}

A SyntaxError is a description of a JSON syntax error.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type UnsupportedValueError

type UnsupportedValueError struct {
	Object ugo.Object
	Str    string
}

An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.

func (*UnsupportedValueError) Error

func (e *UnsupportedValueError) Error() string

Jump to

Keyboard shortcuts

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