updated format -- now more general...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2026-01-13 03:20:11 +03:00
parent c980745ae1
commit 5f47b03648
4 changed files with 22 additions and 20 deletions

View File

@ -65,6 +65,8 @@ Extensions to JSON:
### Recursion
If an object is encountered
### null types
### BigInt

View File

@ -1,6 +1,6 @@
{
"name": "ig-serialize",
"version": "1.0.6",
"version": "1.1.0",
"description": "experimental extended json serializaion...",
"main": "serialize.js",
"scripts": {

View File

@ -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
View File

@ -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({