@@ -1869,10 +1869,10 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
1869
1869
if (PG_ARGISNULL (0 ))
1870
1870
{
1871
1871
/*
1872
- * Make this StringInfo in a context where it will persist for the
1873
- * duration off the aggregate call. It's only needed for this initial
1874
- * piece , as the StringInfo routines make sure they use the right
1875
- * context to enlarge the object if necessary.
1872
+ * Make the StringInfo in a context where it will persist for the
1873
+ * duration of the aggregate call. Switching context is only needed
1874
+ * for this initial step , as the StringInfo routines make sure they
1875
+ * use the right context to enlarge the object if necessary.
1876
1876
*/
1877
1877
oldcontext = MemoryContextSwitchTo (aggcontext );
1878
1878
state = makeStringInfo ();
@@ -1886,56 +1886,43 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
1886
1886
appendStringInfoString (state , ", " );
1887
1887
}
1888
1888
1889
- if (PG_ARGISNULL (1 ))
1890
- ereport (ERROR ,
1891
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1892
- errmsg ("field name must not be null" )));
1893
-
1894
- val_type = get_fn_expr_argtype (fcinfo -> flinfo , 1 );
1895
-
1896
1889
/*
1897
- * turn a constant (more or less literal) value that's of unknown type
1898
- * into text. Unknowns come in as a cstring pointer.
1890
+ * Note: since json_object_agg() is declared as taking type "any", the
1891
+ * parser will not do any type conversion on unknown-type literals (that
1892
+ * is, undecorated strings or NULLs). Such values will arrive here as
1893
+ * type UNKNOWN, which fortunately does not matter to us, since
1894
+ * unknownout() works fine.
1899
1895
*/
1900
- if (val_type == UNKNOWNOID && get_fn_expr_arg_stable (fcinfo -> flinfo , 1 ))
1901
- {
1902
- val_type = TEXTOID ;
1903
- arg = CStringGetTextDatum (PG_GETARG_POINTER (1 ));
1904
- }
1905
- else
1906
- {
1907
- arg = PG_GETARG_DATUM (1 );
1908
- }
1896
+ val_type = get_fn_expr_argtype (fcinfo -> flinfo , 1 );
1909
1897
1910
- if (val_type == InvalidOid || val_type == UNKNOWNOID )
1898
+ if (val_type == InvalidOid )
1911
1899
ereport (ERROR ,
1912
1900
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1913
1901
errmsg ("could not determine data type for argument %d" , 1 )));
1914
1902
1903
+ if (PG_ARGISNULL (1 ))
1904
+ ereport (ERROR ,
1905
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1906
+ errmsg ("field name must not be null" )));
1907
+
1908
+ arg = PG_GETARG_DATUM (1 );
1909
+
1915
1910
add_json (arg , false, state , val_type , true);
1916
1911
1917
1912
appendStringInfoString (state , " : " );
1918
1913
1919
1914
val_type = get_fn_expr_argtype (fcinfo -> flinfo , 2 );
1920
- /* see comments above */
1921
- if (val_type == UNKNOWNOID && get_fn_expr_arg_stable (fcinfo -> flinfo , 2 ))
1922
- {
1923
- val_type = TEXTOID ;
1924
- if (PG_ARGISNULL (2 ))
1925
- arg = (Datum ) 0 ;
1926
- else
1927
- arg = CStringGetTextDatum (PG_GETARG_POINTER (2 ));
1928
- }
1929
- else
1930
- {
1931
- arg = PG_GETARG_DATUM (2 );
1932
- }
1933
1915
1934
- if (val_type == InvalidOid || val_type == UNKNOWNOID )
1916
+ if (val_type == InvalidOid )
1935
1917
ereport (ERROR ,
1936
1918
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1937
1919
errmsg ("could not determine data type for argument %d" , 2 )));
1938
1920
1921
+ if (PG_ARGISNULL (2 ))
1922
+ arg = (Datum ) 0 ;
1923
+ else
1924
+ arg = PG_GETARG_DATUM (2 );
1925
+
1939
1926
add_json (arg , PG_ARGISNULL (2 ), state , val_type , false);
1940
1927
1941
1928
PG_RETURN_POINTER (state );
@@ -1972,7 +1959,7 @@ json_build_object(PG_FUNCTION_ARGS)
1972
1959
int nargs = PG_NARGS ();
1973
1960
int i ;
1974
1961
Datum arg ;
1975
- char * sep = "" ;
1962
+ const char * sep = "" ;
1976
1963
StringInfo result ;
1977
1964
Oid val_type ;
1978
1965
@@ -1988,60 +1975,51 @@ json_build_object(PG_FUNCTION_ARGS)
1988
1975
1989
1976
for (i = 0 ; i < nargs ; i += 2 )
1990
1977
{
1978
+ /*
1979
+ * Note: since json_build_object() is declared as taking type "any",
1980
+ * the parser will not do any type conversion on unknown-type literals
1981
+ * (that is, undecorated strings or NULLs). Such values will arrive
1982
+ * here as type UNKNOWN, which fortunately does not matter to us,
1983
+ * since unknownout() works fine.
1984
+ */
1985
+ appendStringInfoString (result , sep );
1986
+ sep = ", " ;
1987
+
1991
1988
/* process key */
1989
+ val_type = get_fn_expr_argtype (fcinfo -> flinfo , i );
1990
+
1991
+ if (val_type == InvalidOid )
1992
+ ereport (ERROR ,
1993
+ (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1994
+ errmsg ("could not determine data type for argument %d" ,
1995
+ i + 1 )));
1992
1996
1993
1997
if (PG_ARGISNULL (i ))
1994
1998
ereport (ERROR ,
1995
1999
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
1996
2000
errmsg ("argument %d cannot be null" , i + 1 ),
1997
2001
errhint ("Object keys should be text." )));
1998
- val_type = get_fn_expr_argtype (fcinfo -> flinfo , i );
1999
2002
2000
- /*
2001
- * turn a constant (more or less literal) value that's of unknown type
2002
- * into text. Unknowns come in as a cstring pointer.
2003
- */
2004
- if (val_type == UNKNOWNOID && get_fn_expr_arg_stable (fcinfo -> flinfo , i ))
2005
- {
2006
- val_type = TEXTOID ;
2007
- arg = CStringGetTextDatum (PG_GETARG_POINTER (i ));
2008
- }
2009
- else
2010
- {
2011
- arg = PG_GETARG_DATUM (i );
2012
- }
2013
- if (val_type == InvalidOid || val_type == UNKNOWNOID )
2014
- ereport (ERROR ,
2015
- (errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
2016
- errmsg ("could not determine data type for argument %d" ,
2017
- i + 1 )));
2018
- appendStringInfoString (result , sep );
2019
- sep = ", " ;
2003
+ arg = PG_GETARG_DATUM (i );
2004
+
2020
2005
add_json (arg , false, result , val_type , true);
2021
2006
2022
2007
appendStringInfoString (result , " : " );
2023
2008
2024
2009
/* process value */
2025
-
2026
2010
val_type = get_fn_expr_argtype (fcinfo -> flinfo , i + 1 );
2027
- /* see comments above */
2028
- if (val_type == UNKNOWNOID && get_fn_expr_arg_stable (fcinfo -> flinfo , i + 1 ))
2029
- {
2030
- val_type = TEXTOID ;
2031
- if (PG_ARGISNULL (i + 1 ))
2032
- arg = (Datum ) 0 ;
2033
- else
2034
- arg = CStringGetTextDatum (PG_GETARG_POINTER (i + 1 ));
2035
- }
2036
- else
2037
- {
2038
- arg = PG_GETARG_DATUM (i + 1 );
2039
- }
2040
- if (val_type == InvalidOid || val_type == UNKNOWNOID )
2011
+
2012
+ if (val_type == InvalidOid )
2041
2013
ereport (ERROR ,
2042
2014
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
2043
2015
errmsg ("could not determine data type for argument %d" ,
2044
2016
i + 2 )));
2017
+
2018
+ if (PG_ARGISNULL (i + 1 ))
2019
+ arg = (Datum ) 0 ;
2020
+ else
2021
+ arg = PG_GETARG_DATUM (i + 1 );
2022
+
2045
2023
add_json (arg , PG_ARGISNULL (i + 1 ), result , val_type , false);
2046
2024
}
2047
2025
@@ -2068,45 +2046,45 @@ json_build_array(PG_FUNCTION_ARGS)
2068
2046
int nargs = PG_NARGS ();
2069
2047
int i ;
2070
2048
Datum arg ;
2071
- char * sep = "" ;
2049
+ const char * sep = "" ;
2072
2050
StringInfo result ;
2073
2051
Oid val_type ;
2074
2052
2075
-
2076
2053
result = makeStringInfo ();
2077
2054
2078
2055
appendStringInfoChar (result , '[' );
2079
2056
2080
2057
for (i = 0 ; i < nargs ; i ++ )
2081
2058
{
2059
+ /*
2060
+ * Note: since json_build_array() is declared as taking type "any",
2061
+ * the parser will not do any type conversion on unknown-type literals
2062
+ * (that is, undecorated strings or NULLs). Such values will arrive
2063
+ * here as type UNKNOWN, which fortunately does not matter to us,
2064
+ * since unknownout() works fine.
2065
+ */
2066
+ appendStringInfoString (result , sep );
2067
+ sep = ", " ;
2068
+
2082
2069
val_type = get_fn_expr_argtype (fcinfo -> flinfo , i );
2083
- arg = PG_GETARG_DATUM (i + 1 );
2084
- /* see comments in json_build_object above */
2085
- if (val_type == UNKNOWNOID && get_fn_expr_arg_stable (fcinfo -> flinfo , i ))
2086
- {
2087
- val_type = TEXTOID ;
2088
- if (PG_ARGISNULL (i ))
2089
- arg = (Datum ) 0 ;
2090
- else
2091
- arg = CStringGetTextDatum (PG_GETARG_POINTER (i ));
2092
- }
2093
- else
2094
- {
2095
- arg = PG_GETARG_DATUM (i );
2096
- }
2097
- if (val_type == InvalidOid || val_type == UNKNOWNOID )
2070
+
2071
+ if (val_type == InvalidOid )
2098
2072
ereport (ERROR ,
2099
2073
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
2100
2074
errmsg ("could not determine data type for argument %d" ,
2101
2075
i + 1 )));
2102
- appendStringInfoString (result , sep );
2103
- sep = ", " ;
2076
+
2077
+ if (PG_ARGISNULL (i ))
2078
+ arg = (Datum ) 0 ;
2079
+ else
2080
+ arg = PG_GETARG_DATUM (i );
2081
+
2104
2082
add_json (arg , PG_ARGISNULL (i ), result , val_type , false);
2105
2083
}
2084
+
2106
2085
appendStringInfoChar (result , ']' );
2107
2086
2108
2087
PG_RETURN_TEXT_P (cstring_to_text_with_len (result -> data , result -> len ));
2109
-
2110
2088
}
2111
2089
2112
2090
/*
0 commit comments