pgtype icon indicating copy to clipboard operation
pgtype copied to clipboard

`func (src VarcharArray) MarshalJSON() error` not implemented

Open powersjcb opened this issue 5 years ago • 2 comments

I noticed that there's no implementation of MarshalJSON() for pgtype.VarcharArray.

Was this intentionally excluded or would this be a good target for contribution?

powersjcb avatar Jun 08 '20 22:06 powersjcb

The original reason was that PG arrays can be multi-dimensional and have arbitrary indexes. e.g.

jack@[local]:5432 jack=# select '[3:4][5:7]={{1,2,3},{4,5,6}}'::int4[];
             int4
──────────────────────────────
 [3:4][5:7]={{1,2,3},{4,5,6}}
(1 row)

There is no clear way to perfectly represent this in JSON. So rather than have an imperfect representation I chose to have no representation. However, on further reflection I suppose it would be reasonable to match the behavior of PG itself.

jack@[local]:5432 jack=# select to_json('[3:4][5:7]={{1,2,3},{4,5,6}}'::int4[]);
      to_json
───────────────────
 [[1,2,3],[4,5,6]]
(1 row)

But one other thing is that the array types are created with code generation -- so it wouldn't make sense to do it just for pgtype.VarcharArray -- it would make sense to do it for all arrays.

jackc avatar Jun 09 '20 13:06 jackc

I spent some time toying around with updates to add MarshalJSON to the array templates.

VarcharArray, TextArray and several others seem like good candidates for this since their Elements have well defined encoding characteristics.

Unfortunately, several pgtypes don't such standardized json encoding. (Numeric, Time, TsRange)

I think I'm starting to see why this hasn't been implemented yet.

powersjcb avatar Jun 09 '20 23:06 powersjcb