serialijse
Advanced tools
Comparing version 0.0.19 to 0.0.23
declare module "serialijse" { | ||
export function serialize<T>(object: T): string; | ||
export function deserialize<T>(serializationString: string): T; | ||
export function declarePersistable(constructor: any): void; | ||
export function declarePersistable(constructor: any, name?: string): void; | ||
export function serializeZ<T>(obj: T, callback: (err: string, buff: any) => void): void; | ||
export function deserializeZ<T>(buff: any, callback: (err: string, obj: T) => void): void; | ||
} |
@@ -6,18 +6,58 @@ (function (exports) { | ||
var isFunction = function(obj) { | ||
return typeof obj == 'function' || false; | ||
var isFunction = function (obj) { | ||
return typeof obj === 'function'; | ||
}; | ||
function declarePersistable(constructor) { | ||
function declarePersistable(constructor, name) { | ||
var className = constructor.prototype.constructor.name; | ||
if (name) { | ||
className = name; | ||
} | ||
if (g_global.hasOwnProperty(className)) { | ||
console.warn("warning: declarePersistable : class " + className + " already registered"); | ||
} | ||
//xx assert(!g_global.hasOwnProperty(className)); | ||
g_global[className] = constructor; | ||
} | ||
/** | ||
* returns true if the property is persistable. | ||
* @param obj | ||
* @param propertyName | ||
* @returns {boolean} | ||
*/ | ||
function isPropertyPersistable(obj, propertyName) { | ||
if (!obj.hasOwnProperty(propertyName)) { | ||
return false; | ||
} | ||
if (propertyName === '____index') { | ||
return false; | ||
} | ||
if (obj.constructor && obj.constructor.serialijseOptions) { | ||
// | ||
var options = obj.constructor.serialijseOptions; | ||
if (options.ignored) { | ||
options.ignored = (options.ignored instanceof Array) ? options.ignored : [options.ignored]; | ||
for (var i = 0; i < options.ignored.length; i++) { | ||
var o = options.ignored[i]; | ||
if (typeof o === "string") { | ||
if (o === propertyName) { | ||
return false; | ||
} | ||
} else if (o instanceof RegExp) { | ||
if (propertyName.match(o)) { | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
function serialize(object) { | ||
assert(object !== undefined,"serialize: expect a valid object to serialize "); | ||
assert(object !== undefined, "serialize: expect a valid object to serialize "); | ||
@@ -71,3 +111,3 @@ var index = []; | ||
} | ||
if (className !== "Object" && !g_global.hasOwnProperty(className) ) { | ||
if (className !== "Object" && !g_global.hasOwnProperty(className)) { | ||
throw new Error("class " + className + " is not registered in class Factory - deserialization will not be possible"); | ||
@@ -89,3 +129,4 @@ } | ||
for (v in object) { | ||
if (object.hasOwnProperty(v) && v !== '____index') { | ||
if (isPropertyPersistable(object, v)) { | ||
// if (object.hasOwnProperty(v) && v !== '____index') { | ||
if (object[v] !== null) { | ||
@@ -124,3 +165,2 @@ s.d[v] = _serialize(object[v]); | ||
} | ||
var obj = _serialize(object); | ||
@@ -142,8 +182,8 @@ // unset temporary ___index properties | ||
data = JSON.parse(serializationString); | ||
} else if (typeof serializationString ==='object') { | ||
} else if (typeof serializationString === 'object') { | ||
data = serializationString; | ||
} | ||
var index = data[0], | ||
obj = data[1], | ||
cache = []; | ||
obj = data[1], | ||
cache = []; | ||
@@ -157,5 +197,9 @@ function deserialize_node_or_value(node) { | ||
var postDeserialiseActions = []; | ||
function deserialize_node(node) { | ||
// special treatment | ||
if (!node) { return null;} | ||
if (!node) { | ||
return null; | ||
} | ||
@@ -173,2 +217,3 @@ if (node.hasOwnProperty("d")) { | ||
} | ||
// check if this object has already been de-serialized | ||
if (cache[object_id] !== undefined) { | ||
@@ -181,3 +226,3 @@ return cache[object_id]; | ||
assert(cache[object_id] === cache_object); | ||
return cache_object; | ||
return cache_object; | ||
@@ -206,4 +251,3 @@ } else if (node.hasOwnProperty("a")) { | ||
if (!constructor) { | ||
throw new Error(" Cannot find constructor to deserialize class of type" + className + ". use declarePersistable(Constructor)"); | ||
throw new Error(" Cannot find constructor to deserialize class of type " + className + ". use declarePersistable(Constructor)"); | ||
} | ||
@@ -218,11 +262,22 @@ assert(isFunction(constructor)); | ||
try { | ||
obj[v] = deserialize_node_or_value(data[v]); | ||
} | ||
catch(err) { | ||
console.log(" property : ", v); | ||
console.log(err); | ||
throw err; | ||
obj[v] = deserialize_node_or_value(data[v]); | ||
} | ||
catch (err) { | ||
console.log(" property : ", v); | ||
console.log(err); | ||
throw err; | ||
} | ||
} | ||
} | ||
if (constructor && constructor.serialijseOptions) { | ||
// onDeserialize is called immediately after object has been created | ||
if (constructor.serialijseOptions.onDeserialize) { | ||
constructor.serialijseOptions.onDeserialize(obj); | ||
} | ||
// onPostDeserialize call is postponed after the main object has been fully de-serializednpm | ||
if (constructor.serialijseOptions.onPostDeserialize) { | ||
postDeserialiseActions.push(obj); | ||
} | ||
} | ||
return obj; | ||
@@ -232,3 +287,9 @@ } | ||
return deserialize_node(obj); | ||
var deserializedObject = deserialize_node(obj); | ||
postDeserialiseActions.forEach(function(o){ | ||
o.constructor.serialijseOptions.onPostDeserialize(o); | ||
}); | ||
postDeserialiseActions = []; | ||
return deserializedObject; | ||
} | ||
@@ -265,3 +326,3 @@ | ||
})(typeof exports === 'undefined'? this['serialijse']={}: exports); | ||
})(typeof exports === 'undefined' ? this['serialijse'] = {} : exports); | ||
{ | ||
"name": "serialijse", | ||
"version": "0.0.19", | ||
"version": "0.0.23", | ||
"description": "serialize and deserialize your javascript objects, preserve your object model ", | ||
@@ -19,3 +19,11 @@ "main": "index.js", | ||
"chrome/latest", | ||
"firefox/latest" | ||
"firefox/latest", | ||
"ie/6..latest", | ||
"chrome/22..latest", | ||
"firefox/16..latest", | ||
"safari/latest", | ||
"opera/11.0..latest", | ||
"iphone/6", | ||
"ipad/6", | ||
"android-browser/latest" | ||
] | ||
@@ -36,10 +44,8 @@ }, | ||
"devDependencies": { | ||
"mocha": "latest", | ||
"should": "^11.1.1", | ||
"uglify-js": "^2.7.4" | ||
"mocha": "^3.4.2", | ||
"should": "^11.2.1", | ||
"uglify-js": "^3.0.19" | ||
}, | ||
"dependencies": { | ||
"underscore": "^1.8.3" | ||
}, | ||
"dependencies": {}, | ||
"types": "./lib/serialijse.d.ts" | ||
} |
/*global describe, it*/ | ||
var Should; | ||
if(typeof require !== "undefined") { | ||
if (typeof require !== "undefined") { | ||
Should = require("should"); | ||
@@ -8,2 +8,3 @@ | ||
} else { | ||
} | ||
@@ -20,5 +21,2 @@ Should(true).eql(true); | ||
"use strict"; | ||
@@ -34,4 +32,5 @@ | ||
this.color = new Color("blue"); | ||
this.created_on= new Date("04 May 1956 GMT"); | ||
this.created_on = new Date("04 May 1956 GMT"); | ||
} | ||
declarePersistable(Vehicule); | ||
@@ -43,7 +42,5 @@ declarePersistable(Color); | ||
it("should persist a simple javascript object (pojo)", function () { | ||
it("should persist a simple javascript object", function () { | ||
var vehicule = { name: "GM" }; | ||
var vehicule = {name: "GM"}; | ||
var serializationString = serialize(vehicule); | ||
@@ -57,3 +54,3 @@ //xx console.log(serializationString); | ||
it("should persist a simple object", function () { | ||
it("should persist a javascript class instance", function () { | ||
var vehicule = new Vehicule(); | ||
@@ -76,5 +73,5 @@ | ||
it("should persist a simple containing an array", function () { | ||
it("should persist a simple object containing an array", function () { | ||
var vehicule = new Vehicule(); | ||
vehicule.passengers =["Joe","Jack"]; | ||
vehicule.passengers = ["Joe", "Jack"]; | ||
var serializationString = serialize(vehicule); | ||
@@ -88,5 +85,5 @@ //xx console.log(serializationString); | ||
it("should persist a array of persistable object", function () { | ||
it("should persist a array of persistable objects", function () { | ||
var vehicules = [ new Vehicule(), new Vehicule() ]; | ||
var vehicules = [new Vehicule(), new Vehicule()]; | ||
@@ -103,3 +100,3 @@ vehicules[0].brand = "Renault"; | ||
it("should persist a array containing two elements pointing to the same object ", function () { | ||
it("should persist a array containing two elements pointing to the same object ", function () { | ||
@@ -111,3 +108,3 @@ var the_vehicule = new Vehicule(); | ||
var vehicules = [ the_vehicule,the_vehicule ]; | ||
var vehicules = [the_vehicule, the_vehicule]; | ||
@@ -120,6 +117,6 @@ vehicules[0].should.equal(vehicules[1]); | ||
var expected = '[['+ | ||
'{"c":"Vehicule","d":{"brand":"Citroen","price":95000,"color":{"o":1},"created_on":{"d":-651981600000}}},' + | ||
'{"c":"Color","d":{"name":"blue"}}' + | ||
'],'+'{"a":[{"o":0},{"o":0}]}]'; | ||
var expected = '[[' + | ||
'{"c":"Vehicule","d":{"brand":"Citroen","price":95000,"color":{"o":1},"created_on":{"d":-651981600000}}},' + | ||
'{"c":"Color","d":{"name":"blue"}}' + | ||
'],' + '{"a":[{"o":0},{"o":0}]}]'; | ||
@@ -138,3 +135,3 @@ serializationString.should.eql(expected); | ||
it("should not persist property defined in class prototype",function () { | ||
it("should not persist property defined in class prototype", function () { | ||
@@ -144,17 +141,17 @@ function Rectangle() { | ||
this.height = 20; | ||
Object.defineProperty(this,"area",{ | ||
get: function() { | ||
return this.width * this.height; | ||
} | ||
Object.defineProperty(this, "area", { | ||
get: function () { | ||
return this.width * this.height; | ||
} | ||
}); | ||
} | ||
Rectangle.prototype.__defineGetter__("perimeter",function(){ | ||
return (this.width + this.height)*2.0; | ||
Rectangle.prototype.__defineGetter__("perimeter", function () { | ||
return (this.width + this.height) * 2.0; | ||
}); | ||
declarePersistable(Rectangle); | ||
var rect1 = new Rectangle(); | ||
rect1.width = 100; | ||
rect1.height= 2; | ||
rect1.height = 2; | ||
rect1.area.should.equal(200); | ||
@@ -172,14 +169,14 @@ rect1.perimeter.should.equal(204); | ||
it("testing compression impact ",function(done){ | ||
it("testing compression impact ", function (done) { | ||
var vehicules = [new Vehicule(),new Vehicule(),new Vehicule()]; | ||
var vehicules = [new Vehicule(), new Vehicule(), new Vehicule()]; | ||
var uncompressed_serializationString = serialize(vehicules); | ||
serializeZ(vehicules,function(err,buffer){ | ||
serializeZ(vehicules, function (err, buffer) { | ||
var serializationString = buffer.toString("base64"); | ||
var compression_ratio = Math.round(100.0- 100.0*(buffer.length/uncompressed_serializationString.length)); | ||
console.log(" = ", uncompressed_serializationString.length, "compressed =",buffer.length," ratio ", compression_ratio,"%"); | ||
deserializeZ(buffer,function(err,reconstructedObject){ | ||
var compression_ratio = Math.round(100.0 - 100.0 * (buffer.length / uncompressed_serializationString.length)); | ||
console.log(" = ", uncompressed_serializationString.length, "compressed =", buffer.length, " ratio ", compression_ratio, "%"); | ||
deserializeZ(buffer, function (err, reconstructedObject) { | ||
done(err); | ||
@@ -192,3 +189,3 @@ reconstructedObject.should.eql(vehicules); | ||
it("should persist object with boolean", function(done) { | ||
it("should persist object with boolean", function (done) { | ||
@@ -203,9 +200,10 @@ var vehicule = new Vehicule(); | ||
vehicule.requireServicing.should.equal(true); | ||
done(); | ||
}); | ||
it("should persist an object with a undefined property",function(done){ | ||
it("should persist an object with an undefined property", function (done) { | ||
var vehicule = new Vehicule(); | ||
vehicule.serviceDate = [ null, new Date("2013/01/02")]; | ||
vehicule.serviceDate = [null, new Date("2013/01/02")]; | ||
@@ -216,3 +214,3 @@ // try to mess with the serialisation algo by adding a fake null property | ||
// delete it as it should not interfer | ||
// delete it as it should not interfere | ||
delete vehicule.toto; | ||
@@ -226,3 +224,3 @@ | ||
it("should deserialize from an already parsed JSON string",function(done) { | ||
it("should deserialize an already parsed JSON string", function (done) { | ||
@@ -238,7 +236,7 @@ var vehicule = new Vehicule(); | ||
done(); | ||
done(); | ||
}); | ||
it("unlike JSON.stringify/parse, it should serialize a standard object and preserve date",function(){ | ||
it("unlike JSON.stringify/parse, it should serialize a standard object and preserve date", function () { | ||
@@ -269,3 +267,3 @@ | ||
function Person(name) { | ||
this.name= name; | ||
this.name = name; | ||
this.parent = null; | ||
@@ -277,3 +275,3 @@ this.children = []; | ||
Person.prototype.addChild = function(name) { | ||
Person.prototype.addChild = function (name) { | ||
var child = new Person(name); | ||
@@ -285,5 +283,5 @@ child.parent = this; | ||
var mark = new Person("mark"), | ||
valery = mark.addChild("valery"), | ||
edgar = mark.addChild("edgar"); | ||
var mark = new Person("mark"), | ||
valery = mark.addChild("valery"), | ||
edgar = mark.addChild("edgar"); | ||
@@ -293,3 +291,3 @@ valery.parent.should.equal(mark); | ||
Should(function(){ | ||
Should(function () { | ||
JSON.stringify(mark); | ||
@@ -314,4 +312,64 @@ }).throwError(); // Circular | ||
function MyClassWithUnpersistableMembers() { | ||
this.name = "unset"; | ||
this._cache = []; | ||
this.$someOtherStuff = 0; | ||
} | ||
MyClassWithUnpersistableMembers.serialijseOptions = { | ||
ignored: [ | ||
"_cache", | ||
/$.*/ | ||
] | ||
}; | ||
declarePersistable(MyClassWithUnpersistableMembers); | ||
it("should not persist un-persistable members", function () { | ||
var obj = new MyClassWithUnpersistableMembers(); | ||
obj._cache = [1, 2, 3]; | ||
obj.$someOtherStuff = 35; | ||
obj.name = "Hello"; | ||
var serializationString = serialize(obj); | ||
var json_obj = JSON.parse(serializationString); | ||
var reconstructedObject = deserialize(json_obj); | ||
Should.exist(reconstructedObject.name); | ||
Should.exist(reconstructedObject._cache); | ||
Should.exist(reconstructedObject.$someOtherStuff); | ||
reconstructedObject._cache.should.eql([]); | ||
reconstructedObject.$someOtherStuff.should.eql(0); | ||
}); | ||
function MyClassWithPostDeserializeAction() { | ||
this.name = "unset"; | ||
this._cache = []; | ||
} | ||
MyClassWithPostDeserializeAction.prototype.reconstructCache = function(){ | ||
this._cache.push("reconstructCache has been called"); | ||
}; | ||
MyClassWithPostDeserializeAction.serialijseOptions = { | ||
ignored: [ | ||
"_cache" | ||
], | ||
onPostDeserialize: function(o) { o.reconstructCache(); } | ||
}; | ||
declarePersistable(MyClassWithPostDeserializeAction); | ||
it("should run onPostDeserialize during deserialization", function () { | ||
var obj = new MyClassWithPostDeserializeAction(); | ||
obj._cache.push(36); | ||
var serializationString = serialize(obj); | ||
var reconstructedObject = deserialize(serializationString); | ||
reconstructedObject._cache.should.eql(["reconstructCache has been called"]); | ||
}); | ||
}); | ||
}()); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
1355885
0
22
35009
31
- Removedunderscore@^1.8.3
- Removedunderscore@1.13.7(transitive)