Documentation
¶
Overview ¶
Package jsonslice is a JSON-backed slice type that round-trips through SQL columns and GraphQL scalars without dragging gqlgen or any database driver into the dependency graph.
The core type is JsonSlice, an alias for []any that:
- Marshals/unmarshals as a JSON array via the standard encoding/json interfaces.
- Implements sql.Scanner and driver.Valuer so it can be stored in a JSON / JSONB column directly.
- Implements MarshalGQL/UnmarshalGQL so gqlgen will pick it up as a custom scalar via duck typing — no gqlgen import is required, and consumers that don't use gqlgen pay nothing.
The zero value is an empty slice. nil is treated as the JSON value null when scanning; on marshal a nil JsonSlice is rendered as null too.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JsonSlice ¶
type JsonSlice []any
JsonSlice is a slice of arbitrary JSON values.
func (JsonSlice) MarshalGQL ¶
MarshalGQL is the gqlgen-compatible marshal hook. gqlgen detects this method by name; we therefore need not import gqlgen. Errors are rare (the slice already round-tripped through JSON to reach a resolver) and would indicate a programmer error encoding an unsupported type into the slice — those are surfaced by writing an empty array, never panicking inside a request goroutine.
func (JsonSlice) MarshalJSON ¶
MarshalJSON implements json.Marshaler. A nil slice marshals to the JSON value null; an empty non-nil slice marshals to [].
func (*JsonSlice) Scan ¶
Scan implements [sql.Scanner]. Accepts []byte, string, or nil. NULL scans into a nil slice.
func (*JsonSlice) UnmarshalGQL ¶
UnmarshalGQL is the gqlgen-compatible unmarshal hook. It accepts the already-parsed JSON value gqlgen passes (a []any for a JSON array, or a scalar that the receiver chooses to wrap), re-encodes it, and decodes into the slice.
func (*JsonSlice) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler. The JSON value null deserialises to a nil slice.