-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Remove zend json from json controller #10342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
baeb58c
db6f353
4f78388
7e46cea
ff0581b
41e118b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,24 +29,63 @@ class Json extends AbstractResult | |
protected $json; | ||
|
||
/** | ||
* @param \Magento\Framework\Translate\InlineInterface $translateInline | ||
* @var \Magento\Framework\Serialize\Serializer\Json | ||
*/ | ||
public function __construct(InlineInterface $translateInline) | ||
{ | ||
private $serializer; | ||
|
||
/** | ||
* @param InlineInterface $translateInline | ||
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer | ||
* @throws \RuntimeException | ||
*/ | ||
public function __construct( | ||
InlineInterface $translateInline, | ||
\Magento\Framework\Serialize\Serializer\Json $serializer = null | ||
) { | ||
$this->translateInline = $translateInline; | ||
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() | ||
->get(\Magento\Framework\Serialize\Serializer\Json::class); | ||
} | ||
|
||
/** | ||
* Set json data | ||
* | ||
* @param mixed $data | ||
* @param boolean $cycleCheck Optional; whether or not to check for object recursion; off by default | ||
* @param array $options Additional options used during encoding | ||
* @return $this | ||
* @param array|string|\Magento\Framework\DataObject $data | ||
* @param bool $cycleCheck | ||
* @param array $options | ||
* @return Json | ||
* @throws \InvalidArgumentException | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
* @deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree with deprecating this method. Two arguments which has no effect should be deprecated though. Please change method description accordingly. |
||
*/ | ||
public function setData($data, $cycleCheck = false, $options = []) | ||
{ | ||
$this->json = \Zend_Json::encode($data, $cycleCheck, $options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is just setter, no need to call another setter, Magento is already slow enough. |
||
if ($data instanceof \Magento\Framework\DataObject) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why need this explicitly? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed to maintain compatibility with the old implementation which took care of this inside. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see...
Why it is not maintained for any object then? |
||
return $this->setArrayData($data->toArray()); | ||
} | ||
|
||
if (is_array($data)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please clarify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return $this->setArrayData($data); | ||
} | ||
|
||
if (is_string($data)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Passing string parameter did encode There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please clarify There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return $this->setJsonData($data); | ||
} | ||
|
||
throw new \Magento\Framework\Exception\LocalizedException( | ||
new \Magento\Framework\Phrase('Invalid argument type') | ||
); | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return $this | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function setArrayData(array $data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this method as it does not bring any value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method was introduced to deprecate and remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I strongly disagree with. Its responsibility is quite clear - encode value and set it. There is no need to think which value you're passing.
I do agree that |
||
{ | ||
$this->setJsonData($this->serializer->serialize($data)); | ||
return $this; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we rely on particular implementation and not interface by the way?
https://github.com/magento/magento2/blob/develop/app/etc/di.xml#L162
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#8331 (comment) was the original comment from the team on the matter,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I expected something like this. Not sure why such interface was introduced at all if we keep creating json-only code.
Needs to be mentioned somewhere to avoid confusion like #10335 (comment)