8000 Merge master into testing. · hubcarl/json-typescript-mapper@da581d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit da581d4

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
Merge master into testing.
2 parents f1673b1 + ee29dec commit da581d4

File tree

7 files changed

+75
-17
lines changed

7 files changed

+75
-17
lines changed

index.d.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import 'reflect-metadata';
2+
import { IDecoratorMetaData } from './';
3+
/**
4+
* provide interface to indicate the object is allowed to be traversed
5+
*
6+
* @interface
7+
*/
8+
export interface IGenericObject {
9+
[key: string]: any;
10+
}
11+
/**
12+
* IDecoratorMetaData<T>
13+
* DecoratorConstraint
14+
*
15+
* @interface
16+
*/
17+
export interface IDecoratorMetaData<T> {
18+
name?: string;
19+
clazz?: {
20+
new (): T;
21+
};
22+
}
23+
/**
24+
* JsonProperty
25+
*
26+
* @function
27+
* @property {IDecoratorMetaData<T>|string} metadata, encapsulate it to DecoratorMetaData for standard use
28+
* @return {(target:Object, targetKey:string | symbol)=> void} decorator function
29+
*/
30+
export declare function JsonProperty<T>(metadata?: IDecoratorMetaData<T> | string): (target: Object, targetKey: string | symbol) => void;
31+
/**
32+
* deserialize
33+
*
34+
* @function
35+
* @param {{new():T}} clazz, class type which is going to initialize and hold a mapping json
36+
* @param {Object} json, input json object which to be mapped
37+
*
38+
* @return {T} return mapped object
39+
*/
40+
export declare function deserialize<T extends IGenericObject>(Clazz: {
41+
new (): T;
42+
}, json: IGenericObject): T;

index.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import 'reflect-metadata';
22
import {isTargetType, isPrimitiveOrPrimitiveClass, isArrayOrArrayClass} from './libs/utils';
3+
import {IDecoratorMetaData} from './';
4+
5+
/**
6+
* provide interface to indicate the object is allowed to be traversed
7+
*
8+
* @interface
9+
*/
10+
export interface IGenericObject {
11+
[key: string]: any;
12+
}
13+
314

415
/**
516
* Decorator variable name
@@ -47,10 +58,10 @@ class DecoratorMetaData<T> {
4758
* @return {(target:Object, targetKey:string | symbol)=> void} decorator function
4859
*/
4960
export function JsonProperty<T>(metadata?: IDecoratorMetaData<T>|string): (target: Object, targetKey: string | symbol)=> void {
50-
let decoratorMetaData;
61+
let decoratorMetaData: IDecoratorMetaData<T>;
5162

5263
if (isTargetType(metadata, 'string')) {
53-
decoratorMetaData = new DecoratorMetaData(metadata as string);
64+
decoratorMetaData = new DecoratorMetaData<T>(metadata as string);
5465
}
5566
else if (isTargetType(metadata, 'object')) {
5667
decoratorMetaData = metadata as IDecoratorMetaData<T>;
@@ -97,23 +108,23 @@ function getJsonProperty<T>(target: any, propertyKey: string): IDecoratorMetaDat
97108
* @return {IDecoratorMetaData<T>} check if any arguments is null or undefined
98109
*/
99110
function hasAnyNullOrUndefined(...args: any[]) {
100-
return args.some((arg: any)=>arg === null || arg === undefined);
111+
return args.some((arg: any) => arg === null || arg === undefined);
101112
}
102113

103114

104-
function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T, json: Object, key: any): any {
115+
function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T, json: IGenericObject, key: any): any {
105116
/**
106117
* if decorator name is not found, use target property key as decorator name. It means mapping it directly
107118
*/
108119
let decoratorName = decoratorMetadata.name || key;
109-
let innerJson = json ? json[decoratorName] : undefined;
120+
let innerJson: any = json ? json[decoratorName] : undefined;
110121
let clazz = getClazz(instance, key);
111122
if (isArrayOrArrayClass(clazz)) {
112123
let metadata = getJsonProperty(instance, key);
113124
if (metadata && metadata.clazz || isPrimitiveOrPrimitiveClass(clazz)) {
114125
if (innerJson && isArrayOrArrayClass(innerJson)) {
115126
return innerJson.map(
116-
(item)=> deserialize(metadata.clazz, item)
127+
(item: any) => deserialize(metadata.clazz, item)
117128
);
118129
}
119130
return;
@@ -138,7 +149,7 @@ function mapFromJson<T>(decoratorMetadata: IDecoratorMetaData<any>, instance: T,
138149
*
139150
* @return {T} return mapped object
140151
*/
141-
export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
152+
export function deserialize<T extends IGenericObject>(Clazz: {new(): T}, json: IGenericObject): T {
142153
/**
143154
* As it is a recursive function, ignore any arguments that are unset
144155
*/
@@ -157,7 +168,7 @@ export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
157168
*/
158169
let instance = new Clazz();
159170

160-
Object.keys(instance).forEach((key: any) => {
171+
Object.keys(instance).forEach((key: string) => {
161172
/**
162173
* get decoratorMetaData, structure: { name?:string, clazz?:{ new():T } }
163174
*/

libs/utils.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare function isTargetType(val: any, type: "object" | "string"): boolean;
2+
export declare function isPrimitiveOrPrimitiveClass(obj: any): boolean;
3+
export declare function isArrayOrArrayClass(clazz: Function): boolean;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"mocha": "2.0.1"
1313
},
1414
"scripts": {
15-
"test": "mocha ./spec/*.js"
15+
"test": "mocha ./spec/*.js",
16+
"typings:generate": "tsc --declaration"
1617
},
1718
"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.",
1819
"main": "index.js",

spec/index.d.ts

Whitespace-only changes.

spec/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ describe('index()', function () {
5858
"Name": "Mark",
5959
"xing": "Galea",
6060
"age": 30,
61-
"AddressArr": [],
62-
"Address": null
61+
"AddressArr": [] as Array<any>,
62+
"Address": null as any
6363
};
6464
const person = deserialize(Person, json);
6565
expect(person.address).to.be.equals(void 0);
@@ -136,32 +136,32 @@ describe('index()', function () {
136136
});
137137

138138
it('empty json object #2', function () {
139-
let json = null;
139+
let json: any = null;
140140
const person = deserialize(Person, json);
141141
expect(person).to.be.equals(void 0);
142142
});
143143

144144
it('empty json object #3', function () {
145-
let json = void 0;
145+
let json: any = void 0;
146146
const person = deserialize(Person, json);
147147
expect(person).to.be.equals(void 0);
148148
});
149149

150150
it('invalid primitive value #1', function () {
151151
let json = 123;
152-
const person = deserialize(Person, json);
152+
const person = deserialize(Person, json as any);
153153
expect(person).to.be.equals(void 0);
154154
});
155155

156156
it('invalid primitive value #2', function () {
157157
let json = '';
158-
const person = deserialize(Person, json);
158+
const person = deserialize(Person, json as any);
159159
expect(person).to.be.equals(void 0);
160160
});
161161

162162
it('invalid primitive value #3', function () {
163163
let json = NaN;
164-
const person = deserialize(Person, json);
164+
const person = deserialize(Person, json as any);
165165
expect(person).to.be.equals(void 0);
166166
});
167167

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"target": "es5",
55
"sourceMap": true,
66
"experimentalDecorators": true,
7-
"emitDecoratorMetadata": true
7+
"emitDecoratorMetadata": true,
8+
"noImplicitAny": true
89
},
910
"exclude": [
1011
"node_modules"

0 commit comments

Comments
 (0)
0