PostGIS 3.7.0dev-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 422 of file lwgeom_out_geojson.c.

425{
426 char *outputstr;
427 text *jsontext;
428
429 check_stack_depth();
430
431 /* callers are expected to ensure that null keys are not passed in */
432 Assert(!(key_scalar && is_null));
433
434 if (is_null)
435 {
436 appendStringInfoString(result, "null");
437 return;
438 }
439
440 if (key_scalar &&
441 (tcategory == JSONTYPE_ARRAY ||
442 tcategory == JSONTYPE_COMPOSITE ||
443 tcategory == JSONTYPE_JSON ||
444 tcategory == JSONTYPE_CAST))
445 ereport(ERROR,
446 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
447 errmsg("key value must be scalar, not array, composite, or json")));
448
449 switch (tcategory)
450 {
451 case JSONTYPE_ARRAY:
452 array_to_json_internal(val, result, false);
453 break;
455 composite_to_json(val, result, false);
456 break;
457 case JSONTYPE_BOOL:
458 outputstr = DatumGetBool(val) ? "true" : "false";
459 if (key_scalar)
460 escape_json(result, outputstr);
461 else
462 appendStringInfoString(result, outputstr);
463 break;
464 case JSONTYPE_NUMERIC:
465 outputstr = OidOutputFunctionCall(outfuncoid, val);
466
467 /*
468 * Don't call escape_json for a non-key if it's a valid JSON
469 * number.
470 */
471 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
472 appendStringInfoString(result, outputstr);
473 else
474 escape_json(result, outputstr);
475 pfree(outputstr);
476 break;
477 case JSONTYPE_DATE:
478 {
479 char buf[MAXDATELEN + 1];
480
481 postgis_JsonEncodeDateTime(buf, val, DATEOID);
482 appendStringInfo(result, "\"%s\"", buf);
483 }
484 break;
486 {
487 char buf[MAXDATELEN + 1];
488
489 postgis_JsonEncodeDateTime(buf, val, TIMESTAMPOID);
490 appendStringInfo(result, "\"%s\"", buf);
491 }
492 break;
494 {
495 char buf[MAXDATELEN + 1];
496
497 postgis_JsonEncodeDateTime(buf, val, TIMESTAMPTZOID);
498 appendStringInfo(result, "\"%s\"", buf);
499 }
500 break;
501 case JSONTYPE_JSON:
502 /* JSON and JSONB output will already be escaped */
503 outputstr = OidOutputFunctionCall(outfuncoid, val);
504 appendStringInfoString(result, outputstr);
505 pfree(outputstr);
506 break;
507 case JSONTYPE_CAST:
508 /* outfuncoid refers to a cast function, not an output function */
509 jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
510 outputstr = text_to_cstring(jsontext);
511 appendStringInfoString(result, outputstr);
512 pfree(outputstr);
513 pfree(jsontext);
514 break;
515 default:
516 outputstr = OidOutputFunctionCall(outfuncoid, val);
517 escape_json(result, outputstr);
518 pfree(outputstr);
519 break;
520 }
521}
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: