note 123931 added to function.json-encode

From: Date: Wed, 12 Jun 2019 20:30:40 +0000
Subject: note 123931 added to function.json-encode
Groups: php.notes 
Request: Send a blank email to php-notes+get-211242@lists.php.net to get a copy of this message
<?php class Json { /** * @todo verificar a existência das funções json padrão PHP */ public function __construct(){ if(!function_exists('json_encode') || !function_exists('json_decode')){ throw new \Exception('As funções padrão de codificação e decodificação Json json_encode() e json_decode() não estão habilitadas'); } } /** * @param mix => $value = conteudo a ser codificado * @param int => $options = flags * @param int => $depth = profundidade máxima */ public function encode($value, int $options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK, int $depth = 512) : string { $json_encode = json_encode($value, $options, $depth); switch(json_last_error()){ case JSON_ERROR_RECURSION: throw new \Exception('O objeto ou array passado para json_encode() inclui referências recursivas, e não pode ser formatada'); break; case JSON_ERROR_INF_OR_NAN: throw new \Exception('Um valor passado para json_encode() inclui NAN ou INF'); break; case JSON_ERROR_UNSUPPORTED_TYPE: throw new \Exception('Um valor de um tipo não suportado foi informado para json_encode(), possivelmente um resource'); break; } return $json_encode; } /** * @param str => $json = conteudo a ser decodificado * @param bol => $assoc = define se o valor retornado será um array ou um objeto */ public function decode($json, bool $assoc = true){ if(!is_string($json) || trim($json) === ''){ return $json; } $this->is_json($json); return json_decode($json, $assoc); } /** * @param str => $json = conteudo a ser verificado */ public function is_json(string $json){ json_decode($json); switch(json_last_error()){ case JSON_ERROR_NONE: return true; break; case JSON_ERROR_DEPTH: throw new \Exception('O limite de pilha de chamadas foi ultrapassado'); break; case JSON_ERROR_STATE_MISMATCH: throw new \Exception('Ocorreu um underflow e/ou incongruência de modos'); break; case JSON_ERROR_CTRL_CHAR: throw new \Exception('Erro em caracter de controle, possivelmente erro de enconding'); break; case JSON_ERROR_SYNTAX: throw new \Exception('Erro de sintaxe'); break; case JSON_ERROR_UTF8: throw new \Exception('Caracteres UTF-8 mal formados, possivelmente erro de enconding'); break; default: throw new \Exception('Ocorreu um erro desconhecido ao decodificar o conteúdo'); break; } } } ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.134 (proxied: 2804:d57:2e7f:a900:b444:fc0d:3fff:189c) ---- Manual Page -- http://php.net/manual/en/function.json-encode.php Edit -- https://master.php.net/note/edit/123931 Del: integrated -- https://master.php.net/note/delete/123931/integrated Del: useless -- https://master.php.net/note/delete/123931/useless Del: bad code -- https://master.php.net/note/delete/123931/bad+code Del: spam -- https://master.php.net/note/delete/123931/spam Del: non-english -- https://master.php.net/note/delete/123931/non-english Del: in docs -- https://master.php.net/note/delete/123931/in+docs Del: other reasons-- https://master.php.net/note/delete/123931 Reject -- https://master.php.net/note/reject/123931 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211242) next »