serializer.ts
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -0,1 +1,2 @@ | ||
import "reflect-metadata"; | ||
/** | ||
@@ -2,0 +3,0 @@ * Specifies a type of the property. Property needs to known about its type |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
require("reflect-metadata"); | ||
@@ -2,0 +3,0 @@ var MetadataStorage_1 = require("./metadata/MetadataStorage"); |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
@@ -17,4 +18,4 @@ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
return TypeMissingError; | ||
})(Error); | ||
}(Error)); | ||
exports.TypeMissingError = TypeMissingError; | ||
//# sourceMappingURL=TypeMissingError.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
/** | ||
@@ -35,3 +36,3 @@ * Storage all serializer metadata. | ||
return MetadataStorage; | ||
})(); | ||
}()); | ||
exports.MetadataStorage = MetadataStorage; | ||
@@ -38,0 +39,0 @@ /** |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var PropertyMetadata = (function () { | ||
@@ -21,4 +22,4 @@ function PropertyMetadata(target, key) { | ||
return PropertyMetadata; | ||
})(); | ||
}()); | ||
exports.PropertyMetadata = PropertyMetadata; | ||
//# sourceMappingURL=PropertyMetadata.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var SerializableMetadata = (function () { | ||
@@ -21,4 +22,4 @@ function SerializableMetadata(target, params) { | ||
return SerializableMetadata; | ||
})(); | ||
}()); | ||
exports.SerializableMetadata = SerializableMetadata; | ||
//# sourceMappingURL=SerializableMetadata.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
@@ -35,4 +36,4 @@ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
return SkipMetadata; | ||
})(PropertyMetadata_1.PropertyMetadata); | ||
}(PropertyMetadata_1.PropertyMetadata)); | ||
exports.SkipMetadata = SkipMetadata; | ||
//# sourceMappingURL=SkipMetadata.js.map |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
@@ -29,4 +30,4 @@ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
return TypeMetadata; | ||
})(PropertyMetadata_1.PropertyMetadata); | ||
}(PropertyMetadata_1.PropertyMetadata)); | ||
exports.TypeMetadata = TypeMetadata; | ||
//# sourceMappingURL=TypeMetadata.js.map |
{ | ||
"name": "serializer.ts", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Proper serialization and deserialization raw json objects to classes in Typescript", | ||
@@ -32,7 +32,7 @@ "license": "Apache-2.0", | ||
"del": "^2.2.0", | ||
"gulp": "^3.9.0", | ||
"gulp": "^3.9.1", | ||
"gulp-mocha": "^2.2.0", | ||
"gulp-replace": "^0.5.4", | ||
"gulp-shell": "^0.5.1", | ||
"gulp-tslint": "^4.3.1", | ||
"gulp-tslint": "^4.3.3", | ||
"gulpclass": "0.1.0", | ||
@@ -44,5 +44,5 @@ "mocha": "^2.3.2", | ||
"tslint-stylish": "^2.1.0-beta", | ||
"typescript": "^1.8.0", | ||
"typings": "^0.6.6" | ||
"typescript": "^1.8.9", | ||
"typings": "^0.7.9" | ||
} | ||
} |
@@ -0,5 +1,8 @@ | ||
export interface SerializerOptions { | ||
skipStartedWith: string; | ||
} | ||
export declare class Serializer { | ||
serialize<T>(object: T): any; | ||
deserialize<T>(cls: Function, json: any): T; | ||
private convert(cls, object, operationType); | ||
serialize<T>(object: T, options?: SerializerOptions): any; | ||
deserialize<T>(cls: Function, json: any, options?: SerializerOptions): T; | ||
private convert(cls, object, operationType, options?); | ||
private isSkipped(target, propertyName, operationType); | ||
@@ -6,0 +9,0 @@ private getType(target, propertyName); |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var MetadataStorage_1 = require("./metadata/MetadataStorage"); | ||
@@ -9,3 +10,3 @@ var TypeMissingError_1 = require("./error/TypeMissingError"); | ||
// ------------------------------------------------------------------------- | ||
Serializer.prototype.serialize = function (object) { | ||
Serializer.prototype.serialize = function (object, options) { | ||
var _this = this; | ||
@@ -17,6 +18,6 @@ if (object instanceof Array) { | ||
var cls = object.constructor; | ||
return this.convert(cls, object, "serialization"); | ||
return this.convert(cls, object, "serialization", options); | ||
} | ||
}; | ||
Serializer.prototype.deserialize = function (cls, json) { | ||
Serializer.prototype.deserialize = function (cls, json, options) { | ||
var _this = this; | ||
@@ -27,3 +28,3 @@ if (json instanceof Array) { | ||
else { | ||
return this.convert(cls, json, "deserialization"); | ||
return this.convert(cls, json, "deserialization", options); | ||
} | ||
@@ -34,3 +35,3 @@ }; | ||
// ------------------------------------------------------------------------- | ||
Serializer.prototype.convert = function (cls, object, operationType) { | ||
Serializer.prototype.convert = function (cls, object, operationType, options) { | ||
var _this = this; | ||
@@ -40,10 +41,20 @@ if (object === null || object === undefined) | ||
var newObject = operationType === "serialization" ? {} : new cls(); | ||
Object.keys(object) | ||
.filter(function (key) { return !_this.isSkipped(cls, key, operationType); }) | ||
.forEach(function (key) { | ||
var type = _this.getType(cls, key); | ||
var _loop_1 = function(key) { | ||
if (this_1.isSkipped(cls, key, operationType)) | ||
return "break"; | ||
if (object[key] instanceof Function) | ||
return "break"; | ||
if (options && options.skipStartedWith && | ||
key.substr(0, options.skipStartedWith.length) === options.skipStartedWith) | ||
return "break"; | ||
var type = this_1.getType(cls, key); | ||
if (object[key] instanceof Array) { | ||
if (object[key].length > 0 && !type && operationType === "deserialization") | ||
throw new TypeMissingError_1.TypeMissingError(cls, key); | ||
newObject[key] = object[key].map(function (arrayItem) { return _this.convert(type, arrayItem, operationType); }); | ||
// if (object[key].length > 0 && !type && operationType === "deserialization") | ||
// throw new TypeMissingError(cls, key); | ||
if (object[key].length > 0 && type) { | ||
newObject[key] = object[key].map(function (arrayItem) { return _this.convert(type, arrayItem, operationType); }); | ||
} | ||
else { | ||
newObject[key] = object[key]; | ||
} | ||
} | ||
@@ -66,3 +77,3 @@ else if (object[key] instanceof Object || type) { | ||
else { | ||
newObject[key] = _this.convert(type, object[key], operationType); | ||
newObject[key] = this_1.convert(type, object[key], operationType); | ||
} | ||
@@ -73,3 +84,8 @@ } | ||
} | ||
}); | ||
}; | ||
var this_1 = this; | ||
for (var key in object) { | ||
var state_1 = _loop_1(key); | ||
if (state_1 === "break") break; | ||
} | ||
return newObject; | ||
@@ -90,3 +106,3 @@ }; | ||
return Serializer; | ||
})(); | ||
}()); | ||
exports.Serializer = Serializer; | ||
@@ -93,0 +109,0 @@ var serializer = new Serializer(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
412
33033