From 7ff6447d8dac12fb18d909091d07f553b1109d46 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Mon, 9 Mar 2026 00:17:13 +0100 Subject: [PATCH] Add function to print JSON data without encoding it in bulk (to reduce memory consumption) --- src/MD_STD.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/MD_STD.php b/src/MD_STD.php index 1a98fc6..9335779 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -1087,4 +1087,44 @@ final class MD_STD { return $lowest_option; } + + /** + * Prints an array JSON-encoded. Does so entry by entry to reduce memory consumption. + * + * @param array $input Input. + * @param boolean $html_encode HTML encodes. + * + * @return void + */ + public static function print_json_encoded(array &$input, bool $html_encode):void { + + $last = count($input); + if (array_is_list($input)) { + + echo '['; + $i = 0; + foreach ($input as $value) { + if ($html_encode === true) echo htmlspecialchars(json_encode($value)); + else echo json_encode($value); + $i++; + if ($i !== $last) echo ','; + } + echo ']'; + + } + else { + + echo '{'; + $i = 0; + foreach ($input as $key => $value) { + if ($html_encode === true) echo htmlspecialchars('"' . $key . '" : ' . json_encode($value)); + else echo '"' . $key . '": ' . json_encode($value); + $i++; + if ($i !== $last) echo ','; + } + echo '}'; + + } + + } }