private function encode($jsonData) { $encoder = new JsonEncoder(); $encoder->setPrettyPrinting(true); $encoder->setEscapeSlash(false); $encoder->setTerminateWithLineFeed(true); return $encoder->encode($jsonData); }
/** * @param GuzzleClient $client * @param RequestInterface $request * @return ResponseInterface */ public function handle(GuzzleClient $client, RequestInterface $request) { try { $response = $client->send(new HttpRequest($request->getMethod(), $request->getUri(), $request->getHeaders(), $this->jsonEncoder->encode($request->getBody()))); } catch (\Exception $e) { $this->logger->debug(sprintf('Exception thrown during Guzzle request'), ['message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile(), 'trace' => $e->getTraceAsString()]); $response = new Response(500); if ($e instanceof ClientException) { $response = new Response(401); } } return $request->getResponseFactory()->create($response); }
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 encode(stdClass $jsonData, $path) { try { return $this->jsonEncoder->encode($jsonData); } catch (EncodingFailedException $e) { throw new InvalidConfigException(sprintf('The configuration in %s could not be encoded: %s', $path, $e->getMessage()), 0, $e); } }
/** * 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); }
/** * @return string */ public function __toString() { if (empty($this->data['repositories'])) { unset($this->data['repositories']); } if (empty($this->data['require'])) { unset($this->data['require']); } return $this->jsonEncoder->encode($this->data); }
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); }
/** * 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); }
/** * 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; }
/** * Writes the JSON file. */ private function flush() { $this->encoder->encodeFile($this->json, $this->path); }
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); }
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); } }