Skip to content

Instantly share code, notes, and snippets.

@ozkansen
Created May 25, 2024 17:26
Show Gist options
  • Save ozkansen/a39d6f83ef877f272a281c81a1fbce23 to your computer and use it in GitHub Desktop.
Save ozkansen/a39d6f83ef877f272a281c81a1fbce23 to your computer and use it in GitHub Desktop.
Go Postgresql Client: pgx custom json marshaller & unmarshaller
package example
import (
pgx "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
)
func postgresCustomJSONUpgrade(conn *pgxpool.Pool) {
conn.Config().AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
conn.TypeMap().RegisterType(&pgtype.Type{
Name: "json",
OID: pgtype.JSONOID,
Codec: &pgtype.JSONCodec{
Marshal: json.Marshal,
Unmarshal: json.Unmarshal,
},
})
conn.TypeMap().RegisterType(&pgtype.Type{
Name: "jsonb",
OID: pgtype.JSONBOID,
Codec: &pgtype.JSONBCodec{
Marshal: json.Marshal,
Unmarshal: json.Unmarshal,
},
})
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment