Example #1
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;
 }
Example #2
0
 public function decode($json, $schema = null)
 {
     if (self::ASSOC_ARRAY === $this->objectDecoding && null !== $schema) {
         throw new \InvalidArgumentException('اعتبارسنجی الگو زمانی که اشیاء به عنوان آرایه های انجمنی رمزگشایی شده باشد،پشتیبانی نمی کنید. ' . 'برای اصلاح JsonDecoder::setDecodeObjectsAs(JsonDecoder::JSON_OBJECT) را فراخوانی کنید.');
     }
     $decoded = $this->decodeJson($json);
     if (null !== $schema) {
         $errors = $this->validator->validate($decoded, $schema);
         if (count($errors) > 0) {
             throw ValidationFailedException::fromErrors($errors);
         }
     }
     return $decoded;
 }