By default, JSON strings are not terminated with a line feed.
public setTerminateWithLineFeed ( boolean $enabled ) | ||
$enabled | boolean | Whether JSON strings should be terminated with a line feed |
private function encode($jsonData) { $encoder = new JsonEncoder(); $encoder->setPrettyPrinting(true); $encoder->setEscapeSlash(false); $encoder->setTerminateWithLineFeed(true); return $encoder->encode($jsonData); }
private function encode($jsonData) { $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; return $encoder->encode($jsonData, $configSchema); }
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); }
public function testDoNotTerminateWithLineFeed() { $this->encoder->setTerminateWithLineFeed(false); $this->assertSame('{"name":"Bernhard"}', $this->encoder->encode((object) array('name' => 'Bernhard'))); }
/** * Returns the JSON encoder. * * @return JsonEncoder The JSON encoder. */ public function getJsonEncoder() { if (!$this->jsonEncoder) { $this->jsonEncoder = new JsonEncoder(); $this->jsonEncoder->setPrettyPrinting(true); $this->jsonEncoder->setEscapeSlash(false); $this->jsonEncoder->setTerminateWithLineFeed(true); } return $this->jsonEncoder; }
private function encode(stdClass $jsonData, $path = null) { $encoder = new JsonEncoder(); $encoder->setPrettyPrinting(true); $encoder->setEscapeSlash(false); $encoder->setTerminateWithLineFeed(true); $this->validate($jsonData, $path); return $encoder->encode($jsonData); }
public function __construct($path, $flags = 0) { Assert::string($path, 'The path must be a string. Got: %s'); Assert::notEmpty($path, 'The path must not be empty.'); Assert::integer($flags, 'The flags must be an integer. Got: %s'); $this->path = $path; $this->flags = $flags; $this->encoder = new JsonEncoder(); $this->encoder->setEscapeGtLt($this->flags & self::ESCAPE_GT_LT); $this->encoder->setEscapeAmpersand($this->flags & self::ESCAPE_AMPERSAND); $this->encoder->setEscapeSingleQuote($this->flags & self::ESCAPE_SINGLE_QUOTE); $this->encoder->setEscapeDoubleQuote($this->flags & self::ESCAPE_DOUBLE_QUOTE); $this->encoder->setEscapeSlash(!($this->flags & self::NO_ESCAPE_SLASH)); $this->encoder->setEscapeUnicode(!($this->flags & self::NO_ESCAPE_UNICODE)); $this->encoder->setPrettyPrinting($this->flags & self::PRETTY_PRINT); $this->encoder->setTerminateWithLineFeed($this->flags & self::TERMINATE_WITH_LINE_FEED); $this->decoder = new JsonDecoder(); $this->decoder->setObjectDecoding(JsonDecoder::ASSOC_ARRAY); }