aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/json.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2014-08-09 16:35:29 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2014-08-09 16:35:29 -0400
commit495cadda5ed55f63db9a91d174b6fad0e1816204 (patch)
tree995668755616c0af93c5d8176acc27c0a36016d0 /src/backend/utils/adt/json.c
parent9da86753735ab89b0ee685aea985b25c4218ca0b (diff)
downloadpostgresql-495cadda5ed55f63db9a91d174b6fad0e1816204.tar.gz
postgresql-495cadda5ed55f63db9a91d174b6fad0e1816204.zip
Further cleanup of JSON-specific error messages.
Fix an obvious typo in json_build_object()'s complaint about invalid number of arguments, and make the errhint a bit more sensible too. Per discussion about how to word the improved hint, change the few places in the documentation that refer to JSON object field names as "names" to say "keys" instead, since that's what we've said in the vast majority of places in the docs. Arguably "name" is more correct, since that's the terminology used in RFC 7159; but we're stuck with "key" in view of the naming of json_object_keys() so let's at least be self-consistent. I adjusted a few code comments to match this as well, and failed to resist the temptation to clean up some odd whitespace choices in the same area, as well as a useless duplicate PG_ARGISNULL() check. There's still quite a bit of code that uses the phrase "field name" in non-user- visible ways, so I left those usages alone.
Diffstat (limited to 'src/backend/utils/adt/json.c')
-rw-r--r--src/backend/utils/adt/json.c26
1 files changed, 9 insertions, 17 deletions
diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 0c55e9a1a2f..d1dc87e679f 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -354,8 +354,9 @@ static void
parse_object_field(JsonLexContext *lex, JsonSemAction *sem)
{
/*
- * an object field is "fieldname" : value where value can be a scalar,
- * object or array
+ * An object field is "fieldname" : value where value can be a scalar,
+ * object or array. Note: in user-facing docs and error messages, we
+ * generally call a field name a "key".
*/
char *fname = NULL; /* keep compiler quiet */
@@ -1890,7 +1891,6 @@ json_object_agg_transfn(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("field name must not be null")));
-
val_type = get_fn_expr_argtype(fcinfo->flinfo, 1);
/*
@@ -1976,12 +1976,11 @@ json_build_object(PG_FUNCTION_ARGS)
StringInfo result;
Oid val_type;
-
if (nargs % 2 != 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
- errmsg("invalid number or arguments"),
- errhint("Object must be matched key value pairs.")));
+ errmsg("argument list must have even number of elements"),
+ errhint("The arguments of json_build_object() must consist of alternating keys and values.")));
result = makeStringInfo();
@@ -1989,7 +1988,6 @@ json_build_object(PG_FUNCTION_ARGS)
for (i = 0; i < nargs; i += 2)
{
-
/* process key */
if (PG_ARGISNULL(i))
@@ -2006,10 +2004,7 @@ json_build_object(PG_FUNCTION_ARGS)
if (val_type == UNKNOWNOID && get_fn_expr_arg_stable(fcinfo->flinfo, i))
{
val_type = TEXTOID;
- if (PG_ARGISNULL(i))
- arg = (Datum) 0;
- else
- arg = CStringGetTextDatum(PG_GETARG_POINTER(i));
+ arg = CStringGetTextDatum(PG_GETARG_POINTER(i));
}
else
{
@@ -2048,12 +2043,11 @@ json_build_object(PG_FUNCTION_ARGS)
errmsg("could not determine data type for argument %d",
i + 2)));
add_json(arg, PG_ARGISNULL(i + 1), result, val_type, false);
-
}
+
appendStringInfoChar(result, '}');
PG_RETURN_TEXT_P(cstring_to_text_with_len(result->data, result->len));
-
}
/*
@@ -2127,9 +2121,8 @@ json_build_array_noargs(PG_FUNCTION_ARGS)
/*
* SQL function json_object(text[])
*
- * take a one or two dimensional array of text as name vale pairs
+ * take a one or two dimensional array of text as key/value pairs
* for a json object.
- *
*/
Datum
json_object(PG_FUNCTION_ARGS)
@@ -2219,7 +2212,7 @@ json_object(PG_FUNCTION_ARGS)
/*
* SQL function json_object(text[], text[])
*
- * take separate name and value arrays of text to construct a json object
+ * take separate key and value arrays of text to construct a json object
* pairwise.
*/
Datum
@@ -2299,7 +2292,6 @@ json_object_two_arg(PG_FUNCTION_ARGS)
pfree(result.data);
PG_RETURN_TEXT_P(rval);
-
}