Add function to print JSON data without encoding it in bulk (to reduce
memory consumption)
This commit is contained in:
@@ -1087,4 +1087,44 @@ final class MD_STD {
|
|||||||
return $lowest_option;
|
return $lowest_option;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints an array JSON-encoded. Does so entry by entry to reduce memory consumption.
|
||||||
|
*
|
||||||
|
* @param array<mixed> $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 '}';
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user