mirror of
https://github.com/flynx/serialize.js.git
synced 2026-07-10 11:10:57 +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
|
||||
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
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-serialize",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1",
|
||||
"description": "experimental extended json serializaion...",
|
||||
"main": "serialize.js",
|
||||
"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)
|
||||
// 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) }
|
||||
|
||||
35
test.js
35
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...
|
||||
},
|
||||
})
|
||||
//*/
|
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user