diff --git a/pgtype/json.go b/pgtype/json.go index 562722aa..ef8231b1 100644 --- a/pgtype/json.go +++ b/pgtype/json.go @@ -33,6 +33,15 @@ func (dst *JSON) Set(src interface{}) error { } else { *dst = JSON{Bytes: value, Status: Present} } + // Encode* methods are defined on *JSON. If JSON is passed directly then the + // struct itself would be encoded instead of Bytes. This is clearly a footgun + // so detect and return an error. See https://github.com/jackc/pgx/issues/350. + case JSON: + return errors.New("use pointer to pgtype.JSON instead of value") + // Same as above but for JSONB (because they share implementation) + case JSONB: + return errors.New("use pointer to pgtype.JSONB instead of value") + default: buf, err := json.Marshal(value) if err != nil {