PATCH: Implement value_to_json for single-datum conversion - Mailing list pgsql-hackers

From Craig Ringer
Subject PATCH: Implement value_to_json for single-datum conversion
Date
Msg-id 502872C2.2050906@ringerc.id.au
Whole thread Raw
Responses Re: PATCH: Implement value_to_json for single-datum conversion
Re: PATCH: Implement value_to_json for single-datum conversion
List 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

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Yet another failure mode in pg_upgrade
Next
From: Craig Ringer
Date:
Subject: Re: PATCH: Implement value_to_json for single-datum conversion