From 436ab321a8ed9e2053b778c869428ab10388c13d Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Wed, 15 Jan 2020 09:26:04 -0500
Subject: [PATCH v2 1/4] Adjust src/include/utils/jsonapi.h so it's not
 backend-only.

The major change here is that we no longer include jsonb.h into
jsonapi.h. The reason that was necessary is that jsonapi.h included
several prototypes functions in jsonfuncs.c that depend on the Jsonb
type. Move those prototypes to a new header, jsonfuncs.h, and include
it where needed.

The other change is that JsonEncodeDateTime is now declared in
json.h rather than jsonapi.h.

Taken together, these steps eliminate all dependencies of jsonapi.h
on backend-only data types and header files, so that it can
potentially be included in frontend code.
---
 src/backend/tsearch/to_tsany.c     |  1 +
 src/backend/tsearch/wparser.c      |  1 +
 src/backend/utils/adt/jsonb_util.c |  1 +
 src/backend/utils/adt/jsonfuncs.c  |  1 +
 src/include/utils/json.h           |  2 ++
 src/include/utils/jsonapi.h        | 33 --------------------
 src/include/utils/jsonfuncs.h      | 49 ++++++++++++++++++++++++++++++
 7 files changed, 55 insertions(+), 33 deletions(-)
 create mode 100644 src/include/utils/jsonfuncs.h

diff --git a/src/backend/tsearch/to_tsany.c b/src/backend/tsearch/to_tsany.c
index cc694cda8c..adf181c191 100644
--- a/src/backend/tsearch/to_tsany.c
+++ b/src/backend/tsearch/to_tsany.c
@@ -17,6 +17,7 @@
 #include "tsearch/ts_utils.h"
 #include "utils/builtins.h"
 #include "utils/jsonapi.h"
+#include "utils/jsonfuncs.h"
 
 
 typedef struct MorphOpaque
diff --git a/src/backend/tsearch/wparser.c b/src/backend/tsearch/wparser.c
index 6b5960ecc1..c7499a94ac 100644
--- a/src/backend/tsearch/wparser.c
+++ b/src/backend/tsearch/wparser.c
@@ -21,6 +21,7 @@
 #include "tsearch/ts_utils.h"
 #include "utils/builtins.h"
 #include "utils/jsonapi.h"
+#include "utils/jsonfuncs.h"
 #include "utils/varlena.h"
 
 /******sql-level interface******/
diff --git a/src/backend/utils/adt/jsonb_util.c b/src/backend/utils/adt/jsonb_util.c
index 7c9da701dd..b33c3ef43c 100644
--- a/src/backend/utils/adt/jsonb_util.c
+++ b/src/backend/utils/adt/jsonb_util.c
@@ -19,6 +19,7 @@
 #include "utils/builtins.h"
 #include "utils/datetime.h"
 #include "utils/hashutils.h"
+#include "utils/json.h"
 #include "utils/jsonapi.h"
 #include "utils/jsonb.h"
 #include "utils/memutils.h"
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index 4b5a0214dc..165e6a83ec 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -29,6 +29,7 @@
 #include "utils/json.h"
 #include "utils/jsonapi.h"
 #include "utils/jsonb.h"
+#include "utils/jsonfuncs.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
 #include "utils/syscache.h"
diff --git a/src/include/utils/json.h b/src/include/utils/json.h
index 20b5294491..4345fbdc31 100644
--- a/src/include/utils/json.h
+++ b/src/include/utils/json.h
@@ -18,5 +18,7 @@
 
 /* functions in json.c */
 extern void escape_json(StringInfo buf, const char *str);
+extern char *JsonEncodeDateTime(char *buf, Datum value, Oid typid,
+								const int *tzp);
 
 #endif							/* JSON_H */
diff --git a/src/include/utils/jsonapi.h b/src/include/utils/jsonapi.h
index f72f1cefd5..1190947476 100644
--- a/src/include/utils/jsonapi.h
+++ b/src/include/utils/jsonapi.h
@@ -14,7 +14,6 @@
 #ifndef JSONAPI_H
 #define JSONAPI_H
 
-#include "jsonb.h"
 #include "lib/stringinfo.h"
 
 typedef enum
@@ -132,36 +131,4 @@ extern JsonLexContext *makeJsonLexContextCstringLen(char *json,
  */
 extern bool IsValidJsonNumber(const char *str, int len);
 
-/*
- * Flag types for iterate_json(b)_values to specify what elements from a
- * json(b) document we want to iterate.
- */
-typedef enum JsonToIndex
-{
-	jtiKey = 0x01,
-	jtiString = 0x02,
-	jtiNumeric = 0x04,
-	jtiBool = 0x08,
-	jtiAll = jtiKey | jtiString | jtiNumeric | jtiBool
-} JsonToIndex;
-
-/* an action that will be applied to each value in iterate_json(b)_values functions */
-typedef void (*JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len);
-
-/* an action that will be applied to each value in transform_json(b)_values functions */
-typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len);
-
-extern uint32 parse_jsonb_index_flags(Jsonb *jb);
-extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
-								 JsonIterateStringValuesAction action);
-extern void iterate_json_values(text *json, uint32 flags, void *action_state,
-								JsonIterateStringValuesAction action);
-extern Jsonb *transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
-											JsonTransformStringValuesAction transform_action);
-extern text *transform_json_string_values(text *json, void *action_state,
-										  JsonTransformStringValuesAction transform_action);
-
-extern char *JsonEncodeDateTime(char *buf, Datum value, Oid typid,
-								const int *tzp);
-
 #endif							/* JSONAPI_H */
diff --git a/src/include/utils/jsonfuncs.h b/src/include/utils/jsonfuncs.h
new file mode 100644
index 0000000000..19f087ccae
--- /dev/null
+++ b/src/include/utils/jsonfuncs.h
@@ -0,0 +1,49 @@
+/*-------------------------------------------------------------------------
+ *
+ * jsonfuncs.h
+ *	  Functions to process JSON data types.
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * src/include/utils/jsonapi.h
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#ifndef JSONFUNCS_H
+#define JSONFUNCS_H
+
+#include "utils/jsonapi.h"
+#include "utils/jsonb.h"
+
+/*
+ * Flag types for iterate_json(b)_values to specify what elements from a
+ * json(b) document we want to iterate.
+ */
+typedef enum JsonToIndex
+{
+	jtiKey = 0x01,
+	jtiString = 0x02,
+	jtiNumeric = 0x04,
+	jtiBool = 0x08,
+	jtiAll = jtiKey | jtiString | jtiNumeric | jtiBool
+} JsonToIndex;
+
+/* an action that will be applied to each value in iterate_json(b)_values functions */
+typedef void (*JsonIterateStringValuesAction) (void *state, char *elem_value, int elem_len);
+
+/* an action that will be applied to each value in transform_json(b)_values functions */
+typedef text *(*JsonTransformStringValuesAction) (void *state, char *elem_value, int elem_len);
+
+extern uint32 parse_jsonb_index_flags(Jsonb *jb);
+extern void iterate_jsonb_values(Jsonb *jb, uint32 flags, void *state,
+								 JsonIterateStringValuesAction action);
+extern void iterate_json_values(text *json, uint32 flags, void *action_state,
+								JsonIterateStringValuesAction action);
+extern Jsonb *transform_jsonb_string_values(Jsonb *jsonb, void *action_state,
+											JsonTransformStringValuesAction transform_action);
+extern text *transform_json_string_values(text *json, void *action_state,
+										  JsonTransformStringValuesAction transform_action);
+
+#endif
-- 
2.17.2 (Apple Git-113)

