Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost) - Mailing list pgsql-bugs

From Tom Lane
Subject Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Date
Msg-id 24940.1498517967@sss.pgh.pa.us
Whole thread Raw
In response to [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)  (Арсен Арутюнян <arutar@bk.ru>)
List pgsql-bugs
Арсен Арутюнян <arutar@bk.ru> writes:
> StringInfo JsonObject = makeStringInfo();
> appendStringInfoString(JsonObject, "{\"ObjectName\":\"Bug Json Object\",\"Type\":227,\"Command\":88}");
> Datum Values[2];
> Values[0] = Int32GetDatum(227);
> Values[1] = CStringGetDatum(JsonObject->data);

That's certainly not going to work, because a CString datum doesn't
have the same representation as a JSON datum.

I think you could get away with using CStringGetTextDatum, relying on
the equivalent physical representations of JSON and text.  But it
wouldn't be a terribly good idea IMO, partly because you'd be passing
up validity checking on the JSON string; plus you could never make
that work for JSONB.  What you really should do instead is pass the
CString to JSON's input function, say
 Values[1] = OidInputFunctionCall(F_JSON_IN, JsonObject->data, JSONOID, -1);

This is still cheating a bit --- it knows more than it ought to about
what to pass for typioparam --- but it's unlikely to break.
        regards, tom lane


--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

pgsql-bugs by date:

Previous
From: Andres Freund
Date:
Subject: Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from Ccode first { simboll is lost)
Next
From: Thomas_OReilly
Date:
Subject: [BUGS] libpq.so.5 dependancy error installing pgsql onto linux 6.