@@ -1296,8 +1296,16 @@ function json_encode(_map, _prettify) {
1296
1296
// ########### JSON_STRINGIFY & JSON_PARSE ###########
1297
1297
1298
1298
// This is an any to JSON helper function (to be used before JSON.stringify)
1299
- function _json_replacer ( value )
1299
+ var g_JSON_gml_infunc = undefined ;
1300
+ function _json_replacer ( key , value )
1300
1301
{
1302
+ // call the user method
1303
+ if ( ( g_JSON_gml_infunc != undefined ) && is_callable ( g_JSON_gml_infunc ) ) {
1304
+ var _func = getFunction ( g_JSON_gml_infunc , 1 ) ;
1305
+ _obj = "boundObject" in _func ? _func . boundObject : { } ;
1306
+ value = _func ( _obj , _obj , key , value ) ;
1307
+ } // end if
1308
+
1301
1309
if ( value == undefined ) return null ;
1302
1310
1303
1311
switch ( typeof value ) {
@@ -1333,8 +1341,8 @@ function _json_replacer(value)
1333
1341
g_ENCODE_VISITED_LIST . set ( value , 1 ) ;
1334
1342
1335
1343
var ret = [ ] ;
1336
- value . forEach ( item => {
1337
- ret . push ( _json_replacer ( item ) ) ;
1344
+ value . forEach ( ( item , index ) => {
1345
+ ret . push ( _json_replacer ( index . toString ( ) , item ) ) ;
1338
1346
} ) ;
1339
1347
1340
1348
// Remove the flag
@@ -1371,7 +1379,7 @@ function _json_replacer(value)
1371
1379
1372
1380
if ( ( entry == undefined ) || ( entry [ 0 ] | entry [ 1 ] ) ) {
1373
1381
Object . defineProperty ( ret , name , {
1374
- value : _json_replacer ( value [ oName ] ) ,
1382
+ value : _json_replacer ( name , value [ oName ] ) ,
1375
1383
configurable : true ,
1376
1384
writable : true ,
1377
1385
enumerable : true
@@ -1392,7 +1400,7 @@ function _json_replacer(value)
1392
1400
}
1393
1401
} // end _json_replacer
1394
1402
1395
- function json_stringify ( _v , _prettify )
1403
+ function json_stringify ( _v , _prettify , filter )
1396
1404
{
1397
1405
try {
1398
1406
// Check if we want to prettify the output string
@@ -1403,7 +1411,10 @@ function json_stringify( _v, _prettify )
1403
1411
// if the duplicated reference is actually recursive (the builtin
1404
1412
// 'replacer' callback of the stringify method doesn't meet our needs).
1405
1413
1406
- var _struct = _json_replacer ( _v ) ;
1414
+ var prevFunc = g_JSON_gml_infunc ;
1415
+ g_JSON_gml_infunc = filter ;
1416
+ var _struct = _json_replacer ( "" , _v ) ;
1417
+ g_JSON_gml_infunc = prevFunc ;
1407
1418
return JSON . stringify ( _struct , null , _prettify ? 2 : 0 ) ;
1408
1419
}
1409
1420
catch ( e ) {
@@ -1414,84 +1425,152 @@ function json_stringify( _v, _prettify )
1414
1425
} // end json_stringify
1415
1426
1416
1427
// This is a JSON to any helper function (to be used with JSON.parse)
1428
+ var g_JSON_gml_func = undefined ;
1429
+ var g_counterPointerNull = 0 ;
1417
1430
function _json_reviver ( _ , value )
1418
1431
{
1432
+ var ret = undefined ;
1419
1433
switch ( typeof value ) {
1420
1434
case "string" :
1421
- if ( value == "@@nan$$" ) return NaN ;
1422
- if ( value == "@@infinity$$" ) return Infinity ;
1423
- if ( value == "@@-infinity$$" ) return - Infinity ;
1424
-
1425
- // Check if we can parse an int64 value
1426
- var match = value . match ( _regexp_int64_parse ) ;
1427
- if ( match ) {
1428
- return parseInt ( match [ 1 ] , 16 ) ;
1429
- }
1430
-
1431
- // check to see if this is an @ref
1432
- if ( value . startsWith ( "@ref " ) ) {
1433
- return parseRef ( value ) ;
1434
- }
1435
-
1436
- return value ;
1435
+ if ( value == "@@nan$$" ) ret = NaN ;
1436
+ else
1437
+ if ( value == "@@infinity$$" ) ret = Infinity ;
1438
+ else
1439
+ if ( value == "@@-infinity$$" ) ret = - Infinity ;
1440
+ else {
1441
+ ret = value ;
1442
+
1443
+ // Check if we can parse an int64 value
1444
+ var match = value . match ( _regexp_int64_parse ) ;
1445
+ if ( match ) {
1446
+ ret = parseInt ( match [ 1 ] , 16 ) ;
1447
+ }
1448
+ else
1449
+ // check to see if this is an @ref
1450
+ if ( value . startsWith ( "@ref " ) ) {
1451
+ ret = parseRef ( value ) ;
1452
+ }
1453
+ } // end else
1454
+ break ;
1437
1455
case "number" :
1438
- return value ;
1456
+ ret = value ;
1457
+ break ;
1439
1458
case "boolean" :
1440
- return value ;
1459
+ ret = value ;
1460
+ break ;
1441
1461
case "object" :
1442
1462
1443
1463
// The value is null (make it pointer_null)
1444
- if ( value == null ) return g_pBuiltIn . pointer_null ;
1445
-
1464
+ if ( value == null ) {
1465
+ ret = g_pBuiltIn . pointer_null ;
1466
+ ++ g_counterPointerNull ;
1467
+ } // end if
1468
+ else
1446
1469
// It's an array just return it
1447
1470
if ( value instanceof Array ) {
1448
- return value ;
1449
- }
1450
- // It's an object prepare to set internal values
1451
- var obj = { } ;
1452
- obj . __type = "___struct___" ;
1453
- obj . __yyIsGMLObject = true ;
1454
- // Go through all the property names in parsed value
1455
- for ( var oName in value ) {
1456
- // Continue if it is not a property
1457
- if ( ! value . hasOwnProperty ( oName ) ) continue ;
1458
-
1459
- // Change the name to match the gml standards
1460
- var nName ;
1461
- if ( g_instance_names [ oName ] != undefined ) {
1462
- nName = oName ;
1463
- }
1464
- else if ( typeof g_var2obf !== "undefined" && g_var2obf [ oName ] != undefined ) {
1465
- nName = g_var2obf [ oName ] ;
1466
- }
1467
- else {
1468
- nName = "gml" + oName ;
1469
- }
1471
+ ret = value ;
1472
+ } // end if
1473
+ else {
1474
+ // It's an object prepare to set internal values
1475
+ var obj = { } ;
1476
+ obj . __type = "___struct___" ;
1477
+ obj . __yyIsGMLObject = true ;
1478
+ // Go through all the property names in parsed value
1479
+ for ( var oName in value ) {
1480
+ // Continue if it is not a property
1481
+ if ( ! value . hasOwnProperty ( oName ) ) continue ;
1470
1482
1471
- // Define a property in the new object (proper name and attributes)
1472
- Object . defineProperty ( obj , nName , {
1473
- value : value [ oName ] ,
1474
- configurable : true ,
1475
- writable : true ,
1476
- enumerable : true
1477
- } ) ;
1478
- }
1479
- return obj ;
1483
+ // Change the name to match the gml standards
1484
+ var nName ;
1485
+ if ( g_instance_names [ oName ] != undefined ) {
1486
+ nName = oName ;
1487
+ }
1488
+ else if ( typeof g_var2obf !== "undefined" && g_var2obf [ oName ] != undefined ) {
1489
+ nName = g_var2obf [ oName ] ;
1490
+ }
1491
+ else {
1492
+ nName = "gml" + oName ;
1493
+ }
1494
+
1495
+ // Define a property in the new object (proper name and attributes)
1496
+ Object . defineProperty ( obj , nName , {
1497
+ value : value [ oName ] ,
1498
+ configurable : true ,
1499
+ writable : true ,
1500
+ enumerable : true
1501
+ } ) ;
1502
+ } // end for
1503
+ ret = obj ;
1504
+ } // end else
1505
+ break ;
1480
1506
default :
1481
- return value ;
1507
+ ret = value ;
1508
+ break ;
1482
1509
}
1510
+
1511
+ // call the user method
1512
+ if ( ( g_JSON_gml_func != undefined ) && is_callable ( g_JSON_gml_func ) ) {
1513
+ var _func = getFunction ( g_JSON_gml_func , 1 ) ;
1514
+ _obj = "boundObject" in _func ? _func . boundObject : { } ;
1515
+ if ( ret == g_pBuiltIn . pointer_null ) {
1516
+ ret = undefined ;
1517
+ -- g_counterPointerNull ;
1518
+ } // end if
1519
+ ret = _func ( _obj , _obj , _ , ret ) ;
1520
+ if ( ret === undefined ) {
1521
+ ret = g_pBuiltIn . pointer_null ;
1522
+ ++ g_counterPointerNull ;
1523
+ }
1524
+ } // end if
1525
+ return ret ;
1483
1526
} // end _json_reviver
1484
1527
1485
- function json_parse ( _v )
1528
+ function json_convert_pointer_null ( _v )
1529
+ {
1530
+ var ret = _v ;
1531
+ switch ( typeof _v ) {
1532
+ case "array" :
1533
+ for ( var i in _v ) {
1534
+ _v [ i ] = json_convert_pointer_null ( _v [ i ] ) ;
1535
+ if ( _v [ i ] == g_pBuiltIn . pointer_null ) {
1536
+ _v [ i ] = undefined ;
1537
+ } // end if
1538
+ } // end for
1539
+ break ;
1540
+ case "object" :
1541
+ for ( var key in _v ) {
1542
+ if ( _v . hasOwnProperty ( key ) ) {
1543
+ _v [ key ] = json_convert_pointer_null ( _v [ key ] ) ;
1544
+ if ( _v [ key ] == g_pBuiltIn . pointer_null ) {
1545
+ _v [ key ] = undefined ;
1546
+ } // end if
1547
+ } // endif
1548
+ } // end for
1549
+ break ;
1550
+ }
1551
+ return ret ;
1552
+ }
1553
+
1554
+ function json_parse ( _v , _func )
1486
1555
{
1487
1556
var ret = undefined ;
1557
+ var oldFunc = g_JSON_gml_func ;
1558
+ var oldCounter = g_counterPointerNull ;
1559
+ g_JSON_gml_func = _func ;
1560
+ g_counterPointerNull = 0 ;
1488
1561
try {
1489
- return JSON . parse ( _v , _json_reviver ) ;
1562
+ var ret = JSON . parse ( _v , _json_reviver ) ;
1563
+ if ( g_counterPointerNull > 0 ) {
1564
+ json_convert_pointer_null ( ret ) ;
1565
+ } // end if
1566
+ return ret ;
1490
1567
}
1491
1568
catch ( e ) {
1492
1569
// do nothing
1493
1570
yyError ( "JSON parse error" ) ;
1494
1571
}
1572
+ g_counterPointerNull = oldCounter ;
1573
+ g_JSON_gml_func = oldFunc ;
1495
1574
return ret ;
1496
1575
} // end json_parse
1497
1576
0 commit comments