jsonb top-level constant common

Codec<Object?, Uint8List> const jsonb

A Codec capable of converting Dart objects from and to the JSONB format used by sqlite3.

The codec is useful when columns stored as blobs in SQLite are to be interpreted as JSONB values, as one conversion step between Dart and SQLite (usually implemented by mapping to a JSON string in Dart and then calling the jsonb SQL function, or calling json the other way around) becomes superfluous.

This codec's Codec.encoder supports the same objects as Dart's json encoder with the addition of non-finite double values that can't be represented in regular JSON. When passing a custom object into Codec.encode, it will attempt to call a toJson() method. The encoder also throws the same JsonCyclicError and JsonUnsupportedObjectError classes thrown by the native JSON encoder.

Example:

import 'package:sqlite3/sqlite3.dart';

void main() {
  final database = sqlite3.openInMemory()
    ..execute('CREATE TABLE entries (entry BLOB NOT NULL) STRICT;')
    // You can insert JSONB-formatted values directly
    ..execute('INSERT INTO entries (entry) VALUES (?)', [
      jsonb.encode({'hello': 'dart'})
    ]);
  // And use them with JSON operators in SQLite without a further conversion:
  print(database.select('SELECT entry ->> ? AS r FROM entries;', [r'$.hello']));
}

{@category common}

Implementation

const Codec<Object?, Uint8List> jsonb = _JsonbCodec();