Skip to content

Commit 9961fb6

Browse files
committed
* new arguments to `json_parse` and `json_stringify` to allow user to change values as they come in. * new functions are added as optional parameters and have 2 arguments the `key` and the `value`, they return the value and can modify if it they wish.
1 parent 66f06fe commit 9961fb6

File tree

1 file changed

+139
-60
lines changed

1 file changed

+139
-60
lines changed

scripts/functions/Function_File.js

Lines changed: 139 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,8 +1296,16 @@ function json_encode(_map, _prettify) {
12961296
// ########### JSON_STRINGIFY & JSON_PARSE ###########
12971297

12981298
// 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)
13001301
{
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+
13011309
if (value == undefined) return null;
13021310

13031311
switch (typeof value) {
@@ -1333,8 +1341,8 @@ function _json_replacer(value)
13331341
g_ENCODE_VISITED_LIST.set(value, 1);
13341342

13351343
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));
13381346
});
13391347

13401348
// Remove the flag
@@ -1371,7 +1379,7 @@ function _json_replacer(value)
13711379

13721380
if ((entry == undefined) || (entry[0] | entry[1])) {
13731381
Object.defineProperty( ret, name, {
1374-
value : _json_replacer(value[oName]),
1382+
value : _json_replacer(name, value[oName]),
13751383
configurable : true,
13761384
writable : true,
13771385
enumerable : true
@@ -1392,7 +1400,7 @@ function _json_replacer(value)
13921400
}
13931401
} // end _json_replacer
13941402

1395-
function json_stringify( _v, _prettify )
1403+
function json_stringify( _v, _prettify, filter )
13961404
{
13971405
try {
13981406
// Check if we want to prettify the output string
@@ -1403,7 +1411,10 @@ function json_stringify( _v, _prettify )
14031411
// if the duplicated reference is actually recursive (the builtin
14041412
// 'replacer' callback of the stringify method doesn't meet our needs).
14051413

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;
14071418
return JSON.stringify(_struct, null, _prettify ? 2 : 0);
14081419
}
14091420
catch( e ) {
@@ -1414,84 +1425,152 @@ function json_stringify( _v, _prettify )
14141425
} // end json_stringify
14151426

14161427
// 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;
14171430
function _json_reviver(_, value)
14181431
{
1432+
var ret = undefined;
14191433
switch (typeof value) {
14201434
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;
14371455
case "number":
1438-
return value;
1456+
ret =value;
1457+
break;
14391458
case "boolean":
1440-
return value;
1459+
ret =value;
1460+
break;
14411461
case "object":
14421462

14431463
// 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
14461469
// It's an array just return it
14471470
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;
14701482

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;
14801506
default:
1481-
return value;
1507+
ret = value;
1508+
break;
14821509
}
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;
14831526
} // end _json_reviver
14841527

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 )
14861555
{
14871556
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;
14881561
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;
14901567
}
14911568
catch( e ) {
14921569
// do nothing
14931570
yyError( "JSON parse error" );
14941571
}
1572+
g_counterPointerNull = oldCounter;
1573+
g_JSON_gml_func = oldFunc;
14951574
return ret;
14961575
} // end json_parse
14971576

0 commit comments

Comments
 (0)