-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
192 lines (192 loc) · 5.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
require('reflect-metadata');
var utils_1 = require('./libs/utils');
/**
* Decorator variable name
*
* @const
*/
var JSON_META_DATA_KEY = 'JsonProperty';
/**
* DecoratorMetaData
* Model used for decoration parameters
*
* @class
* @property {string} name, indicate which json property needed to map
* @property {string} clazz, if the target is not primitive type, map it to corresponding class
*/
var DecoratorMetaData = (function () {
function DecoratorMetaData(name, clazz) {
this.name = name;
this.clazz = clazz;
}
return DecoratorMetaData;
}());
/**
* JsonProperty
*
* @function
* @property {IDecoratorMetaData<T>|string} metadata, encapsulate it to DecoratorMetaData for standard use
* @return {(target:Object, targetKey:string | symbol)=> void} decorator function
*/
function JsonProperty(metadata) {
var decoratorMetaData;
if (utils_1.isTargetType(metadata, 'string')) {
decoratorMetaData = new DecoratorMetaData(metadata);
}
else if (utils_1.isTargetType(metadata, 'object')) {
decoratorMetaData = metadata;
}
else {
throw new Error('index.ts: meta data in Json property is undefined. meta data: ' + metadata);
}
return Reflect.metadata(JSON_META_DATA_KEY, decoratorMetaData);
}
exports.JsonProperty = JsonProperty;
/**
* getClazz
*
* @function
* @property {any} target object
* @property {string} propertyKey, used as target property
* @return {Function} Function/Class indicate the target property type
* @description Used for type checking, if it is not primitive type, loop inside recursively
*/
function getClazz(target, propertyKey) {
return Reflect.getMetadata('design:type', target, propertyKey);
}
/**
* getJsonProperty
*
* @function
* @property {any} target object
* @property {string} propertyKey, used as target property
* @return {IDecoratorMetaData<T>} Obtain target property decorator meta data
*/
function getJsonProperty(target, propertyKey) {
return Reflect.getMetadata(JSON_META_DATA_KEY, target, propertyKey);
}
/**
* hasAnyNullOrUndefined
*
* @function
* @property {...args:any[]} any arguments
* @return {IDecoratorMetaData<T>} check if any arguments is null or undefined
*/
function hasAnyNullOrUndefined() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i - 0] = arguments[_i];
}
return args.some(function (arg) { return arg === null || arg === undefined; });
}
function mapFromJson(decoratorMetadata, instance, json, key) {
/**
* if decorator name is not found, use target property key as decorator name. It means mapping it directly
*/
var decoratorName = decoratorMetadata.name || key;
var innerJson = json ? json[decoratorName] : undefined;
var clazz = getClazz(instance, key);
if (utils_1.isArrayOrArrayClass(clazz)) {
var metadata_1 = getJsonProperty(instance, key);
if (metadata_1 && metadata_1.clazz || utils_1.isPrimitiveOrPrimitiveClass(clazz)) {
if (innerJson && utils_1.isArrayOrArrayClass(innerJson)) {
return innerJson.map(function (item) { return deserialize(metadata_1.clazz, item); });
}
return;
}
else {
return innerJson;
}
}
if (!utils_1.isPrimitiveOrPrimitiveClass(clazz)) {
return deserialize(clazz, innerJson);
}
return json ? json[decoratorName] : undefined;
}
/**
* deserialize
*
* @function
* @param {{new():T}} clazz, class type which is going to initialize and hold a mapping json
* @param {Object} json, input json object which to be mapped
*
* @return {T} return mapped object
*/
function deserialize(Clazz, json) {
/**
* As it is a recursive function, ignore any arguments that are unset
*/
if (hasAnyNullOrUndefined(Clazz, json)) {
return void 0;
}
/**
* Prevent non-json continue
*/
if (!utils_1.isTargetType(json, 'object')) {
return void 0;
}
/**
* init root class to contain json
*/
var instance = new Clazz();
Object.keys(instance).forEach(function (key) {
/**
* get decoratorMetaData, structure: { name?:string, clazz?:{ new():T } }
*/
var decoratorMetaData = getJsonProperty(instance, key);
/**
* pass value to instance
*/
if (decoratorMetaData && decoratorMetaData.customConverter) {
instance[key] = decoratorMetaData.customConverter.fromJson(json[decoratorMetaData.name || key]);
}
else {
instance[key] = decoratorMetaData ? mapFromJson(decoratorMetaData, instance, json, key) : json[key];
}
});
return instance;
}
exports.deserialize = deserialize;
/**
* Serialize: Creates a ready-for-json-serialization object from the provided model instance.
* Only @JsonProperty decorated properties in the model instance are processed.
*
* @param instance an instance of a model class
* @returns {any} an object ready to be serialized to JSON
*/
function serialize(instance) {
if (!utils_1.isTargetType(instance, 'object') || utils_1.isArrayOrArrayClass(instance)) {
return instance;
}
var obj = {};
Object.keys(instance).forEach(function (key) {
var metadata = getJsonProperty(instance, key);
obj[metadata && metadata.name ? metadata.name : key] = serializeProperty(metadata, instance[key]);
});
return obj;
}
exports.serialize = serialize;
/**
* Prepare a single property to be serialized to JSON.
*
* @param metadata
* @param prop
* @returns {any}
*/
function serializeProperty(metadata, prop) {
if (!metadata || metadata.excludeToJson === true) {
return;
}
if (metadata.customConverter) {
return metadata.customConverter.toJson(prop);
}
if (!metadata.clazz) {
return prop;
}
if (utils_1.isArrayOrArrayClass(prop)) {
return prop.map(function (propItem) { return serialize(propItem); });
}
return serialize(prop);
}
//# sourceMappingURL=index.js.map