From fc1dc8cbdc07fa917aa0ff0318ff5e984a31ba8f Mon Sep 17 00:00:00 2001 From: MashinaMashina <31430016+MashinaMashina@users.noreply.github.com> Date: Sun, 1 Mar 2020 21:50:34 +0400 Subject: [PATCH 1/4] Fix JSON error on recursion in data Bugs: https://github.com/codeigniter4/CodeIgniter4/issues/2434 and https://github.com/codeigniter4/CodeIgniter4/issues/2586 --- system/Format/JSONFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Format/JSONFormatter.php b/system/Format/JSONFormatter.php index 50caf3930f9d..f428a8266917 100644 --- a/system/Format/JSONFormatter.php +++ b/system/Format/JSONFormatter.php @@ -56,7 +56,7 @@ class JSONFormatter implements FormatterInterface */ public function format($data) { - $options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES; + $options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR; $options = ENVIRONMENT === 'production' ? $options : $options | JSON_PRETTY_PRINT; From 03e37e8c0c0e232cb9c8a64eed34409afc22c5ca Mon Sep 17 00:00:00 2001 From: MashinaMashina <31430016+MashinaMashina@users.noreply.github.com> Date: Sun, 1 Mar 2020 22:13:49 +0400 Subject: [PATCH 2/4] Check result --- system/Format/JSONFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Format/JSONFormatter.php b/system/Format/JSONFormatter.php index f428a8266917..cc53485ba3a1 100644 --- a/system/Format/JSONFormatter.php +++ b/system/Format/JSONFormatter.php @@ -62,7 +62,7 @@ public function format($data) $result = json_encode($data, $options, 512); - if (json_last_error() !== JSON_ERROR_NONE) + if ( ! $result && json_last_error() !== JSON_ERROR_NONE) { throw FormatException::forInvalidJSON(json_last_error_msg()); } From 5af60ab1e7bf1ff19adf29c8889ada02330497ea Mon Sep 17 00:00:00 2001 From: MashinaMashina <31430016+MashinaMashina@users.noreply.github.com> Date: Sun, 8 Mar 2020 20:17:47 +0400 Subject: [PATCH 3/4] Update system/Format/JSONFormatter.php Co-Authored-By: Michal Sniatala --- system/Format/JSONFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Format/JSONFormatter.php b/system/Format/JSONFormatter.php index cc53485ba3a1..1c496a0620cd 100644 --- a/system/Format/JSONFormatter.php +++ b/system/Format/JSONFormatter.php @@ -62,7 +62,7 @@ public function format($data) $result = json_encode($data, $options, 512); - if ( ! $result && json_last_error() !== JSON_ERROR_NONE) + if (! in_array(json_last_error(), [JSON_ERROR_NONE, JSON_ERROR_RECURSION])) { throw FormatException::forInvalidJSON(json_last_error_msg()); } From 99a65a8e21a7740cba90b9de1713518950bedf9f Mon Sep 17 00:00:00 2001 From: MashinaMashina <31430016+MashinaMashina@users.noreply.github.com> Date: Sun, 8 Mar 2020 20:18:19 +0400 Subject: [PATCH 4/4] Text formats in JSONFormatter --- system/Format/JSONFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Format/JSONFormatter.php b/system/Format/JSONFormatter.php index 1c496a0620cd..823f3ca240e6 100644 --- a/system/Format/JSONFormatter.php +++ b/system/Format/JSONFormatter.php @@ -62,7 +62,7 @@ public function format($data) $result = json_encode($data, $options, 512); - if (! in_array(json_last_error(), [JSON_ERROR_NONE, JSON_ERROR_RECURSION])) + if ( ! in_array(json_last_error(), [JSON_ERROR_NONE, JSON_ERROR_RECURSION])) { throw FormatException::forInvalidJSON(json_last_error_msg()); }