encodeFile() public method

Encodes data into a JSON file.
See also: encode
public encodeFile ( mixed $data, string $path, string | object $schema = null )
$data mixed The data to encode
$path string The path where the JSON file will be stored
$schema string | object The schema file or object
Example #1
0
 /**
  * Test that the file name is present in the output.
  *
  * @expectedException \Webmozart\Json\EncodingFailedException
  * @expectedExceptionMessage JsonEncoderTest
  * @expectedExceptionCode 5
  */
 public function testEncodeFileFailsIfNonUtf8()
 {
     if (version_compare(PHP_VERSION, '5.5.0', '<')) {
         $this->markTestSkipped('PHP >= 5.5.0 only');
         return;
     }
     $this->encoder->encodeFile(file_get_contents($this->fixturesDir . '/win-1258.json'), $this->tempFile);
 }
 /**
  * Writes the JSON file.
  */
 protected function flush()
 {
     // The root node always exists
     if (!isset($this->json['/'])) {
         $this->json['/'] = null;
     }
     // Always save in reverse order
     krsort($this->json);
     // Comply to schema
     $json = (object) $this->json;
     if (isset($json->{'_order'})) {
         $order = $json->{'_order'};
         foreach ($order as $path => $entries) {
             foreach ($entries as $key => $entry) {
                 $order[$path][$key] = (object) $entry;
             }
         }
         $json->{'_order'} = (object) $order;
     }
     $this->encoder->encodeFile($json, $this->path, $this->schemaPath);
 }
 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);
 }
Example #4
0
 /**
  * Writes the JSON file.
  */
 private function flush()
 {
     $this->encoder->encodeFile($this->json, $this->path);
 }
 private function save($data)
 {
     try {
         $this->encoder->encodeFile($data, $this->path);
     } catch (EncodingFailedException $e) {
         if (JSON_ERROR_UTF8 === $e->getCode()) {
             throw UnsupportedValueException::forType('binary', $this);
         }
         throw new WriteException($e->getMessage(), 0, $e);
     } catch (IOException $e) {
         throw new WriteException($e->getMessage(), 0, $e);
     }
 }