Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Json.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Framework\App\Response\HttpInterface as HttpResponseInterface;
12 
20 class Json extends AbstractResult
21 {
25  protected $translateInline;
26 
30  protected $json;
31 
35  private $serializer;
36 
41  public function __construct(
43  \Magento\Framework\Serialize\Serializer\Json $serializer = null
44  ) {
45  $this->translateInline = $translateInline;
46  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
47  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
48  }
49 
59  public function setData($data, $cycleCheck = false, $options = [])
60  {
61  if ($data instanceof \Magento\Framework\DataObject) {
62  $data = $data->toArray();
63  }
64  $this->json = $this->serializer->serialize($data);
65  return $this;
66  }
67 
72  public function setJsonData($jsonData)
73  {
74  $this->json = (string)$jsonData;
75  return $this;
76  }
77 
81  protected function render(HttpResponseInterface $response)
82  {
83  $this->translateInline->processResponseBody($this->json, true);
84  $response->setHeader('Content-Type', 'application/json', true);
85  $response->setBody($this->json);
86  return $this;
87  }
88 }
$response
Definition: 404.php:11
__construct(InlineInterface $translateInline, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Json.php:41
render(HttpResponseInterface $response)
Definition: Json.php:81
setData($data, $cycleCheck=false, $options=[])
Definition: Json.php:59