title: Using javascript, how convert to JSON, Java-POJO-like classes?
body: <p>I want use Java-POJO-like classes in my JS project: only private fields, getters and setters.</p>
  
  <pre><code>function Event() {
  var homeTeam;
  var awayTeam;
  var id;
  var singleBets = [];
  var bet;
  
  ...
  
  this.getHomeTeam = function(){
      return homeTeam;
  }
  this.setHomeTeam = function(data){
      homeTeam = data;
  }
  
  this.getAwayTeam = function(){
      return awayTeam;
  }
  this.setAwayTeam = function(data){
      awayTeam = data;
  }
  
  this.getId = function(){
      return id;
  }
  this.setId = function(data){
      id = data;
  }
  
  this.getSingleBets = function(){
      return singleBets;
  }
  this.setSingleBets = function(data){
      singleBets = data;
  }
  
  this.getBet = function(){
      return bet;
  }
  this.setBet = function(data){
      bet = data;
  }
  
  }
  </code></pre>
  
  <p>I would convert my Java-POJO-like class in a JSON string to send it to the server.
  I can't use JSON.stringify(myClass) because the fields are private.
  So I think to use a recursive method like this...</p>
  
  <pre><code>var myJsonParse = function(data){
      var result = "";
      for(var propertyName in data) {
          var method = propertyName;
          if (method.substring(0,3)=="get"){
              ... data[propertyName].call() ...
          }
      }
      return result;
  }
  </code></pre>
  
  <p>...but before I spend many time to write it I want ask you if exist a better way to convert the Java-POJO-like object in JSON.
  Thank you.</p>
  
  <p><strong>Update #1</strong></p>
  
  <p>I am ashamed of myself! This is my working (but absolutely temporary) solution:</p>
  
  <pre><code>this.toJSON = function() {
      var bettableEventsString =  [];
      for (var i = 0, len = bettableEvents.length; i &lt; len; i++) {
          bettableEventsString[i] = bettableEvents[i].toJSON();
      }
      var result = JSON.stringify({
          userId: userId,
          championshipDayId: championshipDayId,
          championshipDayName: championshipDayName
      });
  
      result = result.substring(0, result.length-1);
      result += ',\n  "bettableEvents" : [ ' + bettableEventsString + " ] ";
      result += " } "
  
      return result;
  }
  </code></pre>