jsoncol

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

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

View Source
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

func (j JSON[T]) MarshalJSON() ([]byte, error)

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

func (j *JSON[T]) Scan(src any) error

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

func (j *JSON[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes data into V.

func (JSON[T]) Value

func (j JSON[T]) Value() (driver.Value, error)

Value implements driver.Valuer. It returns the JSON encoding of V as a string: text is accepted by PostgreSQL json/jsonb under both the simple and extended protocols and is stored as TEXT (not BLOB) by SQLite.

Jump to

Keyboard shortcuts

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