3232#include "utils/typcache.h"
3333#include "utils/syscache.h"
3434
35- /* String to output for infinite dates and timestamps */
36- #define DT_INFINITY "\"infinity\""
37-
3835/*
3936 * The context of the parser is maintained by the recursive descent
4037 * mechanism, but is passed explicitly to the error reporting routine
@@ -70,11 +67,11 @@ typedef enum /* type categories for datum_to_json */
7067
7168typedef struct JsonAggState
7269{
73- StringInfo str ;
74- JsonTypeCategory key_category ;
75- Oid key_output_func ;
76- JsonTypeCategory val_category ;
77- Oid val_output_func ;
70+ StringInfo str ;
71+ JsonTypeCategory key_category ;
72+ Oid key_output_func ;
73+ JsonTypeCategory val_category ;
74+ Oid val_output_func ;
7875} JsonAggState ;
7976
8077static inline void json_lex (JsonLexContext * lex );
@@ -360,16 +357,16 @@ pg_parse_json(JsonLexContext *lex, JsonSemAction *sem)
360357int
361358json_count_array_elements (JsonLexContext * lex )
362359{
363- JsonLexContext copylex ;
364- int count ;
360+ JsonLexContext copylex ;
361+ int count ;
365362
366363 /*
367364 * It's safe to do this with a shallow copy because the lexical routines
368- * don't scribble on the input. They do scribble on the other pointers etc,
369- * so doing this with a copy makes that safe.
365+ * don't scribble on the input. They do scribble on the other pointers
366+ * etc, so doing this with a copy makes that safe.
370367 */
371368 memcpy (& copylex , lex , sizeof (JsonLexContext ));
372- copylex .strval = NULL ; /* not interested in values here */
369+ copylex .strval = NULL ; /* not interested in values here */
373370 copylex .lex_level ++ ;
374371
375372 count = 0 ;
@@ -1492,19 +1489,16 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
14921489 char buf [MAXDATELEN + 1 ];
14931490
14941491 date = DatumGetDateADT (val );
1495-
1492+ /* Same as date_out(), but forcing DateStyle */
14961493 if (DATE_NOT_FINITE (date ))
1497- {
1498- /* we have to format infinity ourselves */
1499- appendStringInfoString (result , DT_INFINITY );
1500- }
1494+ EncodeSpecialDate (date , buf );
15011495 else
15021496 {
15031497 j2date (date + POSTGRES_EPOCH_JDATE ,
15041498 & (tm .tm_year ), & (tm .tm_mon ), & (tm .tm_mday ));
15051499 EncodeDateOnly (& tm , USE_XSD_DATES , buf );
1506- appendStringInfo (result , "\"%s\"" , buf );
15071500 }
1501+ appendStringInfo (result , "\"%s\"" , buf );
15081502 }
15091503 break ;
15101504 case JSONTYPE_TIMESTAMP :
@@ -1515,21 +1509,16 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
15151509 char buf [MAXDATELEN + 1 ];
15161510
15171511 timestamp = DatumGetTimestamp (val );
1518-
1512+ /* Same as timestamp_out(), but forcing DateStyle */
15191513 if (TIMESTAMP_NOT_FINITE (timestamp ))
1520- {
1521- /* we have to format infinity ourselves */
1522- appendStringInfoString (result , DT_INFINITY );
1523- }
1514+ EncodeSpecialTimestamp (timestamp , buf );
15241515 else if (timestamp2tm (timestamp , NULL , & tm , & fsec , NULL , NULL ) == 0 )
1525- {
15261516 EncodeDateTime (& tm , fsec , false, 0 , NULL , USE_XSD_DATES , buf );
1527- appendStringInfo (result , "\"%s\"" , buf );
1528- }
15291517 else
15301518 ereport (ERROR ,
15311519 (errcode (ERRCODE_DATETIME_VALUE_OUT_OF_RANGE ),
15321520 errmsg ("timestamp out of range" )));
1521+ appendStringInfo (result , "\"%s\"" , buf );
15331522 }
15341523 break ;
15351524 case JSONTYPE_TIMESTAMPTZ :
@@ -1541,22 +1530,17 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
15411530 const char * tzn = NULL ;
15421531 char buf [MAXDATELEN + 1 ];
15431532
1544- timestamp = DatumGetTimestamp (val );
1545-
1533+ timestamp = DatumGetTimestampTz (val );
1534+ /* Same as timestamptz_out(), but forcing DateStyle */
15461535 if (TIMESTAMP_NOT_FINITE (timestamp ))
1547- {
1548- /* we have to format infinity ourselves */
1549- appendStringInfoString (result , DT_INFINITY );
1550- }
1536+ EncodeSpecialTimestamp (timestamp , buf );
15511537 else if (timestamp2tm (timestamp , & tz , & tm , & fsec , & tzn , NULL ) == 0 )
1552- {
15531538 EncodeDateTime (& tm , fsec , true, tz , tzn , USE_XSD_DATES , buf );
1554- appendStringInfo (result , "\"%s\"" , buf );
1555- }
15561539 else
15571540 ereport (ERROR ,
15581541 (errcode (ERRCODE_DATETIME_VALUE_OUT_OF_RANGE ),
15591542 errmsg ("timestamp out of range" )));
1543+ appendStringInfo (result , "\"%s\"" , buf );
15601544 }
15611545 break ;
15621546 case JSONTYPE_JSON :
@@ -1875,7 +1859,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
18751859{
18761860 MemoryContext aggcontext ,
18771861 oldcontext ;
1878- JsonAggState * state ;
1862+ JsonAggState * state ;
18791863 Datum val ;
18801864
18811865 if (!AggCheckCallContext (fcinfo , & aggcontext ))
@@ -1886,7 +1870,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
18861870
18871871 if (PG_ARGISNULL (0 ))
18881872 {
1889- Oid arg_type = get_fn_expr_argtype (fcinfo -> flinfo , 1 );
1873+ Oid arg_type = get_fn_expr_argtype (fcinfo -> flinfo , 1 );
18901874
18911875 if (arg_type == InvalidOid )
18921876 ereport (ERROR ,
@@ -1905,7 +1889,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
19051889 MemoryContextSwitchTo (oldcontext );
19061890
19071891 appendStringInfoChar (state -> str , '[' );
1908- json_categorize_type (arg_type ,& state -> val_category ,
1892+ json_categorize_type (arg_type , & state -> val_category ,
19091893 & state -> val_output_func );
19101894 }
19111895 else
@@ -1949,7 +1933,7 @@ json_agg_transfn(PG_FUNCTION_ARGS)
19491933Datum
19501934json_agg_finalfn (PG_FUNCTION_ARGS )
19511935{
1952- JsonAggState * state ;
1936+ JsonAggState * state ;
19531937
19541938 /* cannot be called directly because of internal-type argument */
19551939 Assert (AggCheckCallContext (fcinfo , NULL ));
@@ -1976,7 +1960,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
19761960{
19771961 MemoryContext aggcontext ,
19781962 oldcontext ;
1979- JsonAggState * state ;
1963+ JsonAggState * state ;
19801964 Datum arg ;
19811965
19821966 if (!AggCheckCallContext (fcinfo , & aggcontext ))
@@ -2007,7 +1991,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
20071991 (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
20081992 errmsg ("could not determine data type for argument 1" )));
20091993
2010- json_categorize_type (arg_type ,& state -> key_category ,
1994+ json_categorize_type (arg_type , & state -> key_category ,
20111995 & state -> key_output_func );
20121996
20131997 arg_type = get_fn_expr_argtype (fcinfo -> flinfo , 2 );
@@ -2017,7 +2001,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
20172001 (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
20182002 errmsg ("could not determine data type for argument 2" )));
20192003
2020- json_categorize_type (arg_type ,& state -> val_category ,
2004+ json_categorize_type (arg_type , & state -> val_category ,
20212005 & state -> val_output_func );
20222006
20232007 appendStringInfoString (state -> str , "{ " );
@@ -2065,7 +2049,7 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
20652049Datum
20662050json_object_agg_finalfn (PG_FUNCTION_ARGS )
20672051{
2068- JsonAggState * state ;
2052+ JsonAggState * state ;
20692053
20702054 /* cannot be called directly because of internal-type argument */
20712055 Assert (AggCheckCallContext (fcinfo , NULL ));
0 commit comments