Cscript jscript JSON

This is a very strange problem (very !!!).

I have this ARIPTT that runs on Windows XP and 7 using dos CSCRIPT in a file called testJSON.js .

 if ( ! this.JSON ) WScript.Echo("JSON DOESN'T EXISTS"); 

And, well, the message appears, but it is an unexpected behavior of AORIPT, because JSON (as MSDN documentation reports) is the default object in JSCRIPT 5.8 , and my system in Windows 7 runs exactly JSCRIPT 5.8 .

Now I temporarily solved this problem (in a small complex script) by creating a new text file and MANUALLY making a valid JSON string (and, obviously, it does everything fine even if the system does not have JSCRIPT 5.8 as requested by JSON), but I like know basically two things:

1st Why can't I use a JSON object, even if my version of JSCRIPT is the one that supports this object?

2nd I have something about the "inclusion" of JSON (and another) inaccessible object in my JSCRIPT environment, but all the examples are for C #, and I like to know if there is any equivalent code for AORIPT.

+9
source share
3 answers

Why can't I use a JSON object, even if my version of JSCRIPT is the one that supports this object?

According to MSDN , Windows Script Host uses the default JScript 5.7 feature for backward compatibility. The JScript 5.8 feature set is used only in Internet Explorer in IE8 + Standard document modes.

You have the following options:

I read something about the "inclusion" of JSON (and another) inaccessible object in my JSCRIPT environment, but all the examples are for C #, and I like to know if there is any equivalent code for AORIPT.

Script execution custom hosts can only be created using languages ​​with proper COM support, such as C ++, C #, etc. JScript cannot be used for this, because, for example, it does not support parameters.

+9
source

You can use eval() to achieve an effect similar to JSON.parse() .

 eval('obj = {' + JSONstring + '}'); 

And after that, obj.toString() will allow you to get data similar to JSON.stringify() (only without beautify parameters). See this answer for an example in the wild. The fact is that you can create an object from JSON text without having to load any external libraries or switch the interpreter mechanism.

PREVENTION OF THE BIG FOOD !!!

This leads to a vulnerability in the workstation where your code is running. If you are not in control of the JSON generation you want to parse, or if it is possible that a third party can change the JSON between your generation and its interpretation, consider Helen's next tip . If there are bad things in JSON, this can cause your WScript to do bad things. For example, if a line or JSON file contains the following:

 }; var oSH = WSH.CreateObject("wscript.shell"), cmd = oSH.Exec("%comspec%"); WSH.Sleep(250); cmd.StdIn.WriteLine("net user pwnd password /add"); WSH.Sleep(250); cmd.StdIn.WriteLine("net group Administrators pwnd /add"); WSH.Sleep(250); cmd.Terminate(); var obj = { "objName": { "item1": "value 1", "item2": "value 2" } 

... then parsing it with eval will simply add a new administrator to your computer without any visual indication that this has happened.

My advice: feel free to use eval for private or casual use; but for widespread deployment, consider including json2.js , as Helen suggests. Edit: Or ...

htmlfile COM object

You can import JSON methods by calling the htmlfile COM object and making it work in compatibility mode with IE9 (or higher) using the <META> as follows:

 var htmlfile = WSH.CreateObject('htmlfile'), JSON; htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />'); htmlfile.close(JSON = htmlfile.parentWindow.JSON); 

With these three lines, the JSON object and methods are copied to the JScript runtime, allowing you to parse JSON without using eval() or loading json2.js. Now you can do things like this:

 var pretty = JSON.stringify(JSON.parse(json), null, '\t'); WSH.Echo(pretty); 

Here's a breakdown:

 // load htmlfile COM object and declare empty JSON object var htmlfile = WSH.CreateObject('htmlfile'), JSON; // force htmlfile to load Chakra engine htmlfile.write('<meta http-equiv="x-ua-compatible" content="IE=9" />'); // The following statement is an overloaded compound statement, a code golfing trick. // The "JSON = htmlfile.parentWindow.JSON" statement is executed first, copying the // htmlfile COM object JSON object and methods into "JSON" declared above; then // "htmlfile.close()" ignores its argument and unloads the now unneeded COM object. htmlfile.close(JSON = htmlfile.parentWindow.JSON); 

See this answer for other methods (loading json2.js via XHR, InternetExplorer.Application COM object, hybrid HTA method and another htmlfile example).

+17
source

JSON encoding, decoding without analyzer by default: https://gist.github.com/gnh1201/e372f5de2e076dbee205a07eb4064d8d

 var $ = {}; /** * Decode JSON * * @param string jsonString - JSON text * * @return object */ $.json.decode = function(jsonString) { return (new Function("return " + jsonString)()); }; /** * Encode JSON * * @param object obj - Key/Value object * * @return string */ $.json.encode = function(obj) { var items = []; var isArray = (function(_obj) { try { return (_obj instanceof Array); } catch (e) { return false; } })(obj); var _toString = function(_obj) { try { if(typeof(_obj) == "object") { return $.json.encode(_obj); } else { var s = String(_obj).replace(/"/g, '\\"'); if(typeof(_obj) == "number" || typeof(_obj) == "boolean") { return s; } else { return '"' + s + '"'; } } } catch (e) { return "null"; } }; for(var k in obj) { var v = obj[k]; if(!isArray) { items.push('"' + k + '":' + _toString(v)); } else { items.push(_toString(v)); } } if(!isArray) { return "{" + items.join(",") + "}"; } else { return "[" + items.join(",") + "]"; } }; /** * Test JSON * * @param object obj - Key/Value object * * @return boolean */ $.json.test = function(obj) { var t1 = obj; var t2 = $.json.encode(obj); $.echo($.json.encode(t1)); var t3 = $.json.decode(t2); var t4 = $.json.encode(t3); $.echo(t4); if(t2 == t4) { $.echo("success"); return true; } else { $.echo("failed"); return false; } }; /** * Echo * * @param string txt * * @return void */ $.echo = function(txt) { if($.isWScript()) { WScript.Echo(txt); } else { try { window.alert(txt); } catch (e) { console.log(txt); } } }; /** * Check if WScript * * @return bool */ $.isWScript = function() { return typeof(WScript) !== "undefined"; } // test your data var t1 = {"a": 1, "b": "banana", "c": {"d": 2, "e": 3}, "f": [100, 200, "3 hundreds", {"g": 4}]}; $.json.test(t1); 
0
source

Source: https://habr.com/ru/post/956221/


All Articles