mirror of
https://github.com/flynx/serialize.js.git
synced 2026-07-10 03:00:57 +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
|
||||
|
||||
If an object is encountered
|
||||
|
||||
### null types
|
||||
|
||||
### BigInt
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "ig-serialize",
|
||||
"version": "1.0.6",
|
||||
"version": "1.1.0",
|
||||
"description": "experimental extended json serializaion...",
|
||||
"main": "serialize.js",
|
||||
"scripts": {
|
||||
|
||||
26
serialize.js
26
serialize.js
@ -51,9 +51,9 @@ var NAN = 'NaN'
|
||||
var 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
|
||||
?? module.STRING_LENGTH_REF
|
||||
|
||||
// recursive...
|
||||
// reference...
|
||||
var p = seen.get(obj)
|
||||
if(p != null){
|
||||
// NOTE: _serialize(..) is always printed flat here, regardless of indent/depth...
|
||||
return RECURSIVE.replace('%', _serialize(p)) }
|
||||
return REFERENCE.replace('%', _serialize(p)) }
|
||||
|
||||
// functions...
|
||||
// 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 try and make this single stage (see notes for : .recursive(..))
|
||||
// XXX try and make this single stage (see notes for : .reference(..))
|
||||
var eJSON =
|
||||
module.eJSON = {
|
||||
chars: {
|
||||
@ -287,9 +287,9 @@ module.eJSON = {
|
||||
NaN: NaN,
|
||||
|
||||
'<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
|
||||
// stage two... (XXX TEST)
|
||||
// ...need to use serialized paths as keys...
|
||||
recursive: function(state, path, match, str, i, line){
|
||||
debug.lex('recursive', str, i, line)
|
||||
reference: function(state, path, match, str, i, line){
|
||||
debug.lex('reference', str, i, line)
|
||||
return this.sequence(
|
||||
state, path, str, i+match.length, line,
|
||||
'>',
|
||||
function(res, index, str, i, line){
|
||||
var obj
|
||||
;[obj, i, line] = this.array(state, [...path, index], '[', str, i, line)
|
||||
var rec = state.recursive ??= []
|
||||
var rec = state.reference ??= []
|
||||
rec.push([path, obj])
|
||||
return [{}, i, line] }) },
|
||||
|
||||
@ -695,8 +695,8 @@ module.eJSON = {
|
||||
var state = {functions: options.functions}
|
||||
var res = this.value(state, [], str)[0]
|
||||
|
||||
// stage 2: link the recursive structures...
|
||||
for(var [a, b] of state.recursive ?? []){
|
||||
// stage 2: link the reference structures...
|
||||
for(var [a, b] of state.reference ?? []){
|
||||
this.setItem(res, a, this.getItem(res, b)) }
|
||||
|
||||
return res },
|
||||
|
||||
12
test.js
12
test.js
@ -100,19 +100,19 @@ var setups = test.Setups({
|
||||
return ['Map([])'] },
|
||||
|
||||
'function': function(assert){
|
||||
return ['<FUNCTION[14,(function(){})]>'] },
|
||||
return ['<FUNC[14,(function(){})]>'] },
|
||||
|
||||
// recursive...
|
||||
'array-recursive': function(assert){
|
||||
return ['[<RECURSIVE[]>]'] },
|
||||
return ['[<REF[]>]'] },
|
||||
'object-recursive': function(assert){
|
||||
return ['{"r":<RECURSIVE[]>}'] },
|
||||
return ['{"r":<REF[]>}'] },
|
||||
'set-recursive': function(assert){
|
||||
return ['Set([<RECURSIVE[]>])'] },
|
||||
return ['Set([<REF[]>])'] },
|
||||
'map-recursive-key': function(assert){
|
||||
return ['Map([[<RECURSIVE[]>,"value"]])'] },
|
||||
return ['Map([[<REF[]>,"value"]])'] },
|
||||
'map-recursive-value': function(assert){
|
||||
return ['Map([["key",<RECURSIVE[]>]])'] },
|
||||
return ['Map([["key",<REF[]>]])'] },
|
||||
})
|
||||
|
||||
test.Modifiers({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user