2

Any one tell me how i can overwrite this file in magento 2.3

Path:

Magento2\lib\internal\Magento\Framework\Webapi\Rest\Response\Renderer\Json.php

1
  • which method you want to override?
    – Amit Bera
    Commented Apr 17, 2019 at 7:23

3 Answers 3

2

Just Create di.xml under app/code/vendor/Module/etc and put below code

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Framework\Webapi\Rest\Response\Renderer\Json" type="Vendor\Module\Webapi\Rest\Response\Renderer\Json" />
</config>
10
  • Hi Ansar, in magento 2.3 folder structure are differ we can not crate any new folder/file directly in vendor so can you please suggest solution for Magento 2.3 version? Commented Apr 17, 2019 at 6:55
  • 1
    you have no need to create directory in vendor , you need to create a custom module in app/code directory i just give temp name of module Vendor\Module you need to put your name on that Commented Apr 17, 2019 at 7:03
  • I try this as well find below my code: di.xml <?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Webapi\Rest\Response\Renderer" type="Evolve\Apirenderer\Webapi\Rest\Response\Renderer" /> </config> but not working! Commented Apr 17, 2019 at 7:28
  • can you use this xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd" instead of xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd" Commented Apr 17, 2019 at 7:37
  • Not Working : <?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Webapi\Rest\Response\Renderer" type="Evolve\Apirenderer\Webapi\Rest\Response\Renderer" /> </config> Commented Apr 17, 2019 at 8:41
0

Find below the code which is working just need to put file name no need of extension of file.

1
  • <?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Framework\Webapi\Rest\Response\Renderer\Json" type="Evolve\Apirenderer\Webapi\Rest\Response\Renderer\Json" /> </config> Commented Apr 17, 2019 at 9:21
0

Follow Below Steps:

  1. Overwrite this file:

    \Magento\Framework\Webapi\Rest\Response\Renderer\Json.php

  2. We are overwrite Json.php file using : di.xml (under app/code/Bhashkar/Module/etc and put below code)

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="\Magento\Framework\Webapi\Rest\Response\Renderer\Json" type="Bhashkar\Module\Webapi\Rest\Response\Renderer\Json" />
</config>
  1. Create Bhashkar\Webapi\Webapi\Rest\Response\Renderer\Json.php File and modify the functionality as per your Requirement :

    namespace Bhashkar\Module\Webapi\Rest\Response\Renderer;

    class Json implements \Magento\Framework\Webapi\Rest\Response\RendererInterface
    {
        /**
         * Adapter mime type.
         */
        const MIME_TYPE = 'application/json';
    
        /**
         * @var \Magento\Framework\Json\Encoder
         */
        protected $encoder;
    
        /**
         * Initialize dependencies.
         *
         * @param \Magento\Framework\Json\Encoder $encoder
         */
        public function __construct(\Magento\Framework\Json\Encoder $encoder)
        {
            $this->encoder= $encoder;
        }
    
        /**
         * Convert data to JSON.
         *
         * @param object|array|int|string|bool|float|null $data
         * @return string
         */
        public function render($data)
        {
            return $this->encoder->encode($data);
        }
    
        /**
         * Get JSON renderer MIME type.
         *
         * @return string
         */
        public function getMimeType()
        {
            return self::MIME_TYPE;
        }
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.