change JSON serialization for BIGINT?

From: Tim McLaughlin <tim(at)gotab(dot)io>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: change JSON serialization for BIGINT?
Date: 2024-11-26 11:34:06
Message-ID: 536dca0f-ef1a-4621-ac1f-9c1c1dd74fbd@Spark
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Is there a way to have Postgres serialize BIGINT as a string rather than number in JSON?  By default it does this:

select row_to_json(row(500::bigint));
 row_to_json
-------------
 {"f1":500}

But I want it to do this (note that "500" is quoted):

select row_to_json(row(500::bigint));
 row_to_json
-------------
 {"f1":"500"}

I tried doing this, but it has no effect:

CREATE or replace FUNCTION bigintJson(bigint) RETURNS json
    AS 'select $1::text::json;'
    LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT;

create cast (bigint as json) with function bigintJson(bigint) as implicit;

Thanks for any guidance.

--
Tim McLaughlin

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Victor Yegorov 2024-11-26 11:36:26 Re: change JSON serialization for BIGINT?
Previous Message Paul Foerster 2024-11-26 10:10:30 Re: DB Switchover using repmgr--Error