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
9 
12 
13 class Json implements \Magento\Framework\Webapi\Rest\Request\DeserializerInterface
14 {
19  protected $decoder;
20 
24  protected $_appState;
25 
29  private $serializer;
30 
37  public function __construct(
38  \Magento\Framework\Json\Decoder $decoder,
39  State $appState,
40  \Magento\Framework\Serialize\Serializer\Json $serializer = null
41  ) {
42  $this->decoder = $decoder;
43  $this->_appState = $appState;
44  $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
45  ->get(\Magento\Framework\Serialize\Serializer\Json::class);
46  }
47 
56  public function deserialize($encodedBody)
57  {
58  if (!is_string($encodedBody)) {
59  throw new \InvalidArgumentException(
60  sprintf('"%s" data type is invalid. String is expected.', gettype($encodedBody))
61  );
62  }
63  try {
64  $decodedBody = $this->serializer->unserialize($encodedBody);
65  } catch (\InvalidArgumentException $e) {
66  if ($this->_appState->getMode() !== State::MODE_DEVELOPER) {
67  throw new \Magento\Framework\Webapi\Exception(new Phrase('Decoding error.'));
68  } else {
69  throw new \Magento\Framework\Webapi\Exception(
70  new Phrase(
71  'Decoding error: %1%2%3%4',
72  [PHP_EOL, $e->getMessage(), PHP_EOL, $e->getTraceAsString()]
73  )
74  );
75  }
76  }
77  return $decodedBody;
78  }
79 }
__construct(\Magento\Framework\Json\Decoder $decoder, State $appState, \Magento\Framework\Serialize\Serializer\Json $serializer=null)
Definition: Json.php:37