JSON stringify ES6 class property with getter/setter : <p>I have a JavaScript ES6 class that has a property set with <code>set</code> and accessed with <code>get</code> functions. It is also a constructor parameter so the class can be instantiated with said property.</p>

<pre><code>class MyClass {
  constructor(property) {
    this.property = property
  }

  set property(prop) {
  // Some validation etc.
  this._property = prop
  }

  get property() {
    return this._property
  }
}
</code></pre>

<p>I use <code>_property</code> to escape the JS gotcha of using get/set that results in an infinite loop if I set directly to <code>property</code>.</p>

<p>Now I need to stringify an instance of MyClass to send it with a HTTP request. The stringified JSON is an object like:</p>

<pre><code>{
   //...
   _property:
}
</code></pre>

<p>I need the resulting JSON string to preserve <code>property</code> so the service I am sending it to can parse it correctly. I also need <code>property</code> to remain in the constructor because I need to construct instances of MyClass from JSON sent by the service (which is sending objects with <code>property</code> not <code>_property</code>).</p>

<p>How do I get around this? Should I just intercept the MyClass instance before sending it to the HTTP request and mutate <code>_property</code> to <code>property</code> using regex? This seems ugly, but I will be able to keep my current code.</p>

<p>Alternatively I can intercept the JSON being sent to the client from the service and instantiate MyClass with a totally different property name. However this means a different representation of the class either side of the service.</p>
