Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Json Class Reference
Inheritance diagram for Json:
AbstractResult ResultInterface

Public Member Functions

 __construct (InlineInterface $translateInline, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
 
 setData ($data, $cycleCheck=false, $options=[])
 
 setJsonData ($jsonData)
 
- Public Member Functions inherited from AbstractResult
 setHttpResponseCode ($httpCode)
 
 setHeader ($name, $value, $replace=false)
 
 setStatusHeader ($httpCode, $version=null, $phrase=null)
 
 renderResult (ResponseInterface $response)
 

Protected Member Functions

 render (HttpResponseInterface $response)
 
- Protected Member Functions inherited from AbstractResult
 applyHttpHeaders (HttpResponseInterface $response)
 
 render (HttpResponseInterface $response)
 

Protected Attributes

 $translateInline
 
 $json
 
- Protected Attributes inherited from AbstractResult
 $httpResponseCode
 
 $headers = []
 
 $statusHeaderCode
 
 $statusHeaderVersion
 
 $statusHeaderPhrase
 

Detailed Description

A possible implementation of JSON response type (instead of hardcoding json_encode() all over the place) Actual for controller actions that serve ajax requests

@api

Since
100.0.2

Definition at line 20 of file Json.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( InlineInterface  $translateInline,
\Magento\Framework\Serialize\Serializer\Json  $serializer = null 
)
Parameters
\Magento\Framework\Translate\InlineInterface$translateInline
\Magento\Framework\Serialize\Serializer\Json$serializer

Definition at line 41 of file Json.php.

44  {
45  $this->translateInline = $translateInline;
46  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
47  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
48  }

Member Function Documentation

◆ render()

render ( HttpResponseInterface  $response)
protected

{}

Definition at line 81 of file Json.php.

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  }
$response
Definition: 404.php:11

◆ setData()

setData (   $data,
  $cycleCheck = false,
  $options = [] 
)

Set json data

Parameters
mixed$data
boolean$cycleCheckOptional; whether or not to check for object recursion; off by default
array$optionsAdditional options used during encoding
Returns
$this @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 59 of file Json.php.

60  {
61  if ($data instanceof \Magento\Framework\DataObject) {
62  $data = $data->toArray();
63  }
64  $this->json = $this->serializer->serialize($data);
65  return $this;
66  }

◆ setJsonData()

setJsonData (   $jsonData)
Parameters
string$jsonData
Returns
$this

Definition at line 72 of file Json.php.

73  {
74  $this->json = (string)$jsonData;
75  return $this;
76  }

Field Documentation

◆ $json

$json
protected

Definition at line 30 of file Json.php.

◆ $translateInline

$translateInline
protected

Definition at line 25 of file Json.php.


The documentation for this class was generated from the following file: