PATCH: Implement value_to_json for single-datum conversion

From: Craig Ringer <ringerc(at)ringerc(dot)id(dot)au>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: PATCH: Implement value_to_json for single-datum conversion
Date: 2012-08-13 03:21:38
Message-ID: 502872C2.2050906@ringerc.id.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all

Whenever I try to work with the new json types I trip over the lack of a
function to escape text to json. The attached patch against master
provides one by exposing the existing datum_to_json function to SQL.
I've used the name value_to_json, but I'm not sure it's necessarily the
right choice of name.

Please consider this for the 9.2 branch as well as to HEAD, as IMO it's
very important for basic usability of the json functionality. It applies
to 9.2 fine and passes "make check". I know it's late in the game, but
it's also a very small change and it's very hard to build up JSON data
structures other than simple rows or arrays without at minimum a way of
escaping `text' to json strings.

This feels basic enough that I'm wondering if there's a reason it wasn't
included from the start, but I don't see any comments in json.c talking
about anything like this, nor did I find any -hackers discussion about
it. I suspect it's just an oversight.

As value_to_json directly wraps datum_to_json it actually accepts record
and array types too. I didn't see any reason to prevent that and force
the user to instead use row_to_json or array_to_json for those cases. If
you don't want to accept this, I can provide a wrapper for escape_json
that only accepts a text argument instead, but I think *some* way to
escape text to JSON is vital to have in 9.2.

A docs patch will follow shortly if you're happy that this patch is
reasonable.

--
Craig Ringer

Attachment Content-Type Size
0001-Implement-value_to_json-exposing-the-existing-datum_.patch text/x-patch 0 bytes

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Craig Ringer 2012-08-13 03:44:49 Re: PATCH: Implement value_to_json for single-datum conversion
Previous Message Tom Lane 2012-08-13 02:34:28 Yet another failure mode in pg_upgrade