Re: [GENERAL] Stability of JSON textual representation

2015-02-10 Thread Merlin Moncure
On Mon, Feb 9, 2015 at 3:07 AM, David Evans david.ev...@cantab.net wrote:
 On 9 February 2015 at 03:57, Gavin Flower gavinflo...@archidevsys.co.nz
 wrote:

  Would using jsonb be more consistent?


 Yes, casting to jsonb seems produce consistent output:

 # SELECT array_to_json(array[1, 2, 3])::jsonb, json_build_array(1, 2,
 3)::jsonb;
  array_to_json | json_build_array
 ---+--
  [1, 2, 3] | [1, 2, 3]

 That makes sense I suppose, given that only the structure in preserved in
 the jsonb representation.

This is not really due to whitespace/'structure only' preservation
aspects of json and jsonb, respectively.  What is happening here is
that the various functions that render json from text are non
consistent in terms of spacing.  Personally, I greatly prefer the
xxx_to_json functions because they don't insert spurious whitespace
making for more compact documents; putting in spaces for aesthetic
purposes is the job for a prettifier.

merlin


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Stability of JSON textual representation

2015-02-09 Thread David Evans
On 9 February 2015 at 03:57, Gavin Flower gavinflo...@archidevsys.co.nz
wrote:

  Would using jsonb be more consistent?


Yes, casting to jsonb seems produce consistent output:

# SELECT array_to_json(array[1, 2, 3])::jsonb, json_build_array(1, 2,
3)::jsonb;
 array_to_json | json_build_array
---+--
 [1, 2, 3] | [1, 2, 3]

That makes sense I suppose, given that only the structure in preserved in
the jsonb representation.

Thanks! I should have thought of that myself.

Dave


[GENERAL] Stability of JSON textual representation

2015-02-08 Thread David Evans
I've noticed that when representing lists as JSON, Postgres 9.4 sometimes
outputs spaces after commas, and other times does not.

# SELECT array_to_json(array[1, 2, 3]), json_build_array(1, 2, 3);

 array_to_json | json_build_array
---+--
 [1,2,3]   | [1, 2, 3]


This matters to me because I'm intending the hash the JSON text and use the
hash for change detection.

This difference in behaviour doesn't seem to be documented anywhere. Does
anyone know whether it is likely to be stable?

Many thanks,

Dave


Re: [GENERAL] Stability of JSON textual representation

2015-02-08 Thread Paul Jungwirth
 I've noticed that when representing lists as JSON, Postgres 9.4 sometimes
 outputs spaces after commas, and other times does not.

Here is a similar test on 9.3:

# select '[1,2,3]'::json::text, '[1, 2, 3]'::json::text;
  text   |   text
-+---
 [1,2,3] | [1, 2, 3]

It looks like even casting back and forth between text and json
preserves the text.

Paul

-- 
_
Pulchritudo splendor veritatis.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Stability of JSON textual representation

2015-02-08 Thread Gavin Flower

On 03/02/15 00:06, David Evans wrote:
I've noticed that when representing lists as JSON, Postgres 9.4 
sometimes outputs spaces after commas, and other times does not.


# SELECT array_to_json(array[1, 2, 3]), json_build_array(1, 2, 3);

 array_to_json | json_build_array
---+--
 [1,2,3]   | [1, 2, 3]


This matters to me because I'm intending the hash the JSON text and 
use the hash for change detection.


This difference in behaviour doesn't seem to be documented anywhere. 
Does anyone know whether it is likely to be stable?


Many thanks,

Dave

 Would using jsonb be more consistent?


Cheers,
Gavin


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general