Currently in the process of developing an angular application that interacts with the twitch API. The API returns data in various formats, some of which I need to parse and save into specific classes.
My main concern is understanding the potential drawbacks of using Object.assign
to create an instance of a class as opposed to manually assigning properties from deserialized JSON data?
Here's what I have implemented so far:
export class UserDetails implements Serializable<UserDetails>{
public _id: number;
public bio: string;
public created_at: string;
public display_name: string;
public email: string;
public email_verified: boolean;
public logo: string;
public name: string;
public notifications: Object;
public partnered: boolean;
public twitter_connected: boolean;
public type: string;
public updated_at: string;
constructor() {}
deserialize(input: any) {
this._id = input._id;
this.bio = input.bio;
this.created_at = input.created_at;
this.display_name = input.display_name;
this.email = input.email;
this.email_verified = input.email_verified;
this.logo = input.logo;
this.name = input.name;
this.notifications = input.notifications;
this.partnered = input.partnered;
this.twitter_connected = input.twitter_connected;
this.type = input.type;
this.updated_at = input.updated_at;
return this;
}
}
An alternative approach using Object.assign
:
export class UserDetails implements Serializable<UserDetails>{
constructor() {}
deserialize(input: any) {
Object.assign(this, input);
return this;
}
}
I am now faced with creating a new class that will have approximately 70 properties... Is this the most efficient method?
On Second Thought... Or should I consider a combination of both approaches for enhanced intellisense assistance?
export class UserDetails implements Serializable<UserDetails>{
public _id: number;
public bio: string;
public created_at: string;
public display_name: string;
public email: string;
public email_verified: boolean;
public logo: string;
public name: string;
public notifications: Object;
public partnered: boolean;
public twitter_connected: boolean;
public type: string;
public updated_at: string;
constructor() {}
deserialize(input: any) {
Object.assign(this, input);
return this;
}
}