Skip to content

Instantly share code, notes, and snippets.

@peter
Created October 23, 2017 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter/a4a8687e6d00444a20a93c8db40e486b to your computer and use it in GitHub Desktop.
Save peter/a4a8687e6d00444a20a93c8db40e486b to your computer and use it in GitHub Desktop.
Static typing and JSON mapping (in TypeScript)
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
}
}
@ORESoftware
Copy link

yikes, to do this for for every different piece of JSON? seems like there is a better way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment