decode() public method

If a schema is passed, the decoded object is validated against that schema. The schema may be passed as file path or as object returned from JsonDecoder::decodeFile($schemaFile). You can adjust the decoding with {@link setObjectDecoding()}, {@link setBigIntDecoding()} and {@link setMaxDepth()}. Schema validation is not supported when objects are decoded as associative arrays.
public decode ( string $json, string | object $schema = null ) : mixed
$json string The JSON string
$schema string | object The schema file or object
return mixed The decoded value
 /**
  * POST request handling
  * 
  * @param string $path
  * @param array $data
  *
  * @return GuzzleHttp\RequestInterface
  */
 public function post($path, $data = array())
 {
     $response = $this->httpClient->request('POST', $path, ['json' => $data]);
     if ($response->getStatusCode() !== 200) {
         throw new \UnexpectedValueException('HTTP Status: ' . $response->getStatusCode());
     }
     return $this->decoder->decode($response->getBody());
 }
 private function decode($json, $path)
 {
     try {
         return $this->jsonDecoder->decode($json);
     } catch (DecodingFailedException $e) {
         throw new InvalidConfigException(sprintf('The configuration in %s could not be decoded: %s', $path, $e->getMessage()), 0, $e);
     }
 }
 private function decode($json, $path = null)
 {
     $decoder = new JsonDecoder();
     try {
         return $decoder->decode($json);
     } catch (DecodingFailedException $e) {
         throw new InvalidConfigException(sprintf("The configuration%s could not be decoded:\n%s", $path ? ' in ' . $path : '', $e->getMessage()), $e->getCode(), $e);
     }
 }
 private function decode($json, $path = null)
 {
     $decoder = new JsonDecoder();
     // We can't use realpath(), which doesn't work inside PHARs.
     // However, we want to display nice paths if the file is not found.
     $schema = $decoder->decodeFile(Path::canonicalize(__DIR__ . '/../../res/schema/package-schema-1.0.json'));
     $configSchema = $schema->properties->config;
     try {
         return $decoder->decode($json, $configSchema);
     } catch (ValidationFailedException $e) {
         throw new InvalidConfigException(sprintf("The configuration%s is invalid:\n%s", $path ? ' in ' . $path : '', $e->getErrorsAsString()), 0, $e);
     } catch (DecodingFailedException $e) {
         throw new InvalidConfigException(sprintf("The configuration%s could not be decoded:\n%s", $path ? ' in ' . $path : '', $e->getMessage()), $e->getCode(), $e);
     }
 }
 /**
  * @param Response $response
  * @return SystemsResponse
  */
 public function create(Response $response)
 {
     $jsonDecoder = new JsonDecoder();
     $collection = new ArrayCollection();
     if ($response->getStatusCode() === 200) {
         $data = (string) $response->getBody();
         try {
             $data = $jsonDecoder->decode($data);
         } catch (\Exception $e) {
         }
         if (isset($data->results)) {
             foreach ($data->results as $result) {
                 $collection->add(System::createFromApi($result));
             }
         }
     }
     return new SystemsResponse($response, $collection);
 }
 /**
  * @return array
  * @throws \Exception
  * @throws \Webmozart\Json\ValidationFailedException
  */
 protected function fetchRepositories()
 {
     $client = new Client();
     $request = $client->get(static::REPOSITORIES_URL);
     $response = $request->send();
     $responseBody = $response->getBody(true);
     $jsonDecoder = new JsonDecoder();
     $jsonDecoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY);
     $json = $jsonDecoder->decode($responseBody);
     if (null !== $json['meta']) {
         throw new \Exception($json['meta']['error']);
     }
     $repositories = array();
     if (is_array($json['data'])) {
         foreach ($json['data'] as $extKey => $extData) {
             $repositories[] = array('type' => 'vcs', 'url' => $extData['repository_clone_url']);
         }
     }
     return $repositories;
 }
Beispiel #7
0
 public function testDecodeBigIntAsString()
 {
     $this->decoder->setBigIntDecoding(JsonDecoder::STRING);
     $decoded = $this->decoder->decode('12312512423531123');
     $this->assertEquals('12312512423531123', $decoded);
 }
 private function decode($json, $path = null)
 {
     $decoder = new JsonDecoder();
     try {
         $jsonData = $decoder->decode($json);
     } catch (DecodingFailedException $e) {
         throw new InvalidConfigException(sprintf("The configuration%s could not be decoded:\n%s", $path ? ' in ' . $path : '', $e->getMessage()), $e->getCode(), $e);
     }
     $this->assertVersionSupported($jsonData->version, $path);
     $this->validate($jsonData, $path);
     return $jsonData;
 }
 private function decode($json, $path = null)
 {
     $decoder = new JsonDecoder();
     $validator = new JsonValidator();
     // We can't use realpath(), which doesn't work inside PHARs.
     // However, we want to display nice paths if the file is not found.
     $schema = Path::canonicalize(__DIR__ . '/../../res/schema/package-schema-1.0.json');
     try {
         $jsonData = $decoder->decode($json);
     } catch (DecodingFailedException $e) {
         throw new InvalidConfigException(sprintf("The configuration%s could not be decoded:\n%s", $path ? ' in ' . $path : '', $e->getMessage()), $e->getCode(), $e);
     }
     if (version_compare($jsonData->version, '1.0', '<')) {
         throw UnsupportedVersionException::versionTooLow($jsonData->version, '1.0', $path);
     }
     if (version_compare($jsonData->version, '1.0', '>')) {
         throw UnsupportedVersionException::versionTooHigh($jsonData->version, '1.0', $path);
     }
     $errors = $validator->validate($jsonData, $schema);
     if (count($errors) > 0) {
         throw new InvalidConfigException(sprintf("The configuration%s is invalid:\n%s", $path ? ' in ' . $path : '', implode("\n", $errors)));
     }
     return $jsonData;
 }