From 2d9f117190754ae936060f2e7edccd8b2ba33da9 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 1 Jan 2026 12:57:24 +0300 Subject: [PATCH] better testing + bugfixes... Signed-off-by: Alex A. Naanou --- README.md | 17 +++++++++++++++-- package.json | 2 +- serialize.js | 18 +++++++++++------- test.js | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 478cd60..3ba6392 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ -# serilize.js -Extended JSON serilization +# serilize.js: Extended JSON serilization + +This extends the default JSON serialization adding the following: +- Recursive data structure serialization +- `undefined`/`NaN` serialization +- `Set`/`Map` serialization +- Function serialization (off by default) +- Deep and partial-deep cleen object copy + + + +## Motivation + + + diff --git a/package.json b/package.json index 2b379a3..83550ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-serialize", - "version": "1.0.0", + "version": "1.0.1", "description": "experimental extended json serializaion...", "main": "serialize.js", "scripts": { diff --git a/serialize.js b/serialize.js index e1f3a49..63faf14 100644 --- a/serialize.js +++ b/serialize.js @@ -117,7 +117,7 @@ function(obj, path=[], seen=new Map(), indent, depth=0, functions){ seen.set(obj, path) // if functions array is given add function to it and store its // index in the serialized data... - if(functions != null){ + if(functions instanceof Array){ functions.push(obj) obj = functions.length-1 } var s = '('+ obj.toString() +')' @@ -543,6 +543,7 @@ module.eJSON = { // NOTE: this uses eval(..) so care must be taken when enabling this... func: function(state, path, match, str, i, line){ if(state.functions == null){ + console.log('---', state) this.error('Deserializing functions disabled.', str, i, line) } debug.lex('function', str, i, line) @@ -558,6 +559,9 @@ module.eJSON = { if(state.functions instanceof Array){ var [n, i, line] = this.number(state, path, str[i+1], str, i+1, line) res = state.functions[n] + if(str[i] != ')'){ + this.error('Expected ")" got "'+ str[i] +'"', str, i, line) } + i++ // func code... } else { var code = str.slice(i, i+l) @@ -626,15 +630,15 @@ function(str, functions){ var deepCopy = module.deepCopy = -function(obj){ +function(obj, functions){ return deserialize( - serialize(obj)) } + serialize(obj, null, 0, functions), + functions) } -var semiDeepCopy = -module.semiDeepCopy = -function(obj){ - var funcs = [] +var partialDeepCopy = +module.partialDeepCopy = +function(obj, funcs=[]){ return deserialize( serialize(obj, null, 0, funcs), funcs) } diff --git a/test.js b/test.js index 76c828d..2e37e5a 100755 --- a/test.js +++ b/test.js @@ -152,8 +152,43 @@ test.Tests({ var obj = eJSON.deserialize(setup, true) assert( JSON.stringify(obj) == eJSON.serialize(obj) ) }, + + 'deep-copy': function(assert, [setup]){ + var obj = eJSON.deserialize(setup, true) + var copy = eJSON.deepCopy(obj, true) + + assert(eJSON.serialize(obj) == eJSON.serialize(copy)) + + // XXX check if all non-atoms are distinct... + // XXX + }, + + //* XXX ERR + 'partial-deep-copy': function(assert, [setup]){ + var obj = eJSON.deserialize(setup, true) + var funcs = [] + var copy = eJSON.partialDeepCopy(obj, funcs) + + assert(eJSON.serialize(obj) == eJSON.serialize(copy)) + + // XXX check if all non-atoms are distinct and functions are the same... + // XXX + }, + //*/ }) +/* XXX these break things... +test.Cases({ + 'deep-copy-function': function(assert, [setup]){ + // XXX check function isolation... + }, + + 'partial-deep-copy-function': function(assert, [setup]){ + // XXX check function isolation... + }, +}) +//*/ + //---------------------------------------------------------------------