Documentation
¶
Overview ¶
Package encoding provides utility for encoding/decoding IDL types to/from their JSON representation.
Index ¶
- func AsByte(v any) (byte, error)
- func AsEnum[E Enum](v any) (E, error)
- func AsFloat32(v any) (float32, error)
- func AsFloat64(v any) (float64, error)
- func AsInt(v any) (int, error)
- func AsInt32(v any) (int32, error)
- func AsInt64(v any) (int64, error)
- func AsMapItems(v any) (items iter.Seq2[any, any], errFn func() error, length int)
- func AsTime(v any) (time.Time, error)
- func AsTypeCode(v any) (idl.TypeCode, error)
- func In(key string, m map[string]any) error
- func InAndIs[T JSON](key string, m map[string]any) (T, error)
- func Is[T JSON](v any) (t T, err error)
- func NonNilSlice[T SimpleEncoding](slice []T) []T
- type Enum
- type Fallbacker
- type JSON
- type SimpleEncoding
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsByte ¶
AsByte asserts that v is a json.Number and parses it as a byte.
func AsEnum ¶
AsEnum asserts that v is a json.Number and parses it as Enum type E. For unknown values, a fallback is returned if E is a Fallbacker.
func AsFloat32 ¶
AsFloat32 asserts that v is a json.Number and parses it as a float32.
func AsFloat64 ¶
AsFloat64 asserts that v is a json.Number and parses it as a float64.
func AsInt ¶
AsInt asserts that v is a json.Number and parses it as an int.
func AsInt32 ¶
AsInt32 asserts that v is a json.Number and parses it as an int32.
func AsInt64 ¶
AsInt64 asserts that v is a json.Number and parses it as an int64.
func AsMapItems ¶
AsMapItems asserts that v is a container slice of map items with "key" and "value". It returns an iterator over those key-value pairs that stops on the first error, a callback to retrieve this error, and the slice length.
func AsTime ¶
AsTime asserts that v is a json.Number and parses it as a time.Time. The number is interpreted as seconds since the Unix epoch.
func AsTypeCode ¶
AsTypeCode asserts that v is a string and parses it as an idl.TypeCode.
func InAndIs ¶
InAndIs checks that m contains a value for the key like In, and if so asserts that the value is of type T like Is.
func NonNilSlice ¶
func NonNilSlice[T SimpleEncoding](slice []T) []T
NonNilSlice returns the passed slice as is if non-nil. Else a new empty slice of same type is returned. This forces json.Marshal to encode it as a JSON array instead of as JSON null.
Types ¶
type Fallbacker ¶
type Fallbacker[E Enum] interface { Fallback() E }
A Fallbacker has a known Enum value as fallback.
type JSON ¶
type JSON interface {
// true/false | string | array | object | number
bool | string | []any | map[string]any | json.Number
}
JSON types are those which json.Decoder.Decode stores into an interface value when decoding JSON after json.Decoder.UseNumber was called.
The json.Decoder stores nil for JSON null.
type SimpleEncoding ¶
type SimpleEncoding interface {
// boolean | string | int | long | float | double | enumeration
bool | string | int32 | int64 | float32 | float64 | ~int
}
SimpleEncoding types are those of which non-nil slices can be passed to json.Marshal without further encoding work.
Note that byte is not included, as json.Marshal has special handling for []byte.