Documentation
¶
Overview ¶
Package jsoncol provides JSON[T], a generic database column type that stores T as JSON text: PostgreSQL json/jsonb, or SQLite TEXT.
type Account struct {
ID int64 `db:"id,auto"`
Settings jsoncol.JSON[Settings] `db:"settings"`
Extra *jsoncol.JSON[Extra] `db:"extra"` // nullable column
}
JSON[T] is not intended for ClickHouse, whose native driver does not use driver.Valuer/sql.Scanner for its JSON type.
Index ¶
Constants ¶
const ErrScanType = utils.Error("jsoncol: unsupported scan source type")
ErrScanType is returned by JSON.Scan when the driver hands it a value that is not []byte, string or nil.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type JSON ¶
type JSON[T any] struct { V T }
JSON wraps a value of type T that is stored in the database as JSON.
Value always marshals V, so a nil map, slice or pointer T is written as the JSON literal null, not as SQL NULL. For a nullable column use *JSON[T]: a nil *JSON[T] binds SQL NULL, and scanning SQL NULL into it leaves it nil.
func (JSON[T]) MarshalJSON ¶
MarshalJSON encodes the JSON as V alone, without a wrapping object, so a record holding a JSON[T] field serializes the same as one holding T.
func (*JSON[T]) Scan ¶
Scan implements sql.Scanner. src may be []byte or string holding JSON, or nil (SQL NULL). V is reset to T's zero value before decoding, so SQL NULL and the JSON literal null both leave V at its zero value.
func (*JSON[T]) UnmarshalJSON ¶
UnmarshalJSON decodes data into V.