18
18
#include "catalog/pg_cast.h"
19
19
#include "catalog/pg_type.h"
20
20
#include "executor/spi.h"
21
+ #include "funcapi.h"
21
22
#include "lib/stringinfo.h"
22
23
#include "libpq/pqformat.h"
23
24
#include "mb/pg_wchar.h"
@@ -2035,10 +2036,17 @@ json_build_object(PG_FUNCTION_ARGS)
2035
2036
{
2036
2037
int nargs = PG_NARGS ();
2037
2038
int i ;
2038
- Datum arg ;
2039
2039
const char * sep = "" ;
2040
2040
StringInfo result ;
2041
- Oid val_type ;
2041
+ Datum * args ;
2042
+ bool * nulls ;
2043
+ Oid * types ;
2044
+
2045
+ /* fetch argument values to build the object */
2046
+ nargs = extract_variadic_args (fcinfo , 0 , false, & args , & types , & nulls );
2047
+
2048
+ if (nargs < 0 )
2049
+ PG_RETURN_NULL ();
2042
2050
2043
2051
if (nargs % 2 != 0 )
2044
2052
ereport (ERROR ,
@@ -2052,52 +2060,22 @@ json_build_object(PG_FUNCTION_ARGS)
2052
2060
2053
2061
for (i = 0 ; i < nargs ; i += 2 )
2054
2062
{
2055
- /*
2056
- * Note: since json_build_object() is declared as taking type "any",
2057
- * the parser will not do any type conversion on unknown-type literals
2058
- * (that is, undecorated strings or NULLs). Such values will arrive
2059
- * here as type UNKNOWN, which fortunately does not matter to us,
2060
- * since unknownout() works fine.
2061
- */
2062
2063
appendStringInfoString (result , sep );
2063
2064
sep = ", " ;
2064
2065
2065
2066
/* process key */
2066
- val_type = get_fn_expr_argtype (fcinfo -> flinfo , i );
2067
-
2068
- if (val_type == InvalidOid )
2069
- ereport (ERROR ,
2070
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
2071
- errmsg ("could not determine data type for argument %d" ,
2072
- i + 1 )));
2073
-
2074
- if (PG_ARGISNULL (i ))
2067
+ if (nulls [i ])
2075
2068
ereport (ERROR ,
2076
2069
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
2077
2070
errmsg ("argument %d cannot be null" , i + 1 ),
2078
2071
errhint ("Object keys should be text." )));
2079
2072
2080
- arg = PG_GETARG_DATUM (i );
2081
-
2082
- add_json (arg , false, result , val_type , true);
2073
+ add_json (args [i ], false, result , types [i ], true);
2083
2074
2084
2075
appendStringInfoString (result , " : " );
2085
2076
2086
2077
/* process value */
2087
- val_type = get_fn_expr_argtype (fcinfo -> flinfo , i + 1 );
2088
-
2089
- if (val_type == InvalidOid )
2090
- ereport (ERROR ,
2091
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
2092
- errmsg ("could not determine data type for argument %d" ,
2093
- i + 2 )));
2094
-
2095
- if (PG_ARGISNULL (i + 1 ))
2096
- arg = (Datum ) 0 ;
2097
- else
2098
- arg = PG_GETARG_DATUM (i + 1 );
2099
-
2100
- add_json (arg , PG_ARGISNULL (i + 1 ), result , val_type , false);
2078
+ add_json (args [i + 1 ], nulls [i + 1 ], result , types [i + 1 ], false);
2101
2079
}
2102
2080
2103
2081
appendStringInfoChar (result , '}' );
@@ -2120,43 +2098,29 @@ json_build_object_noargs(PG_FUNCTION_ARGS)
2120
2098
Datum
2121
2099
json_build_array (PG_FUNCTION_ARGS )
2122
2100
{
2123
- int nargs = PG_NARGS () ;
2101
+ int nargs ;
2124
2102
int i ;
2125
- Datum arg ;
2126
2103
const char * sep = "" ;
2127
2104
StringInfo result ;
2128
- Oid val_type ;
2105
+ Datum * args ;
2106
+ bool * nulls ;
2107
+ Oid * types ;
2108
+
2109
+ /* fetch argument values to build the array */
2110
+ nargs = extract_variadic_args (fcinfo , 0 , false, & args , & types , & nulls );
2111
+
2112
+ if (nargs < 0 )
2113
+ PG_RETURN_NULL ();
2129
2114
2130
2115
result = makeStringInfo ();
2131
2116
2132
2117
appendStringInfoChar (result , '[' );
2133
2118
2134
2119
for (i = 0 ; i < nargs ; i ++ )
2135
2120
{
2136
- /*
2137
- * Note: since json_build_array() is declared as taking type "any",
2138
- * the parser will not do any type conversion on unknown-type literals
2139
- * (that is, undecorated strings or NULLs). Such values will arrive
2140
- * here as type UNKNOWN, which fortunately does not matter to us,
2141
- * since unknownout() works fine.
2142
- */
2143
2121
appendStringInfoString (result , sep );
2144
2122
sep = ", " ;
2145
-
2146
- val_type = get_fn_expr_argtype (fcinfo -> flinfo , i );
2147
-
2148
- if (val_type == InvalidOid )
2149
- ereport (ERROR ,
2150
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
2151
- errmsg ("could not determine data type for argument %d" ,
2152
- i + 1 )));
2153
-
2154
- if (PG_ARGISNULL (i ))
2155
- arg = (Datum ) 0 ;
2156
- else
2157
- arg = PG_GETARG_DATUM (i );
2158
-
2159
- add_json (arg , PG_ARGISNULL (i ), result , val_type , false);
2123
+ add_json (args [i ], nulls [i ], result , types [i ], false);
2160
2124
}
2161
2125
2162
2126
appendStringInfoChar (result , ']' );
0 commit comments