Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Арсен Арутюнян <arutar(at)bk(dot)ru>
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Date: 2017-06-26 22:59:27
Message-ID: 24940.1498517967@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

=?UTF-8?B?0JDRgNGB0LXQvSDQkNGA0YPRgtGO0L3Rj9C9?= <arutar(at)bk(dot)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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Thomas_OReilly 2017-06-27 07:40:29 libpq.so.5 dependancy error installing pgsql onto linux 6.
Previous Message Andres Freund 2017-06-26 22:35:36 Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)