8000 Custom converter fromJson on property level. · hubcarl/json-typescript-mapper@f554666 · GitHub
[go: up one dir, main page]

Skip to content

Commit f554666

Browse files
Mönnich Daniel, ENT-BD-PDDMönnich Daniel, ENT-BD-PDD
Mönnich Daniel, ENT-BD-PDD
authored and
Mönnich Daniel, ENT-BD-PDD
committed
Custom converter fromJson on property level.
1 parent 9c6ad3b commit f554666

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

index.js

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const JSON_META_DATA_KEY = 'JsonProperty';
1616
*/
1717
export interface IDecoratorMetaData<T> {
1818
name?: string,
19-
clazz?: {new(): T}
19+
clazz?: {new(): T},
20+
fromJson?: (data: any) => any
2021
}
2122

2223
/**
@@ -154,10 +155,16 @@ export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
154155
* get decoratorMetaData, structure: { name?:string, clazz?:{ new():T } }
155156
*/
156157
let decoratorMetaData = getJsonProperty(instance, key);
158+
157159
/**
158160
* pass value to instance
159161
*/
160-
instance[key] = decoratorMetaData ? mapFromJson(decoratorMetaData, instance, json, key) : json[key];
162+
if (decoratorMetaData && decoratorMetaData.fromJson) {
163+
instance[key] = decoratorMetaData.fromJson(json[key]);
164+
} else {
165+
instance[key] = decoratorMetaData ? mapFromJson(decoratorMetaData, instance, json, key) : json[key];
166+
}
167+
161168
});
162169

163170
return instance;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"mocha": "2.0.1"
1313
},
1414
"scripts": {
15-
"test": "mocha ./spec/index.js"
15+
"test": "mocha ./spec/*.js"
1616
},
1717
"description": "For single page application, data sources are obtained from API server. Instead of directly using api data, we \r definitely require an adapter layer to transform data as needed. Furthermore, \r the adapter inverse the the data dependency from API server(API Server is considered uncontrollable and \r highly unreliable as data structure may be edit by backend coder for some specific purposes)to our adapter \r which becomes reliable. Thus, this library is created as the adapter make use of es7 reflect decorator.",
1818
"main": "index.js",
@@ -34,4 +34,4 @@
3434
"url": "https://github.com/jf3096/json-typescript-mapper/issues"
3535
},
3636
"homepage": "https://github.com/jf3096/json-typescript-mapper#readme"
37-
}
37+
}

spec/custom-converter.js

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/custom-converter.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {expect} from 'chai';
2+
import {deserialize, JsonProperty} from '../index';
3+
4+
const dateConverter = function (date: any): any {
5+
return new Date(date);
6+
};
7+
8+
class Student {
9+
@JsonProperty
10+
name: string = undefined;
11+
12+
@JsonProperty({fromJson: dateConverter})
13+
dateOfBirth: Date = undefined;
14+
15+
constructor() {}
16+
}
17+
18+
describe('custom-converter', function () {
19+
it('simple json object', function () {
20+
let json = {
21+
"name": "Mark",
22+
"xing": "Galea",
23+
"age": 30,
24+
"AddressArr": [],
25+
"Address": null,
26+
dateOfBirth: "1995-11-10"
27+
};
28+
const student = deserialize(Student, json);
29+
expect(student.name).to.be.equals('Mark');
30+
expect(student.dateOfBirth).to.be.instanceof(Date);
31+
});
32+
33+
34+
});

0 commit comments

Comments
 (0)
0