title: How to convert PHP Protobuf Object to a JSON string
body: <p>I want to convert my Protobuf object to a JSON object using PHP so that I can send it back to the browser.</p>
  <p>My Protobuf is</p>
  <pre><code>syntax = &quot;proto3&quot;;
  package Protobuf.Gen.ProposalTotalModel;
  option php_generic_services = true;
  
  message ProposalTotal {
  
    message Total {
      double static = 1;
      double hourly = 2;
      double monthly = 3;
      double annual = 4;
    }
  
    message TotalAfterTax {
      double static = 1;
      double hourly = 2;
      double monthly = 3;
      double annual = 4;
    }
  
    Total total = 1;
    TotalAfterTax total_after_tax = 2;
    bool has_totals = 3;
  }
  
  </code></pre>
  <p>The protobuf generated code from the above file(skipping few other auto-generated files):</p>
  <pre><code>&lt;?php
  # Generated by the protocol buffer compiler.  DO NOT EDIT!
  # source: proposal_total.proto
  
  namespace Protobuf\Gen\ProposalTotalModel;
  
  use Google\Protobuf\Internal\GPBType;
  use Google\Protobuf\Internal\RepeatedField;
  use Google\Protobuf\Internal\GPBUtil;
  
  /**
   * Generated from protobuf message &lt;code&gt;Protobuf.Gen.ProposalTotalModel.ProposalTotal&lt;/code&gt;
   */
  class ProposalTotal extends \Google\Protobuf\Internal\Message
  {
      /**
       * Generated from protobuf field &lt;code&gt;.Protobuf.Gen.ProposalTotalModel.ProposalTotal.Total total = 1;&lt;/code&gt;
       */
      private $total = null;
      /**
       * Generated from protobuf field &lt;code&gt;.Protobuf.Gen.ProposalTotalModel.ProposalTotal.TotalAfterTax total_after_tax = 2;&lt;/code&gt;
       */
      private $total_after_tax = null;
      /**
       * Generated from protobuf field &lt;code&gt;bool has_totals = 3;&lt;/code&gt;
       */
      private $has_totals = false;
  
      /**
       * Constructor.
       *
       * @param array $data {
       *     Optional. Data for populating the Message object.
       *
       *     @type \Protobuf\Gen\ProposalTotalModel\ProposalTotal\Total $total
       *     @type \Protobuf\Gen\ProposalTotalModel\ProposalTotal\TotalAfterTax $total_after_tax
       *     @type bool $has_totals
       * }
       */
      public function __construct($data = NULL) {
          \GPBMetadata\ProposalTotal::initOnce();
          parent::__construct($data);
      }
  
      /**
       * Generated from protobuf field &lt;code&gt;.Protobuf.Gen.ProposalTotalModel.ProposalTotal.Total total = 1;&lt;/code&gt;
       * @return \Protobuf\Gen\ProposalTotalModel\ProposalTotal\Total
       */
      public function getTotal()
      {
          return $this-&gt;total;
      }
  
      /**
       * Generated from protobuf field &lt;code&gt;.Protobuf.Gen.ProposalTotalModel.ProposalTotal.Total total = 1;&lt;/code&gt;
       * @param \Protobuf\Gen\ProposalTotalModel\ProposalTotal\Total $var
       * @return $this
       */
      public function setTotal($var)
      {
          GPBUtil::checkMessage($var, \Protobuf\Gen\ProposalTotalModel\ProposalTotal_Total::class);
          $this-&gt;total = $var;
  
          return $this;
      }
  
      /**
       * Generated from protobuf field &lt;code&gt;.Protobuf.Gen.ProposalTotalModel.ProposalTotal.TotalAfterTax total_after_tax = 2;&lt;/code&gt;
       * @return \Protobuf\Gen\ProposalTotalModel\ProposalTotal\TotalAfterTax
       */
      public function getTotalAfterTax()
      {
          return $this-&gt;total_after_tax;
      }
  
      /**
       * Generated from protobuf field &lt;code&gt;.Protobuf.Gen.ProposalTotalModel.ProposalTotal.TotalAfterTax total_after_tax = 2;&lt;/code&gt;
       * @param \Protobuf\Gen\ProposalTotalModel\ProposalTotal\TotalAfterTax $var
       * @return $this
       */
      public function setTotalAfterTax($var)
      {
          GPBUtil::checkMessage($var, \Protobuf\Gen\ProposalTotalModel\ProposalTotal_TotalAfterTax::class);
          $this-&gt;total_after_tax = $var;
  
          return $this;
      }
  
      /**
       * Generated from protobuf field &lt;code&gt;bool has_totals = 3;&lt;/code&gt;
       * @return bool
       */
      public function getHasTotals()
      {
          return $this-&gt;has_totals;
      }
  
      /**
       * Generated from protobuf field &lt;code&gt;bool has_totals = 3;&lt;/code&gt;
       * @param bool $var
       * @return $this
       */
      public function setHasTotals($var)
      {
          GPBUtil::checkBool($var);
          $this-&gt;has_totals = $var;
  
          return $this;
      }
  
  }
  </code></pre>
  <p>I was able to convert my JSON object to a Protobuf object for storage in Redis on doing a <code>POST</code> request</p>
  <pre><code>$total = new \Protobuf\Gen\ProposalTotalModel\ProposalTotal( );
  $total-&gt;mergeFromJsonString( $body, false );
  </code></pre>
  <p>Then, for a <code>GET</code> request, I want to take this object stored in Redis and send it back to the client. I know that in other languages, we could use <code>JsonFormat.printer().print(obj);</code>. But I could not find similar functionality in PHP to do so. Is it possible to generate JSON automatically or do I have to manually construct the JSON by getting all the getter fields from the Protobuf object?</p>
  <p><code>json_encode</code> is out of scope as the generated Php file properties are private.</p>