protected function checkJson($key, $value, array $rule) { try { $value = \Owl\safe_json_decode($value, true); } catch (\UnexpectedValueException $ex) { throw $this->exception($key, 'json_decode() falied, ' . $ex->getMessage()); } return $this->checkArray($key, $value, $rule); }
public function normalize($value, array $attribute) { if (is_array($value)) { return $value; } if ($this->isNull($value)) { return []; } return \Owl\safe_json_decode($value, true); }
public function testSafeJson() { try { $json = '{a:1'; \Owl\safe_json_decode($json, true); $this->fail('test safe_json_decode() failed'); } catch (\UnexpectedValueException $ex) { } try { $string = substr('爱', 0, 1); \Owl\safe_json_encode($string); $this->fail('test safe_json_encode() failed'); } catch (\UnexpectedValueException $ex) { } }
protected function getCache(array $id) { $key = $this->getCacheKey($id); $memcached = $this->getCacheService($key); try { if ($record = $memcached->get($key)) { $record = \Owl\safe_json_decode($record, true); } return $record ?: []; } catch (\UnexpectedValueException $exception) { if (DEBUG) { throw $exception; } return []; } }
protected function decode($string) { $string = base64_decode($string, true); if ($string === false) { return []; } if ($this->getConfig('encrypt')) { // 解密 $string = $this->decrypt($string); } elseif ($this->getConfig('zip')) { // 解压 $string = substr($string, 0, 1) == '_' ? gzuncompress(substr($string, 1)) : $string; } // sha1 raw binary length is 20 $hash_length = 20; // 数字签名校验 do { if (!$string || strlen($string) <= $hash_length) { break; } $hash = substr($string, $hash_length * -1); $string = substr($string, 0, strlen($string) - $hash_length); if ($this->getSign($string) !== $hash) { break; } return \Owl\safe_json_decode($string, true) ?: []; } while (false); return []; }
public function getParsedBody() { $content_type = $this->getHeaderLine('content-type'); $method = $this->getServerParam('REQUEST_METHOD'); if ($method === 'POST' && ($content_type === 'application/x-www-form-urlencoded' || $content_type === 'multipart/form-data')) { return $this->post; } $body = (string) $this->body; if ($body === '') { return; } if ($content_type === 'application/json') { return \Owl\safe_json_decode($body, true); } return $body; }