Created
October 23, 2017 08:48
-
-
Save peter/a4a8687e6d00444a20a93c8db40e486b to your computer and use it in GitHub Desktop.
Static typing and JSON mapping (in TypeScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class User { | |
id: string | |
country: string | |
email: string | |
name: string | |
tags: string[] | |
verified: boolean | |
active: boolean | |
locale: string | |
openIds: any | |
parentalControl: boolean | |
parentalControlPinCode: string | |
zipCode: string | |
mobilePhone: string | |
createdTimestamp: string | |
static fromJson(json: any): User { | |
let user = new User() | |
user.id = json.userId | |
user.country = json.country | |
user.email = json.email | |
user.name = json.name | |
user.tags = json.tags | |
user.verified = json.verified | |
user.active = json.active | |
user.locale = json.locale | |
user.openIds = json.openIds | |
user.parentalControl = json.parentalControl | |
user.mobilePhone = json.mobilePhone | |
user.zipCode = json.zipCode | |
user.parentalControlPinCode = json.parentalControlPinCode | |
user.createdTimestamp = json.createdTimestamp | |
console.log(json) | |
return user | |
} | |
toJson(): any { | |
let json = {} | |
json['userId'] = this.id | |
json['name'] = this.name | |
json['email'] = this.email | |
json['country'] = this.country | |
json['tags'] = this.tags | |
json['verified'] = this.verified | |
json['active'] = this.active | |
json['locale'] = this.locale | |
json['openIds'] = this.openIds | |
json['parentalControl'] = this.parentalControl | |
json['mobilePhone'] = this.mobilePhone | |
json['zipCode'] = this.zipCode | |
json['parentalControlPinCode'] = this.parentalControlPinCode | |
json['createdTimestamp'] = this.createdTimestamp | |
return json | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yikes, to do this for for every different piece of JSON? seems like there is a better way