예제 #1
0
 private function decodeJson($json)
 {
     $assoc = self::ASSOC_ARRAY === $this->objectDecoding;
     if (PHP_VERSION_ID >= 50400 && !defined('JSON_C_VERSION')) {
         $options = self::STRING === $this->bigIntDecoding ? JSON_BIGINT_AS_STRING : 0;
         $decoded = json_decode($json, $assoc, $this->maxDepth, $options);
     } else {
         $decoded = json_decode($json, $assoc, $this->maxDepth);
     }
     if (null === $decoded && 'null' !== $json) {
         $parser = new JsonParser();
         $e = $parser->lint($json);
         if ($e instanceof ParsingException) {
             throw new DecodingFailedException(sprintf('داده JSON نمی تواند رمزگشایی شود: %s.', $e->getMessage()), 0, $e);
         }
         throw new DecodingFailedException(sprintf('داده JSON نمی تواند رمزگشایی شود: %s.', JsonError::getLastErrorMessage()), json_last_error());
     }
     return $decoded;
 }
예제 #2
0
 public function encode($data, $schema = null)
 {
     if (null !== $schema) {
         $errors = $this->validator->validate($data, $schema);
         if (count($errors) > 0) {
             throw ValidationFailedException::fromErrors($errors);
         }
     }
     $options = 0;
     if (self::JSON_OBJECT === $this->arrayEncoding) {
         $options |= JSON_FORCE_OBJECT;
     }
     if (self::JSON_NUMBER === $this->numericEncoding) {
         $options |= JSON_NUMERIC_CHECK;
     }
     if ($this->gtLtEscaped) {
         $options |= JSON_HEX_TAG;
     }
     if ($this->ampersandEscaped) {
         $options |= JSON_HEX_AMP;
     }
     if ($this->singleQuoteEscaped) {
         $options |= JSON_HEX_APOS;
     }
     if ($this->doubleQuoteEscaped) {
         $options |= JSON_HEX_QUOT;
     }
     if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
         if (!$this->slashEscaped) {
             $options |= JSON_UNESCAPED_SLASHES;
         }
         if (!$this->unicodeEscaped) {
             $options |= JSON_UNESCAPED_UNICODE;
         }
         if ($this->prettyPrinting) {
             $options |= JSON_PRETTY_PRINT;
         }
     }
     if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
         $maxDepth = $this->maxDepth;
         if (!defined('HHVM_VERSION')) {
             --$maxDepth;
         }
         $encoded = json_encode($data, $options, $maxDepth);
     } else {
         $encoded = json_encode($data, $options);
     }
     if ($encoded === false) {
         throw new EncodingFailedException(sprintf('داده ها نمی توانند به عنوان JSON کدگذاری شوند: %s', JsonError::getLastErrorMessage()), json_last_error());
     }
     if ($this->terminatedWithLineFeed) {
         $encoded .= "\n";
     }
     return $encoded;
 }