class User {
userid: number;
username: string;
age: number;
password: string; // should not be serialized
constructor(userid: number, username: string, age: number, password: string) {
this.userid = userid;
this.username = username;
this.age = age;
this.password = password;
}
}
const user: User = new User(9382732, "A AI", 52, "ah@746Eo87R!");
// Convert class instance to JSON
const jsonString: string = JSON.stringify(user, (key, value) => {
if (key === "password") {
return undefined; // Exclude this key
}
return value;
});
console.log("JSON String:", jsonString);
/*
run:
"JSON String:", "{"userid":9382732,"username":"A AI","age":52}"
*/