#include "utils/typcache.h"
 #include "utils/syscache.h"
 
+/* String to output for infinite dates and timestamps */
+#define DT_INFINITY "\"infinity\""
+
 /*
  * The context of the parser is maintained by the recursive descent
  * mechanism, but is passed explicitly to the error reporting routine
 
                date = DatumGetDateADT(val);
 
-               /* XSD doesn't support infinite values */
                if (DATE_NOT_FINITE(date))
-                   ereport(ERROR,
-                           (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
-                            errmsg("date out of range"),
-                            errdetail("JSON does not support infinite date values.")));
+               {
+                   /* we have to format infinity ourselves */
+                   appendStringInfoString(result,DT_INFINITY);
+               }
                else
                {
                    j2date(date + POSTGRES_EPOCH_JDATE,
                           &(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
                    EncodeDateOnly(&tm, USE_XSD_DATES, buf);
+                   appendStringInfo(result, "\"%s\"", buf);
                }
-
-               appendStringInfo(result, "\"%s\"", buf);
            }
            break;
        case JSONTYPE_TIMESTAMP:
 
                timestamp = DatumGetTimestamp(val);
 
-               /* XSD doesn't support infinite values */
                if (TIMESTAMP_NOT_FINITE(timestamp))
-                   ereport(ERROR,
-                           (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
-                            errmsg("timestamp out of range"),
-                            errdetail("JSON does not support infinite timestamp values.")));
+               {
+                   /* we have to format infinity ourselves */
+                   appendStringInfoString(result,DT_INFINITY);
+               }
                else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
+               {
                    EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
+                   appendStringInfo(result, "\"%s\"", buf);
+               }
                else
                    ereport(ERROR,
                            (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                             errmsg("timestamp out of range")));
-
-               appendStringInfo(result, "\"%s\"", buf);
            }
            break;
        case JSONTYPE_TIMESTAMPTZ:
 
                timestamp = DatumGetTimestamp(val);
 
-               /* XSD doesn't support infinite values */
                if (TIMESTAMP_NOT_FINITE(timestamp))
-                   ereport(ERROR,
-                           (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
-                            errmsg("timestamp out of range"),
-                            errdetail("JSON does not support infinite timestamp values.")));
+               {
+                   /* we have to format infinity ourselves */
+                   appendStringInfoString(result,DT_INFINITY);
+               }
                else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
+               {
                    EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
+                   appendStringInfo(result, "\"%s\"", buf);
+               }
                else
                    ereport(ERROR,
                            (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
                             errmsg("timestamp out of range")));
-
-               appendStringInfo(result, "\"%s\"", buf);
            }
            break;
        case JSONTYPE_JSON: