Currently, I am developing an AngularJS application (v1.3.15) using TypeScript 1.5 in Visual Studio 2013. I have encountered a challenge related to TypeScript object properties and JSON serialization while utilizing $http.post(). Although I consider myself proficient in AngularJS, I am relatively new to TypeScript.
The structure of my TypeScript class is outlined below:
module Wizard.Models {
import Address = Wizard.Models.Address;
"use strict";
export class YourDetailsModel {
public useSecondaryAsPrimary: boolean;
private _primaryFirstName: string;
private _primaryLastName: string;
private _primaryAddressModel: Address = new Models.Address();
get primaryFirstName(): string {
return !this.useSecondaryAsPrimary ? this._primaryFirstName : this.SecondaryFirstName;
}
set primaryFirstName(primaryFirstName: string) {
this._primaryFirstName = primaryFirstName;
}
get primaryLastName(): string {
return !this.useSecondaryAsPrimary ? this._primaryLastName : this.SecondaryLastName;
}
set primaryLastName(primaryLastName: string) {
this._primaryLastName = primaryLastName;
}
get primaryAddressModel(): Address {
return !this.useSecondaryAsPrimary ? this._primaryAddressModel : this.SecondaryAddressModel;
}
set primaryAddressModel(primaryAddressModel: Address) {
this._primaryAddressModel = primaryAddressModel;
}
public SecondaryFirstName: string;
public SecondaryLastName: string;
public SecondaryAddressModel: Address = new Models.Address();
}
}
My goal is to ensure that when the object is serialized, only public members and properties accessed through accessors are included in the serialization process, excluding private properties. However, the current behavior indicates that certain private members are being serialized, while some public ones are not.
Do you think my expectation is unrealistic? There may be alternate methods to achieve the desired outcome, so it is not critical if I cannot resolve this issue. Modifying the model class in this manner is not imperative for my app functionality.
Nevertheless, maintaining the confidentiality of the model's information is a subtle yet elegant approach that I aim to uphold.
Any assistance would be greatly appreciated. M.