outputstr = OidOutputFunctionCall(outfuncoid, val);
/*
- * Don't call escape_json for a non-key if it's a valid JSON
- * number.
+ * Don't quote a non-key if it's a valid JSON number (i.e., not
+ * "Infinity", "-Infinity", or "NaN"). Since we know this is a
+ * numeric data type's output, we simplify and open-code the
+ * validation for better performance.
*/
- if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
+ if (!key_scalar &&
+ ((*outputstr >= '0' && *outputstr <= '9') ||
+ (*outputstr == '-' &&
+ (outputstr[1] >= '0' && outputstr[1] <= '9'))))
appendStringInfoString(result, outputstr);
else
- escape_json(result, outputstr);
+ {
+ appendStringInfoChar(result, '"');
+ appendStringInfoString(result, outputstr);
+ appendStringInfoChar(result, '"');
+ }
pfree(outputstr);
break;
case JSONTYPE_DATE:
char buf[MAXDATELEN + 1];
JsonEncodeDateTime(buf, val, DATEOID, NULL);
- appendStringInfo(result, "\"%s\"", buf);
+ appendStringInfoChar(result, '"');
+ appendStringInfoString(result, buf);
+ appendStringInfoChar(result, '"');
}
break;
case JSONTYPE_TIMESTAMP:
char buf[MAXDATELEN + 1];
JsonEncodeDateTime(buf, val, TIMESTAMPOID, NULL);
- appendStringInfo(result, "\"%s\"", buf);
+ appendStringInfoChar(result, '"');
+ appendStringInfoString(result, buf);
+ appendStringInfoChar(result, '"');
}
break;
case JSONTYPE_TIMESTAMPTZ:
char buf[MAXDATELEN + 1];
JsonEncodeDateTime(buf, val, TIMESTAMPTZOID, NULL);
- appendStringInfo(result, "\"%s\"", buf);
+ appendStringInfoChar(result, '"');
+ appendStringInfoString(result, buf);
+ appendStringInfoChar(result, '"');
}
break;
case JSONTYPE_JSON:
int i;
bool needsep = false;
const char *sep;
+ int seplen;
+ /*
+ * We can avoid expensive strlen() calls by precalculating the separator
+ * length.
+ */
sep = use_line_feeds ? ",\n " : ",";
+ seplen = use_line_feeds ? strlen(",\n ") : strlen(",");
td = DatumGetHeapTupleHeader(composite);
continue;
if (needsep)
- appendStringInfoString(result, sep);
+ appendBinaryStringInfo(result, sep, seplen);
needsep = true;
attname = NameStr(att->attname);