mirror of
https://github.com/flynx/serialize.js.git
synced 2026-08-25 17:26:42 +00:00
updated format -- now more general...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
c980745ae1
commit
5f47b03648
@ -65,6 +65,8 @@ Extensions to JSON:
|
|||||||
|
|
||||||
### Recursion
|
### Recursion
|
||||||
|
|
||||||
|
If an object is encountered
|
||||||
|
|
||||||
### null types
|
### null types
|
||||||
|
|
||||||
### BigInt
|
### BigInt
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-serialize",
|
"name": "ig-serialize",
|
||||||
"version": "1.0.6",
|
"version": "1.1.0",
|
||||||
"description": "experimental extended json serializaion...",
|
"description": "experimental extended json serializaion...",
|
||||||
"main": "serialize.js",
|
"main": "serialize.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
26
serialize.js
26
serialize.js
@ -51,9 +51,9 @@ var NAN = 'NaN'
|
|||||||
var INFINITY = 'Infinity'
|
var INFINITY = 'Infinity'
|
||||||
var NEG_INFINITY = '-Infinity'
|
var NEG_INFINITY = '-Infinity'
|
||||||
|
|
||||||
var RECURSIVE = '<RECURSIVE%>'
|
var REFERENCE = '<REF%>'
|
||||||
|
|
||||||
var FUNCTION = '<FUNCTION[%]>'
|
var FUNCTION = '<FUNC[%]>'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ var debug = {
|
|||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
module.STRING_LENGTH_REF = RECURSIVE.length * 8
|
module.STRING_LENGTH_REF = REFERENCE.length * 8
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -148,11 +148,11 @@ function(obj, path=[], seen=new Map(), indent, depth=0, options={}){
|
|||||||
options.string_length_ref
|
options.string_length_ref
|
||||||
?? module.STRING_LENGTH_REF
|
?? module.STRING_LENGTH_REF
|
||||||
|
|
||||||
// recursive...
|
// reference...
|
||||||
var p = seen.get(obj)
|
var p = seen.get(obj)
|
||||||
if(p != null){
|
if(p != null){
|
||||||
// NOTE: _serialize(..) is always printed flat here, regardless of indent/depth...
|
// NOTE: _serialize(..) is always printed flat here, regardless of indent/depth...
|
||||||
return RECURSIVE.replace('%', _serialize(p)) }
|
return REFERENCE.replace('%', _serialize(p)) }
|
||||||
|
|
||||||
// functions...
|
// functions...
|
||||||
// NOTE: we are storing function length to avoid parsing the function...
|
// NOTE: we are storing function length to avoid parsing the function...
|
||||||
@ -260,7 +260,7 @@ function(obj, indent, depth=0, options){
|
|||||||
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||||
|
|
||||||
// XXX better error handling...
|
// XXX better error handling...
|
||||||
// XXX try and make this single stage (see notes for : .recursive(..))
|
// XXX try and make this single stage (see notes for : .reference(..))
|
||||||
var eJSON =
|
var eJSON =
|
||||||
module.eJSON = {
|
module.eJSON = {
|
||||||
chars: {
|
chars: {
|
||||||
@ -287,9 +287,9 @@ module.eJSON = {
|
|||||||
NaN: NaN,
|
NaN: NaN,
|
||||||
|
|
||||||
'<empty>': 'empty',
|
'<empty>': 'empty',
|
||||||
'<RECURSIVE': 'recursive',
|
'<REF': 'reference',
|
||||||
|
|
||||||
'<FUNCTION[': 'func',
|
'<FUNC[': 'func',
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
@ -614,15 +614,15 @@ module.eJSON = {
|
|||||||
// directly reference that -- this would eliminate the need for
|
// directly reference that -- this would eliminate the need for
|
||||||
// stage two... (XXX TEST)
|
// stage two... (XXX TEST)
|
||||||
// ...need to use serialized paths as keys...
|
// ...need to use serialized paths as keys...
|
||||||
recursive: function(state, path, match, str, i, line){
|
reference: function(state, path, match, str, i, line){
|
||||||
debug.lex('recursive', str, i, line)
|
debug.lex('reference', str, i, line)
|
||||||
return this.sequence(
|
return this.sequence(
|
||||||
state, path, str, i+match.length, line,
|
state, path, str, i+match.length, line,
|
||||||
'>',
|
'>',
|
||||||
function(res, index, str, i, line){
|
function(res, index, str, i, line){
|
||||||
var obj
|
var obj
|
||||||
;[obj, i, line] = this.array(state, [...path, index], '[', str, i, line)
|
;[obj, i, line] = this.array(state, [...path, index], '[', str, i, line)
|
||||||
var rec = state.recursive ??= []
|
var rec = state.reference ??= []
|
||||||
rec.push([path, obj])
|
rec.push([path, obj])
|
||||||
return [{}, i, line] }) },
|
return [{}, i, line] }) },
|
||||||
|
|
||||||
@ -695,8 +695,8 @@ module.eJSON = {
|
|||||||
var state = {functions: options.functions}
|
var state = {functions: options.functions}
|
||||||
var res = this.value(state, [], str)[0]
|
var res = this.value(state, [], str)[0]
|
||||||
|
|
||||||
// stage 2: link the recursive structures...
|
// stage 2: link the reference structures...
|
||||||
for(var [a, b] of state.recursive ?? []){
|
for(var [a, b] of state.reference ?? []){
|
||||||
this.setItem(res, a, this.getItem(res, b)) }
|
this.setItem(res, a, this.getItem(res, b)) }
|
||||||
|
|
||||||
return res },
|
return res },
|
||||||
|
|||||||
12
test.js
12
test.js
@ -100,19 +100,19 @@ var setups = test.Setups({
|
|||||||
return ['Map([])'] },
|
return ['Map([])'] },
|
||||||
|
|
||||||
'function': function(assert){
|
'function': function(assert){
|
||||||
return ['<FUNCTION[14,(function(){})]>'] },
|
return ['<FUNC[14,(function(){})]>'] },
|
||||||
|
|
||||||
// recursive...
|
// recursive...
|
||||||
'array-recursive': function(assert){
|
'array-recursive': function(assert){
|
||||||
return ['[<RECURSIVE[]>]'] },
|
return ['[<REF[]>]'] },
|
||||||
'object-recursive': function(assert){
|
'object-recursive': function(assert){
|
||||||
return ['{"r":<RECURSIVE[]>}'] },
|
return ['{"r":<REF[]>}'] },
|
||||||
'set-recursive': function(assert){
|
'set-recursive': function(assert){
|
||||||
return ['Set([<RECURSIVE[]>])'] },
|
return ['Set([<REF[]>])'] },
|
||||||
'map-recursive-key': function(assert){
|
'map-recursive-key': function(assert){
|
||||||
return ['Map([[<RECURSIVE[]>,"value"]])'] },
|
return ['Map([[<REF[]>,"value"]])'] },
|
||||||
'map-recursive-value': function(assert){
|
'map-recursive-value': function(assert){
|
||||||
return ['Map([["key",<RECURSIVE[]>]])'] },
|
return ['Map([["key",<REF[]>]])'] },
|
||||||
})
|
})
|
||||||
|
|
||||||
test.Modifiers({
|
test.Modifiers({
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user