note 128844 added to function.json-encode

From: Date: Thu, 31 Aug 2023 09:24:48 +0000
Subject: note 128844 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-220806@lists.php.net to get a copy of this message
JSON Encode Huge Data with minimum Memory footprint. <?php /** * Creates JSON * * This class is built to avoid creation of large array objects * (which leads to memory limit issues for larger data set) * which are then converted to JSON. This class gives access to * create JSON in parts for what ever smallest part of data * we have of the large data set which are yet to be fetched. */ class JsonEncode { /** * Array of JsonEncodeObject objects * * @var array */ private $objects = []; /** * Current JsonEncodeObject object * * @var object */ private $currentObject = null; /** * JsonEncode constructor */ public function __construct() { } /** * Escape the json string key or value * * @param string $str json key or value string. * @return string */ private function escape($str) { if (is_null($str)) return 'null'; $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c"); $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); $str = str_replace($escapers, $replacements, $str); return '"' . $str . '"'; } /** * Encodes both simple and associative array to json * * @param $arr string value escaped and array value json_encode function is applied. * @return void */ public function encode($arr) { if ($this->currentObject) { echo $this->currentObject->comma; } if (is_array($arr)) { echo json_encode($arr); } else { echo $this->escape($arr); } if ($this->currentObject) { $this->currentObject->comma = ','; } } /** * Add simple array/value as in the json format. * * @param $value data type is string/array. This is used to add value/array in the current Array. * @return void */ public function addValue($value) { if ($this->currentObject->mode !== 'Array') { throw new Exception('Mode should be Array'); } $this->encode($value); } /** * Add simple array/value as in the json format. * * @param string $key key of associative array * @param $value data type is string/array. This is used to add value/array in the current Array. * @return void */ public function addKeyValue($key, $value) { if ($this->currentObject->mode !== 'Assoc') { throw new Exception('Mode should be Assoc'); } echo $this->currentObject->comma; echo $this->escape($key) . ':'; $this->currentObject->comma = ''; $this->encode($value); } /** * Start simple array * * @param string $key Used while creating simple array inside an associative array and $key is the key. * @return void */ public function startArray($key = null) { if ($this->currentObject) { echo $this->currentObject->comma; array_push($this->objects, $this->currentObject); } $this->currentObject = new JsonEncodeObject('Array'); if (!is_null($key)) { echo $this->escape($key) . ':'; } echo '['; } /** * End simple array * * @return void */ public function endArray() { echo ']'; $this->currentObject = null; if (count($this->objects)>0) { $this->currentObject = array_pop($this->objects); $this->currentObject->comma = ','; } } /** * Start simple array * * @param string $key Used while creating associative array inside an associative array and $key is the key. * @return void */ public function startAssoc($key = null) { if ($this->currentObject) { echo $this->currentObject->comma; array_push($this->objects, $this->currentObject); } $this->currentObject = new JsonEncodeObject(' ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 103.38.68.42) ---- Manual Page -- https://php.net/manual/en/function.json-encode.php Edit -- https://main.php.net/note/edit/128844 Del: integrated -- https://main.php.net/note/delete/128844/integrated Del: useless -- https://main.php.net/note/delete/128844/useless Del: bad code -- https://main.php.net/note/delete/128844/bad+code Del: spam -- https://main.php.net/note/delete/128844/spam Del: non-english -- https://main.php.net/note/delete/128844/non-english Del: in docs -- https://main.php.net/note/delete/128844/in+docs Del: other reasons-- https://main.php.net/note/delete/128844 Reject -- https://main.php.net/note/reject/128844 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220806) next »