From 01cb1ee33eb324ae3fcf95266d0d713af9fd00f7 Mon Sep 17 00:00:00 2001 From: Yoshinari Takaoka Date: Fri, 3 Nov 2023 04:59:12 +0900 Subject: [PATCH 1/4] Added json_validate function description. --- reference/json/functions/json-validate.xml | 140 +++++++++++++++++++++ reference/json/versions.xml | 1 + 2 files changed, 141 insertions(+) create mode 100644 reference/json/functions/json-validate.xml diff --git a/reference/json/functions/json-validate.xml b/reference/json/functions/json-validate.xml new file mode 100644 index 000000000000..5f8cbe428dcc --- /dev/null +++ b/reference/json/functions/json-validate.xml @@ -0,0 +1,140 @@ + + + + + json_validate + Validates a JSON string + + + + &reftitle.description; + + booljson_validate + stringjson + intdepth512 + intflags0 + + + Check if a specified string contains a valid json. + Errors during validation can be fetched by using + json_last_error and/or + json_last_error_msg. + + + It's guaranteed that the json valid in json_validate + is also valid in json_decode. + + + + + &reftitle.parameters; + + + + json + + + The json string being analyzed. + + + This function only works with UTF-8 encoded strings. + + &json.implementation.superset; + + + + depth + + + Maximum nesting depth of the structure being decoded. + The value must be greater than 0, + and less than or equal to 2147483647. + + + + + flags + + + Bitmask of + JSON_INVALID_UTF8_IGNORE. + The behaviour of these constants is described on the + JSON constants page. + + + + + + + + + &reftitle.returnvalues; + + Returns &true; if the string passed contains a valid json, otherwise + returns &false;. + + + + + &reftitle.errors; + + If depth is outside the allowed range, + a ValueError is thrown. + + + + + &reftitle.examples; + + + <function>json_validate</function> examples + + +]]> + + &example.outputs; + + + + + + + + + &reftitle.seealso; + + + json_decode + json_last_error + json_last_error_msg + + + + + diff --git a/reference/json/versions.xml b/reference/json/versions.xml index 061749a6b23d..1fbb43574adb 100644 --- a/reference/json/versions.xml +++ b/reference/json/versions.xml @@ -6,6 +6,7 @@ + From 4a3e37a0d3c54f9087b926a27bb9b265b1fc877a Mon Sep 17 00:00:00 2001 From: Yoshinari Takaoka Date: Mon, 6 Nov 2023 20:09:24 +0900 Subject: [PATCH 2/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tim Düsterhus --- reference/json/functions/json-validate.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reference/json/functions/json-validate.xml b/reference/json/functions/json-validate.xml index 5f8cbe428dcc..be9a53063ef3 100644 --- a/reference/json/functions/json-validate.xml +++ b/reference/json/functions/json-validate.xml @@ -3,7 +3,7 @@ json_validate - Validates a JSON string + Checks if a string contains valid JSON @@ -34,7 +34,7 @@ json - The json string being analyzed. + The string to validate. This function only works with UTF-8 encoded strings. @@ -70,7 +70,7 @@ &reftitle.returnvalues; - Returns &true; if the string passed contains a valid json, otherwise + Returns &true; if the given string is syntactically valid JSON, otherwise returns &false;. From f337a4308154f724db190702f40108e9a86ca959 Mon Sep 17 00:00:00 2001 From: Yoshinari Takaoka Date: Mon, 6 Nov 2023 20:13:42 +0900 Subject: [PATCH 3/4] Apply suggestions from code review in description part. --- reference/json/functions/json-validate.xml | 30 ++++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/reference/json/functions/json-validate.xml b/reference/json/functions/json-validate.xml index be9a53063ef3..8b47010ea3c7 100644 --- a/reference/json/functions/json-validate.xml +++ b/reference/json/functions/json-validate.xml @@ -15,15 +15,35 @@ intflags0 - Check if a specified string contains a valid json. - Errors during validation can be fetched by using - json_last_error and/or + Returns whether the given &string; is syntactically valid JSON. + If json_validate returns &true;, json_decode + will successfully decode the given string when using the same + depth and flags. + + + If json_validate returns &false;, the cause + can be retrieved using json_last_error and json_last_error_msg. - It's guaranteed that the json valid in json_validate - is also valid in json_decode. + json_validate uses less memory than + json_decode if the decoded JSON payload is + not used, because it does not need to build the array or + object structure containing the payload. + + + Calling json_validate immediately before + json_decode will unnecessarily parse the string + twice, as json_decode implicitly performs + validation during decoding. + + + json_validate should therefore only be used + if the decode JSON payload is not immediately used and knowing whether + the string contains valid JSON is needed. + + From d9f89f7c195b29153d9dded6f737ac360ddc0b43 Mon Sep 17 00:00:00 2001 From: Yoshinari Takaoka Date: Wed, 15 Nov 2023 03:18:23 +0900 Subject: [PATCH 4/4] fixed flags parameter description. --- reference/json/functions/json-validate.xml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/reference/json/functions/json-validate.xml b/reference/json/functions/json-validate.xml index 8b47010ea3c7..f45929ae944a 100644 --- a/reference/json/functions/json-validate.xml +++ b/reference/json/functions/json-validate.xml @@ -76,10 +76,9 @@ flags - Bitmask of - JSON_INVALID_UTF8_IGNORE. - The behaviour of these constants is described on the - JSON constants page. + Currently only + JSON_INVALID_UTF8_IGNORE + is accepted.