- Notifications
You must be signed in to change notification settings - Fork1
a solution for adapter layer which make use of typescript reflection mapping from pure server-returned json to target typescript model
License
hubcarl/json-typescript-mapper
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
For single page application, data sources are obtained from API server. Instead of directly using api data, wedefinitely require an adapter layer to transform data as needed. Furthermore,the adapter inverse the the data dependency from API server(API Server is considered uncontrollable andhighly unreliable as data structure may be edit by backend coder for some specific purposes)to our adapterwhich becomes reliable. Thus, this library is created as the adapter.
npm install json-typescript-mapper --save
- NodeJS
- Browser
- Typescript
import {deserialize} from'json-typescript-mapper';deserialize(<Class Type>,<JSON Object>);serialize(<Object>);Here is a complex example, hopefully could give you an idea of how to use it (for more on how to use, checkout /spec which are unit test cases):
classStudent{ @JsonProperty('name')fullName:string;constructor(){this.fullName=undefined;}}classAddress{ @JsonProperty('first-line')firstLine:string; @JsonProperty('second-line')secondLine:string; @JsonProperty({clazz:Student})student:Student;city:string;constructor(){this.firstLine=undefined;this.secondLine=undefined;this.city=undefined;this.student=undefined}}classPerson{ @JsonProperty('Name')name:string; @JsonProperty('xing')surname:string;age:number; @JsonProperty({clazz:Address,name:'AddressArr'})addressArr:Address[]; @JsonProperty({clazz:Address,name:'Address'})address:Address;constructor(){this.name=void0;this.surname=void0;this.age=void0;this.addressArr=void0;this.address=void0;}}
Now here is what API server return, assume it is already parsed to JSON object.
letjson={"Name":"Mark","xing":"Galea","age":30,"AddressArr":[{"first-line":"Some where","second-line":"Over Here","city":"In This City","student":{name1:"Ailun"}},{"first-line":"Some where","second-line":"Over Here","city":"In This City","student":{name1:"Ailun"}}],"Address":{"first-line":"Some where","second-line":"Over Here","city":"In This City","student":{name:"Ailun"}}
Simply, just map it use following code. The mapping is based on <@JsonProperty> decorator meta data.
constperson=deserialize(Person,json);
If you want to reverse the action, from the other way round:
constjson=serialize(person);
Remember to add:experimentalDecorators andemitDecoratorMetadata in your tsconfig.json.This is essential to enable decorator support for your typescript program. Example shown as followings:
{"compilerOptions": {"module":"commonjs","target":"es5","sourceMap":true,"experimentalDecorators":true,"emitDecoratorMetadata":true },"exclude": ["node_modules" ]}The test case will be covered in the next push. This caused by inconsistent return type.
- Fixed test cases. According to typescript official website tipsNULL IS BAD,therefore I updated all null value to void 0 which is a better expression than undefined (idea from underscore source code).Most cases it won't affect previous version at all.
json-typescript-mapper 1.1.1
- Added serialized function
- Passed more unit tests
About
a solution for adapter layer which make use of typescript reflection mapping from pure server-returned json to target typescript model
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- TypeScript100.0%