Commits

Andrew Dunstan committed edc62a1

Fix numeric input.

Comments (0)

Files changed (1)

 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
 #include "parser/parse_coerce.h"
+#include "parser/parse_type.h"
 #include "utils/array.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
 PG_FUNCTION_INFO_V1(row_to_json);
 PG_FUNCTION_INFO_V1(row_to_json_pretty);
 
+/* letters appearing in numeric output that aren't valid in a JSON number */
+#define NON_NUMERIC_LETTER "NnAaIiFfTtYy"
 /*
  * Input.
  */
 		case TYPCATEGORY_NUMERIC:
 			outputstr = OidOutputFunctionCall(typoutputfunc, val);
 			/*
-			 * Don't call escape_json here. Numeric output should
-			 * be a valid JSON number and JSON numbers shouldn't
-			 * be quoted.
+			 * Don't call escape_json here if it's a valid JSON
+			 * number. Numeric output should usually be a valid 
+			 * JSON number and JSON numbers shouldn't be quoted. 
+			 * Quote cases like "Nan" and "Infinity", however.
 			 */
-			appendStringInfoString(result, outputstr);
+			if (strpbrk(outputstr,NON_NUMERIC_LETTER) == NULL)
+				appendStringInfoString(result, outputstr);
+			else
+				escape_json(result, outputstr);
 			pfree(outputstr);
 			break;
 		default:
 					  typalign, &elements, &nulls,
 					  &nitems);
 
-	/* can't have an array of arrays, so this is the only special case here */
 	if (element_type == RECORDOID)
 		tcategory = TYPCATEGORY_COMPOSITE;
 	else