How to use \Magento\Framework\Controller\Result\JsonFactory for Magento 2.3?

This post is related to this Magento2 – How to get JSON response from Controller

What I am trying to do is to format the json response of my custom web API, but it seems \Magento\Framework\Controller\Result\JsonFactory doesn’t seem to work in Magento 2.3

Here’s my implementation:

/** @var \Magento\Framework\Controller\Result\JsonFactory  */ protected $  resultJsonFactory;  public function __construct(     ...     \Magento\Framework\Controller\Result\JsonFactory $  resultJsonFactory     ... ) {     ...     $  this->resultJsonFactory = $  resultJsonFactory;     ... }   

And implemented it like this:

$  result = $  this->resultJsonFactory->create(); $  result->setData(['message' => 'test']); return $  result;   

And the result (Just a blank bracket):

[] 

Additional solution made:
Tried to implement \Zend_Json::encode() and json_encode but getting this result:

"{\"message\":\"test\"}" 

Instead of (expected result/prettified json response):

{     "message": "test" }