private function decodeFile($path) { $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 (!file_exists($path)) { throw FileNotFoundException::forPath($path); } try { return $decoder->decodeFile($path, $configSchema); } catch (ValidationFailedException $e) { throw new InvalidConfigException(sprintf("The configuration in %s is invalid:\n%s", $path, $e->getErrorsAsString()), 0, $e); } catch (DecodingFailedException $e) { throw new InvalidConfigException(sprintf("The configuration in %s could not be decoded:\n%s", $path, $e->getMessage()), $e->getCode(), $e); } }
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); }
/** * @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); } }