On 05/20/2015 09:16 AM, Shulgin, Oleksandr wrote:
> Hi, Hackers!
>
> Attached is a patch against master to generalize the JSON-producing
> functions in utils/adt/json.c and to provide a set of callbacks which
> can be overridden the same way that is already provided for *parsing*
> JSON.
>
> The motivation behind this to be able to produce specially-crafted
> JSON in a logical replication output plugin, such that numeric (and
> bigint) values are quoted. This requirement, in turn, arises from the
> fact that JavaScript specification, which is quite natural to expect
> as a consumer for this JSON data, allows to silently drop significant
> digits when converting from string to number object.
>
> I believe this is a well-known problem and I'm aware of a number of
> tricks that might be used to avoid it, but none of them seems to be
> optimal from my standpoint.
>
> I can also imagine this can be used to convert date/time to string
> differently, or adding indentation depending on the depth in object
> hierarchy, etc.
I'm not necessarily opposed to this, but it sure seems like a lot of
changes, and moderately invasive ones, to support something that could
be done, at the cost of reparsing, with a simple loadable extension that
I could create in a few hours of programming. The parser API was created
precisely to make this sort of transformation close to trivial. Other
fairly obvious transformations include translating to XML or YAML, and a
less obvious one could be something very specialized, like translating
certain fields. Anyway, for this purpose I could imagine a function like:
json_format ( j json (or text), indent_spaces smallint default 0, space_after_colon boolean
defaultfalse, space_after_comma boolean default false, quote_numerics boolean default false) returns
json
cheers
andrew