title: Angular 2 (or 4) object serialization
body: <p>Recently come across the following conundrum:</p>
  
  <p><strong>Standards</strong></p>
  
  <p>The <a href="https://www.typescriptlang.org/docs/handbook/classes.html#public-private-and-protected-modifiers" rel="noreferrer">Typescript documentation</a> suggests creating object definitions with private members having an underscore prefix, allowing getters/setters to utilize the original 'name' of said members, providing indirect public access (as they should).</p>
  
  <p>The <a href="https://angular.io/styleguide#!#03-04" rel="noreferrer">Angular documentation</a> recommends otherwise, however does not provide an example of using getters &amp; setters for the private variable in their object definition example.</p>
  
  <p><strong>The Issue</strong></p>
  
  <p>I've been following the Typescript coding standard to the most extent. Take the following class for example:</p>
  
  <pre><code>public class Foo{
      private _bar:string;
  
      constructor(){ this._bar='Baz'; }
  
      get bar():string{return this._bar}
  }
  </code></pre>
  
  <p>Using the following code to serialize it into JSON:</p>
  
  <pre><code>console.log(JSON.stringify(new Foo()));
  </code></pre>
  
  <p>...will produce:</p>
  
  <pre><code>{
      '_bar': 'Baz'
  }
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <ol>
  <li>Taking into account these are 'guidelines' and simply recommendations, what would be the Angular-way of handling an object definition, allowing for direct serialization?</li>
  <li>In my example, surely the private access modifier shouldn't allow direct access to the value of it? What have I missed in my understanding of using the private access modifier here?</li>
  </ol>
  
  <p>I've done a fair share of reading to other articles and StackOverflow posts regarding different ways of handling serialization, want to deter from the 'how', and rather ask 'why' with regards to the behavior mentioned.</p>
  
  <p>Any feedback would be greatly appreciated! :)</p>
  
  <p>Kind regards</p>