summaryrefslogtreecommitdiff
path: root/json.c
diff options
context:
space:
mode:
Diffstat (limited to 'json.c')
-rw-r--r--json.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/json.c b/json.c
index eed3665..c7a62df 100644
--- a/json.c
+++ b/json.c
@@ -351,6 +351,10 @@ char *json_decode_string(const char **sp, size_t *length, bool strict);
/*
* json_validate
* Make sure the given UTF-8 string is valid JSON.
+ *
+ * TODO: Consider making a dedicated function for this so we don't have to
+ * convert to UTF-8, build a JSON node, then free both
+ * whenever we need to validate (such as in json_in and json_recv).
*/
bool
json_validate(const char *str)
@@ -364,6 +368,27 @@ json_validate(const char *str)
}
/*
+ * json_validate_server_encoded
+ * Variant of json_validate that takes a server-encoded string
+ * rather than a UTF-8 string.
+ *
+ * Note that a dedicated json_validate (described in the TODO above)
+ * would be able to handle both encodings natively, since both are
+ * ASCII-compatible.
+ */
+bool
+json_validate_server_encoded(const char *str)
+{
+ char *str_utf8 = server_to_utf8(str, strlen(str));
+ bool result = json_validate(str_utf8);
+
+ if (str_utf8 != str)
+ pfree(str_utf8);
+
+ return result;
+}
+
+/*
* json_decode
* Convert a JSON-encoded string to a JSON node.
* @str must be valid UTF-8.