|
|
@ -7,6 +7,22 @@ |
|
|
|
var dateToJSON = function () { return { $$date: this.getTime() }; } |
|
|
|
var dateToJSON = function () { return { $$date: this.getTime() }; } |
|
|
|
, originalDateToJSON = Date.prototype.toJSON |
|
|
|
, originalDateToJSON = Date.prototype.toJSON |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Check a key, throw an error if the key is non valid |
|
|
|
|
|
|
|
* @param {String} k key |
|
|
|
|
|
|
|
* @param {Model} v value, needed to treat the Date edge case |
|
|
|
|
|
|
|
* Non-treatable edge case here: if part of the object if of the form { $$date: number } |
|
|
|
|
|
|
|
* Its serialized-then-deserialized version it will transformed into a Date object |
|
|
|
|
|
|
|
* But you really need to want it to trigger such behaviour, even when warned not to use '$' at the beginning of the field names... |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
function checkKey (k, v) { |
|
|
|
|
|
|
|
if (k[0] === '$' && !(k === '$$date' && typeof v === 'number')) { |
|
|
|
|
|
|
|
throw 'Keys cannot begin with the $ character'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Serialize an object to be persisted to a one-line string |
|
|
|
* Serialize an object to be persisted to a one-line string |
|
|
|
* Accepted primitive types: Number, String, Boolean, Date, null |
|
|
|
* Accepted primitive types: Number, String, Boolean, Date, null |
|
|
@ -19,10 +35,7 @@ function serialize (obj) { |
|
|
|
Date.prototype.toJSON = dateToJSON; |
|
|
|
Date.prototype.toJSON = dateToJSON; |
|
|
|
|
|
|
|
|
|
|
|
res = JSON.stringify(obj, function (k, v) { |
|
|
|
res = JSON.stringify(obj, function (k, v) { |
|
|
|
// Non-treatable edge case here: if part of the object if of the form { $$date: number }
|
|
|
|
checkKey(k, v); |
|
|
|
// Its serialized-then-deserialized version it will transformed into a Date object
|
|
|
|
|
|
|
|
// But you really need to want it to trigger such behaviour, even when warned not to use '$' at the beginning of the field names...
|
|
|
|
|
|
|
|
if (k[0] === '$' && !(k === '$$date' && typeof v === 'number')) { throw 'Keys cannot begin with the $ character'; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || v === null) { return v; } |
|
|
|
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean' || v === null) { return v; } |
|
|
|
//if (v && v.constructor && v.constructor.name === 'Date') { console.log("==============="); return { $$date: v.toString() }; }
|
|
|
|
//if (v && v.constructor && v.constructor.name === 'Date') { console.log("==============="); return { $$date: v.toString() }; }
|
|
|
@ -75,6 +88,7 @@ function deepCopy (obj) { |
|
|
|
if (typeof obj === 'object') { |
|
|
|
if (typeof obj === 'object') { |
|
|
|
res = {}; |
|
|
|
res = {}; |
|
|
|
Object.keys(obj).forEach(function (k) { |
|
|
|
Object.keys(obj).forEach(function (k) { |
|
|
|
|
|
|
|
checkKey(k, obj[k]); |
|
|
|
res[k] = deepCopy(obj[k]); |
|
|
|
res[k] = deepCopy(obj[k]); |
|
|
|
}); |
|
|
|
}); |
|
|
|
return res; |
|
|
|
return res; |
|
|
|