Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json operators treat pguint as strings... #15

Open
NoMoreNicksLeft opened this issue Mar 25, 2020 · 2 comments
Open

Json operators treat pguint as strings... #15

NoMoreNicksLeft opened this issue Mar 25, 2020 · 2 comments

Comments

@NoMoreNicksLeft
Copy link

create table crap (test integer);
CREATE TABLE
insert into crap values (456);
INSERT 0 1
select row_to_json(crap) as j from crap;
      j
--------------
 {"test":456}

But if instead I use...

create table crap2 (test uint4);
CREATE TABLE
insert into crap2 values (456);
INSERT 0 1
select row_to_json(crap2) as j from crap2;
       j
----------------
 {"test":"456"}

This is a minor nitpick of course. I was hoping that there's some clever create cast statement that will resolve the issue. Or would a solution need to be more involved than this?

@ranvis
Copy link

ranvis commented Mar 27, 2020

Source code says cast to json is looked up:
https://github.com/postgres/postgres/blob/24e2885ee304cb6a94fdfc25a1a108344ed9f4f7/src/backend/utils/adt/json.c#L206

create function json(uint4) returns json as 'select $1::text::json;' language sql immutable returns null on null input;
create cast (uint4 as json) with function json(uint4);
select row_to_json(crap2) as j from crap2;
      j
--------------
 {"test":456}

Though I don't know if it is optimal, this seems to work.

@NoMoreNicksLeft
Copy link
Author

Thanks @ranvis . This is awesome. This will be a big help for me actually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants