serialijse
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -12,5 +12,5 @@ /*global exports*/ | ||
if (g_global.hasOwnProperty(className)) { | ||
throw new Error(" class " + className + " already registered"); | ||
console.warn("warning: declarePersistable : class " + className + " already registered"); | ||
} | ||
assert(!g_global.hasOwnProperty(className)); | ||
//xx assert(!g_global.hasOwnProperty(className)); | ||
g_global[className] = constructor; | ||
@@ -56,7 +56,3 @@ } | ||
// @ => already serialized object | ||
if (className === "Object") { | ||
// default JSON object | ||
serializingObject["j"] = object; | ||
return; | ||
} | ||
if (className === "Array") { | ||
@@ -67,7 +63,10 @@ serializingObject["a"] = object.map(_serialize); | ||
if (className === "Date") { | ||
serializingObject["d"] = object.toUTCString(); | ||
serializingObject["d"] = object.getTime(); | ||
return; | ||
} | ||
if (!g_global.hasOwnProperty(className)) { | ||
if (className === "Object") { | ||
// // default JSON object | ||
// serializingObject["j"] = object; | ||
// return; | ||
} else if (!g_global.hasOwnProperty(className)) { | ||
throw new Error("class " + className + " is not registered in class Factory - deserialization will not be possible"); | ||
@@ -192,11 +191,14 @@ } | ||
constructor = g_global[className]; | ||
if (!constructor) { | ||
throw new Error(" Cannot find constructor to deserialize class of type" + className + ". use declarePersistable(Constructor)"); | ||
if (className === "Object") { | ||
obj = {}; | ||
} else { | ||
constructor = g_global[className]; | ||
if (!constructor) { | ||
throw new Error(" Cannot find constructor to deserialize class of type" + className + ". use declarePersistable(Constructor)"); | ||
} | ||
assert(_.isFunction(constructor)); | ||
obj = new constructor(); | ||
} | ||
assert(_.isFunction(constructor)); | ||
obj = new constructor(); | ||
cache[object_id] = obj; | ||
@@ -203,0 +205,0 @@ for (v in data) { |
{ | ||
"name": "serialijse", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "serialize and deserialize your javascript objects, preserve your object model ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -99,3 +99,3 @@ /*global describe, it*/ | ||
var expected = '[['+ | ||
'{"c":"Vehicule","d":{"brand":"Citroen","price":95000,"color":{"o":1},"created_on":{"d":"Wed, 04 May 1949 22:00:00 GMT"}}},' + | ||
'{"c":"Vehicule","d":{"brand":"Citroen","price":95000,"color":{"o":1},"created_on":{"d":-651981600000}}},' + | ||
'{"c":"Color","d":{"name":"blue"}}' + | ||
@@ -116,49 +116,2 @@ '],'+'{"a":[{"o":0},{"o":0}]}]'; | ||
it("should persist a shared object only once", function () { | ||
function Person(name) { | ||
this.name= name; | ||
this.parent = null; | ||
this.children = []; | ||
} | ||
declarePersistable(Person); | ||
Person.prototype.addChild = function(name) { | ||
var child = new Person(name); | ||
child.parent = this; | ||
this.children.push(child); | ||
return child; | ||
}; | ||
var mark = new Person("mark"), | ||
valery = mark.addChild("valery"), | ||
edgar = mark.addChild("edgar"); | ||
valery.parent.should.equal(mark); | ||
edgar.parent.should.equal(mark); | ||
should(function(){ | ||
JSON.stringify(mark); | ||
}).throwError(); // Circular | ||
var serializationString = serialize(mark); | ||
//xx console.log(serializationString); | ||
var mark_reloaded = deserialize(serializationString); | ||
mark_reloaded.name.should.eql("mark"); | ||
mark_reloaded.children.length.should.eql(2); | ||
mark_reloaded.should.be.instanceOf(Person); | ||
mark_reloaded.children[0].should.be.instanceOf(Person); | ||
mark_reloaded.children[1].should.be.instanceOf(Person); | ||
mark_reloaded.children[0].parent.should.equal(mark_reloaded); | ||
mark_reloaded.children[1].parent.should.equal(mark_reloaded); | ||
mark_reloaded.children[0].name.should.eql("valery"); | ||
mark_reloaded.children[1].name.should.eql("edgar"); | ||
}); | ||
it("should not persist property defined in class prototype",function () { | ||
@@ -231,3 +184,3 @@ | ||
it("should persist an object with a undefined property",function(done){ | ||
it("should persist an object with a undefined property",function(done){ | ||
@@ -266,4 +219,74 @@ var vehicule = new Vehicule(); | ||
it("unlike JSON.stringify/parse, it should serialize a standard object and preserve date",function(){ | ||
var some_object = { | ||
the_date: new Date() | ||
}; | ||
some_object.the_date.should.be.instanceOf(Date); | ||
var serializationString = serialize(some_object); | ||
console.log("serializationString= ",serializationString); | ||
var reconstructedObject = deserialize(serializationString); | ||
console.log(reconstructedObject); | ||
reconstructedObject.the_date.should.be.instanceOf(Date); | ||
reconstructedObject.the_date.should.eql(some_object.the_date); | ||
reconstructedObject.should.eql(some_object); | ||
reconstructedObject.should.eql(some_object); | ||
}); | ||
it("unlike JSON.stringify/parse, it should persist object that contains cyclic object value", function () { | ||
function Person(name) { | ||
this.name= name; | ||
this.parent = null; | ||
this.children = []; | ||
} | ||
declarePersistable(Person); | ||
Person.prototype.addChild = function(name) { | ||
var child = new Person(name); | ||
child.parent = this; | ||
this.children.push(child); | ||
return child; | ||
}; | ||
var mark = new Person("mark"), | ||
valery = mark.addChild("valery"), | ||
edgar = mark.addChild("edgar"); | ||
valery.parent.should.equal(mark); | ||
edgar.parent.should.equal(mark); | ||
should(function(){ | ||
JSON.stringify(mark); | ||
}).throwError(); // Circular | ||
var serializationString = serialize(mark); | ||
//xx console.log(serializationString); | ||
var mark_reloaded = deserialize(serializationString); | ||
mark_reloaded.name.should.eql("mark"); | ||
mark_reloaded.children.length.should.eql(2); | ||
mark_reloaded.should.be.instanceOf(Person); | ||
mark_reloaded.children[0].should.be.instanceOf(Person); | ||
mark_reloaded.children[1].should.be.instanceOf(Person); | ||
mark_reloaded.children[0].parent.should.equal(mark_reloaded); | ||
mark_reloaded.children[1].parent.should.equal(mark_reloaded); | ||
mark_reloaded.children[0].name.should.eql("valery"); | ||
mark_reloaded.children[1].name.should.eql("edgar"); | ||
}); | ||
}); | ||
}()); |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20660
393
0