Re: Add jsonb_compact(...) for whitespace-free jsonb to text - Mailing list pgsql-hackers

From Shulgin, Oleksandr
Subject Re: Add jsonb_compact(...) for whitespace-free jsonb to text
Date
Msg-id CACACo5RuOhqVzMuv8HTDRHxHruHk8JkYo7_cf3ZtYsrgz3SQJg@mail.gmail.com
Whole thread Raw
In response to Re: Add jsonb_compact(...) for whitespace-free jsonb to text  (Andrew Dunstan <andrew@dunslane.net>)
Responses Re: Add jsonb_compact(...) for whitespace-free jsonb to text
List pgsql-hackers
On Fri, Apr 29, 2016 at 3:18 PM, Andrew Dunstan <andrew@dunslane.net> wrote:

On 04/28/2016 04:29 PM, Alvaro Herrera wrote:

Actually we did have someone come up with a patch to "normalize" how
JSON stuff was output, because our code seems to do it in three
different, inconsistent ways.  And our response was for them to get the
heck outta here, because we're so much in love with our current
practice that we don't need to refactor the three implementations into a
single one.

That's a pretty bad mischaracterization of the discussion. What was proposed was broken, as I pointed out to the OP, and then again later to you when you asked about it. What he wanted to achieve simply couldn't be done they way he was trying to achieve it.

Yeah, the original request was pretty invalid, but when I've proposed to resubmit just the normalization of whitespace nobody has shown enthusiasm about the idea either:


Regarding the present proposal:

I wouldn't necessarily be opposed to us having one or more of the following:

a) suppressing whitespace addition in all json generation and text output, possibly governed by a GUC setting so we could maintain behaviour compatibility if required

I'm not thrilled about GUC that would silently break stuff.  That being said, if someone's code is dependent on exact placement of whitespace in the JSON text, it's pretty broken already and it's just a matter of time when they hit an issue there.
 
b) a function to remove whitespace from json values, but otherwise preserve things like key order
c) a function to pretty-print json similar to the output from jsonb, but again preserving key order
d) a function to reorder keys in json so they were sorted according to the relevant collation.

None of these things except possibly the last should be terribly difficult to do.

My vote goes to remove all optional whitespace by default and have a single function to prettify it.  Key reordering can then be handled with an optional parameter to such prettifying function.

It would probably make sense model this function after Python's "dump-to-JSON-string" function: https://docs.python.org/2/library/json.html#json.dumps  With the optional parameters for sorting the keys, indentation size and punctuation.  This way all the prettiness enhancements could be contained in a single function w/o the need for generalized interface used in many places.

How about that?

--
Alex

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: 9.6 and fsync=off
Next
From: Michael Paquier
Date:
Subject: Re: VS 2015 support in src/tools/msvc