Consider following class in JavaScript:
Tools.UserBase = Tools.Class.define("Tools.UserBase", Tools.EntityBase, {
UserId: { type: System.Int32, key: true, computed: true },
IsActive: { type: System.Boolean },
IsAdmin: { type: System.Boolean },
UserName: { type: System.String },
UserToken: { type: System.Guid },
init: function () {
Tools.EntityBase.call(this, arguments);
},
onEndEdit: function () {
if (this.IsActive == false && this.IsAdmin == true) {
throw new Error("Can't disable admin user");
}
this.parentClass.onEndEdit();
}
});
When I execute this code:
var user = new Tools.UserBase()
I'll get following results:
UserBase {
IsActive: false
IsAdmin: false
UserId: 0
UserName: ""
UserToken: "00000000-0000-0000-0000-000000000000"
__BackingField__IsActive: false
__BackingField__IsAdmin: false
__BackingField__UserId: 0
__BackingField__UserName: ""
__BackingField__UserToken: "00000000-0000-0000-0000-000000000000"
__proto__: PrototypeConstructor }
Then I use following command to create json from user object.
JSON.stringify(user)
And I get following results:
""__BackingField__UserId":0,"__BackingField__IsActive":false,"__BackingField__IsAdmin":false,"__BackingField__UserName":"","__BackingField__UserToken":"00000000-0000-0000-0000-000000000000"}"
As you can see it serialize my object with its fields instead of its properties.
And I've no control on serialization process at all.
The deserialization process is the same, JSON.parse will create a plain object instead of typed objects. (I'm not saying that it should do what I want, I'm looking for a solution for my situation)
Is there any JavaScript library which fits my needs? as like as Newtonsoft in .NET ?
Thanks in advance.