PostGIS 3.6.2dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ datum_to_json()

static void datum_to_json ( Datum  val,
bool  is_null,
StringInfo  result,
JsonTypeCategory  tcategory,
Oid  outfuncoid,
bool  key_scalar 
)
static

Definition at line 385 of file lwgeom_out_geojson.c.

388{
389 char *outputstr;
390 text *jsontext;
391
392 check_stack_depth();
393
394 /* callers are expected to ensure that null keys are not passed in */
395 Assert(!(key_scalar && is_null));
396
397 if (is_null)
398 {
399 appendStringInfoString(result, "null");
400 return;
401 }
402
403 if (key_scalar &&
404 (tcategory == JSONTYPE_ARRAY ||
405 tcategory == JSONTYPE_COMPOSITE ||
406 tcategory == JSONTYPE_JSON ||
407 tcategory == JSONTYPE_CAST))
408 ereport(ERROR,
409 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
410 errmsg("key value must be scalar, not array, composite, or json")));
411
412 switch (tcategory)
413 {
414 case JSONTYPE_ARRAY:
415 array_to_json_internal(val, result, false);
416 break;
418 composite_to_json(val, result, false);
419 break;
420 case JSONTYPE_BOOL:
421 outputstr = DatumGetBool(val) ? "true" : "false";
422 if (key_scalar)
423 escape_json(result, outputstr);
424 else
425 appendStringInfoString(result, outputstr);
426 break;
427 case JSONTYPE_NUMERIC:
428 outputstr = OidOutputFunctionCall(outfuncoid, val);
429
430 /*
431 * Don't call escape_json for a non-key if it's a valid JSON
432 * number.
433 */
434 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
435 appendStringInfoString(result, outputstr);
436 else
437 escape_json(result, outputstr);
438 pfree(outputstr);
439 break;
440 case JSONTYPE_DATE:
441 {
442 char buf[MAXDATELEN + 1];
443
444 postgis_JsonEncodeDateTime(buf, val, DATEOID);
445 appendStringInfo(result, "\"%s\"", buf);
446 }
447 break;
449 {
450 char buf[MAXDATELEN + 1];
451
452 postgis_JsonEncodeDateTime(buf, val, TIMESTAMPOID);
453 appendStringInfo(result, "\"%s\"", buf);
454 }
455 break;
457 {
458 char buf[MAXDATELEN + 1];
459
460 postgis_JsonEncodeDateTime(buf, val, TIMESTAMPTZOID);
461 appendStringInfo(result, "\"%s\"", buf);
462 }
463 break;
464 case JSONTYPE_JSON:
465 /* JSON and JSONB output will already be escaped */
466 outputstr = OidOutputFunctionCall(outfuncoid, val);
467 appendStringInfoString(result, outputstr);
468 pfree(outputstr);
469 break;
470 case JSONTYPE_CAST:
471 /* outfuncoid refers to a cast function, not an output function */
472 jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
473 outputstr = text_to_cstring(jsontext);
474 appendStringInfoString(result, outputstr);
475 pfree(outputstr);
476 pfree(jsontext);
477 break;
478 default:
479 outputstr = OidOutputFunctionCall(outfuncoid, val);
480 escape_json(result, outputstr);
481 pfree(outputstr);
482 break;
483 }
484}
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
@ JSONTYPE_JSON
@ JSONTYPE_TIMESTAMP
@ JSONTYPE_NUMERIC
@ JSONTYPE_DATE
@ JSONTYPE_BOOL
@ JSONTYPE_CAST
@ JSONTYPE_COMPOSITE
@ JSONTYPE_ARRAY
@ JSONTYPE_TIMESTAMPTZ
static char * postgis_JsonEncodeDateTime(char *buf, Datum value, Oid typid)
static void composite_to_json(Datum composite, StringInfo result, bool use_line_feeds)
static void array_to_json_internal(Datum array, StringInfo result, bool use_line_feeds)

References array_to_json_internal(), composite_to_json(), JSONTYPE_ARRAY, JSONTYPE_BOOL, JSONTYPE_CAST, JSONTYPE_COMPOSITE, JSONTYPE_DATE, JSONTYPE_JSON, JSONTYPE_NUMERIC, JSONTYPE_TIMESTAMP, JSONTYPE_TIMESTAMPTZ, postgis_JsonEncodeDateTime(), and result.

Referenced by array_dim_to_json(), composite_to_geojson(), and composite_to_json().

Here is the call graph for this function:
Here is the caller graph for this function: