mirror of
https://github.com/flynx/serialize.js.git
synced 2026-08-26 09:46:41 +00:00
better testing + bugfixes...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
836489e68a
commit
2d9f117190
17
README.md
17
README.md
@ -1,2 +1,15 @@
|
|||||||
# serilize.js
|
# serilize.js: Extended JSON serilization
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-serialize",
|
"name": "ig-serialize",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"description": "experimental extended json serializaion...",
|
"description": "experimental extended json serializaion...",
|
||||||
"main": "serialize.js",
|
"main": "serialize.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
18
serialize.js
18
serialize.js
@ -117,7 +117,7 @@ function(obj, path=[], seen=new Map(), indent, depth=0, functions){
|
|||||||
seen.set(obj, path)
|
seen.set(obj, path)
|
||||||
// if functions array is given add function to it and store its
|
// if functions array is given add function to it and store its
|
||||||
// index in the serialized data...
|
// index in the serialized data...
|
||||||
if(functions != null){
|
if(functions instanceof Array){
|
||||||
functions.push(obj)
|
functions.push(obj)
|
||||||
obj = functions.length-1 }
|
obj = functions.length-1 }
|
||||||
var s = '('+ obj.toString() +')'
|
var s = '('+ obj.toString() +')'
|
||||||
@ -543,6 +543,7 @@ module.eJSON = {
|
|||||||
// NOTE: this uses eval(..) so care must be taken when enabling this...
|
// NOTE: this uses eval(..) so care must be taken when enabling this...
|
||||||
func: function(state, path, match, str, i, line){
|
func: function(state, path, match, str, i, line){
|
||||||
if(state.functions == null){
|
if(state.functions == null){
|
||||||
|
console.log('---', state)
|
||||||
this.error('Deserializing functions disabled.', str, i, line) }
|
this.error('Deserializing functions disabled.', str, i, line) }
|
||||||
|
|
||||||
debug.lex('function', str, i, line)
|
debug.lex('function', str, i, line)
|
||||||
@ -558,6 +559,9 @@ module.eJSON = {
|
|||||||
if(state.functions instanceof Array){
|
if(state.functions instanceof Array){
|
||||||
var [n, i, line] = this.number(state, path, str[i+1], str, i+1, line)
|
var [n, i, line] = this.number(state, path, str[i+1], str, i+1, line)
|
||||||
res = state.functions[n]
|
res = state.functions[n]
|
||||||
|
if(str[i] != ')'){
|
||||||
|
this.error('Expected ")" got "'+ str[i] +'"', str, i, line) }
|
||||||
|
i++
|
||||||
// func code...
|
// func code...
|
||||||
} else {
|
} else {
|
||||||
var code = str.slice(i, i+l)
|
var code = str.slice(i, i+l)
|
||||||
@ -626,15 +630,15 @@ function(str, functions){
|
|||||||
|
|
||||||
var deepCopy =
|
var deepCopy =
|
||||||
module.deepCopy =
|
module.deepCopy =
|
||||||
function(obj){
|
function(obj, functions){
|
||||||
return deserialize(
|
return deserialize(
|
||||||
serialize(obj)) }
|
serialize(obj, null, 0, functions),
|
||||||
|
functions) }
|
||||||
|
|
||||||
|
|
||||||
var semiDeepCopy =
|
var partialDeepCopy =
|
||||||
module.semiDeepCopy =
|
module.partialDeepCopy =
|
||||||
function(obj){
|
function(obj, funcs=[]){
|
||||||
var funcs = []
|
|
||||||
return deserialize(
|
return deserialize(
|
||||||
serialize(obj, null, 0, funcs),
|
serialize(obj, null, 0, funcs),
|
||||||
funcs) }
|
funcs) }
|
||||||
|
|||||||
35
test.js
35
test.js
@ -152,8 +152,43 @@ test.Tests({
|
|||||||
var obj = eJSON.deserialize(setup, true)
|
var obj = eJSON.deserialize(setup, true)
|
||||||
assert(
|
assert(
|
||||||
JSON.stringify(obj) == eJSON.serialize(obj) ) },
|
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...
|
||||||
|
},
|
||||||
|
})
|
||||||
|
//*/
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user