Created
March 20, 2016 16:24
-
-
Save zajca/3e3cf14db8ea9c67e4da to your computer and use it in GitHub Desktop.
Typescript class "serializer"
This file contains hidden or 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
let classMap = new WeakMap(); | |
function serialize(serializedGroups: array<string>, serializedName: string) { | |
return function(target: any, key: string, descriptor: any) { | |
let map = classMap.get(target); | |
if (map === undefined) { | |
map = new Map(); | |
} | |
if (map.has(key)) { | |
throw 'Double definition'; | |
} | |
map.set(key,{ | |
name: serializedName||key, | |
groups: serializedGroups | |
}) | |
classMap.set(target,map); | |
} | |
} | |
class Serializer { | |
serialize(_class: Object, groups: array<string>,maximumNestingLevel: int) { | |
groups = new Set(groups); | |
let map = classMap.get(Object.getPrototypeOf(_class)); | |
if (map === undefined) { | |
throw 'Missing annotation'; | |
} | |
let result = {}; | |
//go over prop | |
map.forEach((value, key) => { | |
//test if serialize certain value | |
let intersection = value.groups.filter(x => groups.has(x)); | |
if(intersection.length > 0) { | |
if (_class[key] === null || _class[key] === undefined) { | |
result[value['name']] = null; | |
}else{ | |
let inheritedObject = classMap.get(Object.getPrototypeOf(_class[key])); | |
if (inheritedObject !== undefined) { | |
if (maximumNestingLevel > 0) { | |
result[value['name']] = this.serialize(_class[key],groups,maximumNestingLevel--); | |
} | |
} else { | |
switch(typeof _class[key]) { | |
case ('function'): | |
result[value['name']] = _class[key].apply(_class); | |
break; | |
default: | |
result[value['name']] = _class[key]; | |
break; | |
} | |
} | |
} | |
} | |
}) | |
return Object.getOwnPropertyNames(result).length > 0 ? result : null; | |
} | |
} | |
class Address{ | |
@serialize(['address']) | |
city: string; | |
constructor(city: string){ | |
this.city = city; | |
} | |
} | |
class Person{ | |
@serialize(['xxx','x2'],'rename') | |
name: string = 'name prop string'; | |
@serialize(['x2']) | |
address: Address; | |
public static staticName: string = 'test'; | |
constructor(name: string) { | |
this.name = name; | |
} | |
setAddress(adress: Adress){ | |
this.address = adress; | |
} | |
@serialize(['x2'],'nastyName') | |
uglyName() { | |
return `Ugly ${this.name}`; | |
} | |
test() { | |
return `Ugly ${this.name}`; | |
} | |
} | |
let john = new Person('John'); | |
// // john.uglyName(); | |
let serializer = new Serializer() | |
let serialized = serializer.serialize(john, ['x2'],2); | |
console.log(serialized); | |
let address = new Address('Brno'); | |
john.setAddress(address); | |
let serialized2 = serializer.serialize(john, ['x2'],2); | |
console.log(serialized2); | |
// console.log(john, Person.test); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment