Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VsxKK-0005Ul-8s for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Dec 2013 16:16:56 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VsxKJ-0005Zx-Jd for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Dec 2013 16:16:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VsxKI-0005Zr-Tu for pgsql-bugs@postgresql.org; Tue, 17 Dec 2013 16:16:54 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:c:538::196]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VsxKC-000127-Fn for pgsql-bugs@postgresql.org; Tue, 17 Dec 2013 16:16:54 +0000 Received: from mfilter29-d.gandi.net (mfilter29-d.gandi.net [217.70.178.160]) by relay4-d.mail.gandi.net (Postfix) with ESMTP id 71BAF1720EB; Tue, 17 Dec 2013 17:16:45 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter29-d.gandi.net Received: from relay4-d.mail.gandi.net ([217.70.183.196]) by mfilter29-d.gandi.net (mfilter29-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id 7ZIViMMUbxZ6; Tue, 17 Dec 2013 17:16:44 +0100 (CET) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-098-027-058-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 94E551720D6; Tue, 17 Dec 2013 17:16:42 +0100 (CET) Message-ID: <52B078E8.5020707@dunslane.net> Date: Tue, 17 Dec 2013 11:16:40 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 MIME-Version: 1.0 To: Tom Lane CC: Bruce Momjian , email@andersonloyola.com.br, pgsql-bugs@postgresql.org Subject: Re: BUG #8676: Bug Money JSON References: <20131217141021.GA19059@momjian.us> <52B06330.1010503@dunslane.net> <10585.1387294268@sss.pgh.pa.us> In-Reply-To: <10585.1387294268@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------090501040306090408040907" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------090501040306090408040907 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 12/17/2013 10:31 AM, Tom Lane wrote: > Andrew Dunstan writes: >>> On Wed, Dec 11, 2013 at 02:30:04PM +0000, email@andersonloyola.com.br wrote: >>>> postgres=# SELECT to_json(a) FROM (VALUES(1000::money)) a(salario); >>>> to_json >>>> ----------------------- >>>> {"salario":$1,000.00} >>>> (1 row) >> Yeah. I'll have a look. In fact this looks like it's possibly a couple >> of bugs. The JSON produced by the first query is not valid. It looks >> like we might need to force money to text unconditionally. > Isn't this simply failure to quote the string properly? What drives > to_json's choice of whether to quote or not, anyway? > > If it's numeric, it only quotes if it sees a non-numeric character, defined thus: /* letters appearing in numeric output that aren't valid in a JSON number */ #define NON_NUMERIC_LETTER "NnAaIiFfTtYy" I forgot about money when I did that - some of this dates back to 9.2. I'm about to test the attached patch which should force money to be quoted always. cheers andrew --------------090501040306090408040907 Content-Type: text/x-patch; name="json_money.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="json_money.patch" diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c index d8ebf4e..f1c7195 100644 --- a/src/backend/utils/adt/json.c +++ b/src/backend/utils/adt/json.c @@ -1381,6 +1381,8 @@ array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds) tcategory = TYPCATEGORY_COMPOSITE; else if (element_type == JSONOID) tcategory = TYPCATEGORY_JSON; + else if (element_type == CASHOID) + tcategory = TYPCATEGORY_STRING; else tcategory = TypeCategory(element_type); @@ -1476,6 +1478,8 @@ composite_to_json(Datum composite, StringInfo result, bool use_line_feeds) tcategory = TYPCATEGORY_COMPOSITE; else if (tupdesc->attrs[i]->atttypid == JSONOID) tcategory = TYPCATEGORY_JSON; + else if (tupdesc->attrs[i]->atttypid == CASHOID) + tcategory = TYPCATEGORY_STRING; else tcategory = TypeCategory(tupdesc->attrs[i]->atttypid); @@ -1602,6 +1606,8 @@ to_json(PG_FUNCTION_ARGS) tcategory = TYPCATEGORY_COMPOSITE; else if (val_type == JSONOID) tcategory = TYPCATEGORY_JSON; + else if (val_type == CASHOID) + tcategory = TYPCATEGORY_STRING; else tcategory = TypeCategory(val_type); --------------090501040306090408040907 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------090501040306090408040907--