PostGIS 3.0.6dev-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 330 of file lwgeom_out_geojson.c.

333{
334 char *outputstr;
335 text *jsontext;
336
337 check_stack_depth();
338
339 /* callers are expected to ensure that null keys are not passed in */
340 Assert(!(key_scalar && is_null));
341
342 if (is_null)
343 {
344 appendStringInfoString(result, "null");
345 return;
346 }
347
348 if (key_scalar &&
349 (tcategory == JSONTYPE_ARRAY ||
350 tcategory == JSONTYPE_COMPOSITE ||
351 tcategory == JSONTYPE_JSON ||
352 tcategory == JSONTYPE_CAST))
353 ereport(ERROR,
354 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
355 errmsg("key value must be scalar, not array, composite, or json")));
356
357 switch (tcategory)
358 {
359 case JSONTYPE_ARRAY:
360 array_to_json_internal(val, result, false);
361 break;
363 composite_to_json(val, result, false);
364 break;
365 case JSONTYPE_BOOL:
366 outputstr = DatumGetBool(val) ? "true" : "false";
367 if (key_scalar)
368 escape_json(result, outputstr);
369 else
370 appendStringInfoString(result, outputstr);
371 break;
372 case JSONTYPE_NUMERIC:
373 outputstr = OidOutputFunctionCall(outfuncoid, val);
374
375 /*
376 * Don't call escape_json for a non-key if it's a valid JSON
377 * number.
378 */
379 if (!key_scalar && IsValidJsonNumber(outputstr, strlen(outputstr)))
380 appendStringInfoString(result, outputstr);
381 else
382 escape_json(result, outputstr);
383 pfree(outputstr);
384 break;
385 case JSONTYPE_DATE:
386 {
387 char buf[MAXDATELEN + 1];
388
389 postgis_JsonEncodeDateTime(buf, val, DATEOID);
390 appendStringInfo(result, "\"%s\"", buf);
391 }
392 break;
394 {
395 char buf[MAXDATELEN + 1];
396
397 postgis_JsonEncodeDateTime(buf, val, TIMESTAMPOID);
398 appendStringInfo(result, "\"%s\"", buf);
399 }
400 break;
402 {
403 char buf[MAXDATELEN + 1];
404
405 postgis_JsonEncodeDateTime(buf, val, TIMESTAMPTZOID);
406 appendStringInfo(result, "\"%s\"", buf);
407 }
408 break;
409 case JSONTYPE_JSON:
410 /* JSON and JSONB output will already be escaped */
411 outputstr = OidOutputFunctionCall(outfuncoid, val);
412 appendStringInfoString(result, outputstr);
413 pfree(outputstr);
414 break;
415 case JSONTYPE_CAST:
416 /* outfuncoid refers to a cast function, not an output function */
417 jsontext = DatumGetTextPP(OidFunctionCall1(outfuncoid, val));
418 outputstr = text_to_cstring(jsontext);
419 appendStringInfoString(result, outputstr);
420 pfree(outputstr);
421 pfree(jsontext);
422 break;
423 default:
424 outputstr = OidOutputFunctionCall(outfuncoid, val);
425 escape_json(result, outputstr);
426 pfree(outputstr);
427 break;
428 }
429}
@ 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)
char * text_to_cstring(const text *textptr)

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 text_to_cstring().

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: