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); } }
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); } }
/** * 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()); }
/** * @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; }
private function encodeFile($jsonData, $path) { if (!is_string($path) || !Path::isAbsolute($path)) { throw new IOException(sprintf('Cannot write "%s": Expected an absolute path.', $path)); } if (is_dir($path)) { throw new IOException(sprintf('Cannot write %s: Is a directory.', $path)); } $encoder = new JsonEncoder(); $encoder->setPrettyPrinting(true); $encoder->setEscapeSlash(false); $encoder->setTerminateWithLineFeed(true); $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; if (!is_dir($dir = Path::getDirectory($path))) { $filesystem = new Filesystem(); $filesystem->mkdir($dir); } $encoder->encodeFile($jsonData, $path, $configSchema); }
/** * Test that the file name is present in the output. * * @expectedException \Webmozart\Json\InvalidSchemaException * @expectedExceptionMessage valid.json */ public function testDecodeFileFailsIfSchemaInvalid() { $this->decoder->decodeFile($this->fixturesDir . '/valid.json', 'bogus.json'); }
/** * Loads the JSON file. */ private function load() { $decoder = new JsonDecoder(); $decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY); $this->json = file_exists($this->path) ? $decoder->decodeFile($this->path) : array(); }
private function decodeFile($path) { $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'); if (!file_exists($path)) { throw FileNotFoundException::forPath($path); } try { $jsonData = $decoder->decodeFile($path); } catch (DecodingFailedException $e) { throw new InvalidConfigException(sprintf("The configuration in %s could not be decoded:\n%s", $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 in %s is invalid:\n%s", $path, implode("\n", $errors))); } return $jsonData; }
/** * Loads the JSON file. */ private function load() { $decoder = new JsonDecoder(); $decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY); $this->json = file_exists($this->path) ? $decoder->decodeFile($this->path) : array(); if (!isset($this->json['keysByTypeName'])) { $this->json['keysByTypeName'] = array(); $this->json['keysByUuid'] = array(); $this->json['typesByKey'] = array(); $this->json['bindingsByKey'] = array(); $this->json['nextKey'] = 0; } }
/** * Loads the JSON file. */ protected function load() { $decoder = new JsonDecoder(); $this->json = file_exists($this->path) ? (array) $decoder->decodeFile($this->path, $this->schemaPath) : array(); if (isset($this->json['_order'])) { $this->json['_order'] = (array) $this->json['_order']; foreach ($this->json['_order'] as $path => $entries) { foreach ($entries as $key => $entry) { $this->json['_order'][$path][$key] = (array) $entry; } } } // The root node always exists if (!isset($this->json['/'])) { $this->json['/'] = null; } // Make sure the JSON is sorted in reverse order krsort($this->json); }
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; }
/** * @param string $collection * @param string $identifier * * @return array */ public function load($collection, $identifier) { return $this->decoder->decodeFile($this->findFile($collection, $identifier)); }
private function load() { try { return $this->decoder->decodeFile($this->path); } catch (FileNotFoundException $e) { return array(); } catch (DecodingFailedException $e) { throw new ReadException($e->getMessage(), 0, $e); } catch (IOException $e) { throw new ReadException($e->getMessage(), 0, $e); } }