Skip to content

Commit

Permalink
MAGETWO-71062: Remove zend json from json controller #10342
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Korshenko authored Jul 27, 2017
2 parents dc868e6 + dc80f44 commit 2742ee8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Tax/Controller/Adminhtml/Rate/AjaxLoad.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class AjaxLoad extends \Magento\Tax\Controller\Adminhtml\Rate
/**
* Json needed for the Ajax Edit Form
*
* @return void
* @return \Magento\Framework\Controller\Result\Json
* @throws \InvalidArgumentException
*/
public function execute()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AjaxSave extends \Magento\Tax\Controller\Adminhtml\Rate
* Save Tax Rate via AJAX
*
* @return \Magento\Framework\Controller\Result\Json
* @throws \InvalidArgumentException
*/
public function execute()
{
Expand Down
57 changes: 49 additions & 8 deletions lib/internal/Magento/Framework/Controller/Result/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,65 @@ 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
* @see Json::setArrayData
* @see Json::setJsonData
*/
public function setData($data, $cycleCheck = false, $options = [])
{
$this->json = \Zend_Json::encode($data, $cycleCheck, $options);
if ($data instanceof \Magento\Framework\DataObject) {
return $this->setArrayData($data->toArray());
}

if (is_array($data)) {
return $this->setArrayData($data);
}

if (is_string($data)) {
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)
{
$this->setJsonData($this->serializer->serialize($data));
return $this;
}

Expand Down

0 comments on commit 2742ee8

Please sign in to comment.