pgjson

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package pgjson encodes and decodes the jsonb columns of a database that mongo2pg produced.

Why encoding/json is the wrong codec here

mongo2pg writes a jsonb column as Mongo RELAXED EXTENDED JSON: field names come from the document as it existed in MongoDB — i.e. the Go model's `bson` tags — and a date is the object {"$date": "..."} rather than a bare string.

encoding/json reads the `json` tags instead, which on these models are the API-facing camelCase spellings. The two agree only where a field's bson and json tags happen to be identical. Everywhere else, decoding with encoding/json does not fail — it silently yields the zero value, because JSON simply has no such member. A user's `key_set` comes back with an empty userKey and a nil publicKey, and nothing anywhere reports an error.

That is why this package exists and why it is used for EVERY jsonb column rather than only the ones observed to break: "the tags match today" is not a property anyone maintains deliberately, and the failure is silent.

Writes use the same shape as replication

Marshal deliberately emits the same extended-JSON shape mongo2pg writes, so a row this service inserts is indistinguishable from a replicated one. The alternative — reading one shape and writing another — leaves a column holding both, where correctness depends on which process happened to create each row.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v any) ([]byte, error)

Marshal encodes v for a jsonb column.

A nil value becomes a nil []byte, which pgx binds as SQL NULL rather than the four-byte string "null".

func Unmarshal

func Unmarshal(raw []byte, dst any) error

Unmarshal decodes a jsonb column into dst.

An empty or SQL-NULL column leaves dst untouched, matching the json.Unmarshal-based helpers this replaced: a column that holds nothing is not an error, it is a field the document never had.

Types

This section is empty.

Jump to

Keyboard shortcuts

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