public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tim McLaughlin <[email protected]>
To: [email protected]
Subject: change JSON serialization for BIGINT?
Date: Tue, 26 Nov 2024 06:34:06 -0500
Message-ID: <536dca0f-ef1a-4621-ac1f-9c1c1dd74fbd@Spark> (raw)

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


view thread (3+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: change JSON serialization for BIGINT?
  In-Reply-To: <536dca0f-ef1a-4621-ac1f-9c1c1dd74fbd@Spark>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox