toJson() public method

Converts an implementation-specific data structure to JSON.
public toJson ( mixed $data, array $options = [] ) : mixed
$data mixed The data to convert
$options array Additional implementation-specific conversion options
return mixed The JSON data. Pass this data to a {@link JsonEncoder} to generate a JSON string
示例#1
0
 /**
  * Saves a configuration file.
  *
  * The configuration file is saved to the same path that it was read from.
  *
  * @param ConfigFile $configFile The configuration file to save.
  *
  * @throws WriteException         If the file cannot be written.
  * @throws InvalidConfigException If the file contains invalid configuration.
  */
 public function saveConfigFile(ConfigFile $configFile)
 {
     try {
         $jsonData = $this->configFileConverter->toJson($configFile);
     } catch (ConversionFailedException $e) {
         throw new InvalidConfigException(sprintf('The data written to %s could not be converted: %s', $configFile->getPath(), $e->getMessage()), 0, $e);
     }
     $json = $this->encode($jsonData, $configFile->getPath());
     $this->storage->write($configFile->getPath(), $json);
     if ($this->factoryManager) {
         $this->factoryManager->autoGenerateFactoryClass();
     }
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function toJson($data, array $options = array())
 {
     $targetVersion = isset($options['targetVersion']) ? $options['targetVersion'] : $this->currentVersion;
     $this->assertVersionSupported($targetVersion);
     $jsonData = $this->innerConverter->toJson($data, $options);
     $this->assertObject($jsonData);
     $jsonData->version = $this->currentVersion;
     if ($jsonData->version !== $targetVersion) {
         $this->migrate($jsonData, $targetVersion);
         $jsonData->version = $targetVersion;
     }
     return $jsonData;
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function toJson($data, array $options = array())
 {
     $jsonData = $this->innerConverter->toJson($data, $options);
     $this->validate($jsonData);
     return $jsonData;
 }