encode() public method

If a schema is passed, the value is validated against that schema before encoding. The schema may be passed as file path or as object returned from JsonDecoder::decodeFile($schemaFile). You can adjust the decoding with the various setters in this class.
public encode ( mixed $data, string | object $schema = null ) : string
$data mixed The data to encode
$schema string | object The schema file or object
return string The JSON string
示例#1
0
 /**
  * @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);
 }
示例#2
0
 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);
     }
 }
 private function encode($jsonData)
 {
     $encoder = new JsonEncoder();
     $encoder->setPrettyPrinting(true);
     $encoder->setEscapeSlash(false);
     $encoder->setTerminateWithLineFeed(true);
     return $encoder->encode($jsonData);
 }
 /**
  * @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);
 }
示例#5
0
 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);
 }
示例#6
0
 public function testDoNotTerminateWithLineFeed()
 {
     $this->encoder->setTerminateWithLineFeed(false);
     $this->assertSame('{"name":"Bernhard"}', $this->encoder->encode((object) array('name' => 'Bernhard')));
 }
示例#7
0
 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);
 }