I am currently manually going through the properties of my javascript class to create JSON, as shown below. It feels cumbersome and I am looking to automate this process so that I don't have to make changes to the 'toJson' function every time I add or remove properties.
Can someone guide me on how to modify the 'toJson' function below to achieve this goal?
Thank you in advance for your help.
/* Utilizing Simple JavaScript Inheritance
* Created by John Resig http://ejohn.org/
* Licensed under MIT.*/
var LogEntry = Class.extend({
init: function (_conferenceId, _tokenId, _logType, _logValue) {
this.dato = new Date();
this.logValue = _logValue;
this.logType = _logType;
this.conferenceId = _conferenceId;
this.tokenId = _tokenId;
},
toJson: function () {
// ?
var jsonStringBuilder = '{ ';
jsonStringBuilder += '"dato": ' + this.dato.toString() + ',';
jsonStringBuilder += '"conferenceId": ' + this.conferenceId + ',';
if (this.tokenId== null) {
jsonStringBuilder += '"tokenId":null,';
}
else {
jsonStringBuilder += '"tokenId": ' + _tokenId + ',';
}
jsonStringBuilder += '"logValue": ' + this.logValue + ',';
jsonStringBuilder += '"logType": ' + this.logType;
jsonStringBuilder += '}';
return jsonStringBuilder;
}
});