WinBatch and Javascript (well, mostly JSON)

Started by mhall, December 31, 2015, 08:20:51 PM

Previous topic - Next topic

mhall

Hi Tony,

I've got a question for the developers ...

With the open-sourcing of Javascript engines like Google's V8 and Microsoft's Chakra and the (apparent!) ease(?) of incorporating these into C/C++ based programs (such as Node.js), has there been any consideration in extending Winbatch's ability to take advantage and possibly be able to run Javascript code from within Winbatch? Say via an extender or such? Mostly my interest at this time is being able to read/parse/save JSON documents. A couple of use cases ...

Read a JSON document in from disk.
Execute Javascript against that data to change/add properties, etc.
Write the JSON document out to disk again or output it via Webbatch to browser.

Or ...

Make a request to an online API.
Receive JSON response.
Read properties right from returned JSON or modify properties, save to disk, etc.

I am currently doing a lot of expensive string gymnastics to create a JSON formatted string for the browser or to take returned JSON and reformat it so that I can pull data out of it and save it to disk in a format I can work with.

Thanks for any consideration!
~Micheal

stanl

You might want to search 'json' in the Tech Support archives. There are numerous examples. The biggest issues I faced with json were not so much from what Winbatch could do, but from what constituted 'well-formed' json.

td

Quote from: mhall on December 31, 2015, 08:20:51 PM
Hi Tony,

I've got a question for the developers ...

With the open-sourcing of Javascript engines like Google's V8 and Microsoft's Chakra and the (apparent!) ease(?) of incorporating these into C/C++ based programs (such as Node.js), has there been any consideration in extending Winbatch's ability to take advantage and possibly be able to run Javascript code from within Winbatch? Say via an extender or such? Mostly my interest at this time is being able to read/parse/save JSON documents. A couple of use cases ...

Read a JSON document in from disk.
Execute Javascript against that data to change/add properties, etc.
Write the JSON document out to disk again or output it via Webbatch to browser.

Or ...

Make a request to an online API.
Receive JSON response.
Read properties right from returned JSON or modify properties, save to disk, etc.

I am currently doing a lot of expensive string gymnastics to create a JSON formatted string for the browser or to take returned JSON and reformat it so that I can pull data out of it and save it to disk in a format I can work with.

Thanks for any consideration!
~Micheal

There are examples of making JSON request and executing javascript in the Tech Database.  Use the search terms 'JSON' and 'javascript' to find them.  The examples are mostly COM Automation based and there isn't much using WinBatch's CLR hosting.  However, there are several open source dotNet javascript (at least one is Google V8 based) related assemblies available that likely work with WinBatch CLR hosting and  they appear to provide a richer feature set than COM Automation. 

In short, there are no plans to create a javascript extender because it cannot be justified based on what is already available.  However, if you wish to create one, have at it.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

This may be the .NET class Tony is referring to

https://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer(v=vs.110).aspx

There are ample examples of C# code using this class and WB's CLR can execute C# code. However, if you are intending to parse json as delivered from a web query you might want to look into Powershell's ConvertFromJson module (included in PS 3.0 and above) and there is a at least one Tech Support article illustrating using that module with WB.

I helped a co-worker understand json he was receiving from score on XBOX games. It was fairly complicated but he was able to write a C# parser in a day. I was able to replicate with COM javascript but C# worked about 3 times faster.

I feel the best way to bring this thread to a close is for the OP to display specific json to be parsed.

mhall

Thanks Tony, thanks Stan - I appreciate the info.

I've done some searching in the past and played with one or two of the JSON examples there. They looked to use the eval() function quite a bit, which is pretty slow. The examples I saw seemed like they were running eval() on the JSON data each time you asked for a property value. Is there a way to eval the JSON data once, hold it as an object and refer to it again later?

QuoteHowever, if you wish to create one, have at it.
I appreciate your confidence! :) However, that's a hill I don't know that I'm able to climb at the moment. I'm quite comfortable in WinBatch and Javascript within certain limits, but aside from the similarities in syntax between JS and the C family, have no familiarity/experience with C++ ... and isn't that what I would need to use to write an extender?

Stan - I know you've worked with PowerShell quite a bit, you've mentioned it before and it sounds very capable. It's still an unknown to me, but I did some searching after reading your message and it looks interesting. How would one (or would one even need to) use WinBatch/Webbatch together with PowerShell? Can WB and PS share data back and forth?

All of this is a somewhat academic question - I was curious to see what sort of capabilities there were for executing JS within WB. If it were more readily possible, I'd be happy to play with it. I have quite a bit of JS code that works with all of this data and quite a bit of WB code that does a lot of the same things. I thought it would be great if I could switch to storing all data on disk as JSON strings and execute JS within WB code to work with it. As I look a different pain points and how things might be able to improve, reusing my JS code in WB came up as a thought. As it is, I get the idea that diving into this could be quite the task and my busy photography season is about to start, so any work on it will have to be a project for a later date.

If you're still curious, I did post a piece of JSON online at:

http://michealhallphotography.com/holding/user_account.json

It's a reasonably beefy piece - about 2MB - a full user account with albums of favorite images, order history, etc., etc. It's my testing account in our system and has more information than a usual account does. It's got @CRLFs and @TABs in it for formatting, so technically right now it wouldn't pass validation, but otherwise it does. :)

Regards,
Micheal

stanl

I grabbed the json you posted and saved it to a local file. Then issued a 1-line powershell command which serialized to the main objects


Get-Content -Raw -Path c:\reports\ps\testjs.json | ConvertFrom-Json


you can change the name of the local file to test yourself, and can further output to a tabular file. This would help you set up a custom parser (which is generally required with json).

Powershell 3.0 comes with Win7 and includes it's own GUI for script testing. And as you probably noticed from the examples in the tech DB, it can be wrapped within WB. Not sure how that might eventually work with WebBatch, but if Jim monitors this thread he can probably answer that, as well as suggest how the Newtonsoft control can help with the task.

td

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl


td

The class your link points to certainly applies to JSON.  More idle musing than proposing but I was thinking about the possibilities of  the several dotNet javescript classes that should allow a WIL script to both execute javascript and access execution results including javascript variables without ever leaving the WIL script's process.       
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

mhall

Quote from: td on January 04, 2016, 10:02:47 AM
More idle musing than proposing but I was thinking about the possibilities of  the several dotNet javescript classes that should allow a WIL script to both execute javascript and access execution results including javascript variables without ever leaving the WIL script's process.     

That's definitely along the lines of what I was hoping existed. :)

mhall

Quote from: stanl on January 04, 2016, 04:16:00 AM
I grabbed the json you posted and saved it to a local file. Then issued a 1-line powershell command which serialized to the main objects

Thanks again Stan, I appreciate it.

td

Quote from: mhall on January 10, 2016, 10:06:30 PM
That's definitely along the lines of what I was hoping existed. :)

Never used any of them but here's a link to one of several implementations:

https://javascriptdotnet.codeplex.com/
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Quote from: td on January 11, 2016, 06:31:37 AM
Never used any of them but here's a link to one of several implementations:

https://javascriptdotnet.codeplex.com/

64 downloads since 2012, and this at the end:

The Javascript .NET project was brought to you by the people at  Noesis Innovation. Unfortunately they lost interest, so now other people take care of it.


td

Quote from: stanl on January 11, 2016, 12:43:16 PM
64 downloads since 2012, and this at the end:

The Javascript .NET project was brought to you by the people at  Noesis Innovation. Unfortunately they lost interest, so now other people take care of it.


Yes but if you do read it with a little more care, you will notice that it had 64 downloads in the last 7 days, 276 downloads in the last 30 days, 1611 downloads over the last 6 months and 27181 total.  Not that any of that means much.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

Yes, I missed that ... probably more concerned with 'they lost interest'. Also, a larger issue:  WB and the CLR.   I know that has been documented as to what is and what cannot be, but something along the lines of what to do with what you discovered tweaks interest. This goes way off this thread but I would like to get a general guestimate from you about:

1. If a .NET assembly is accessible from the CLR WB can use it?
2. What does this entail with respect to WB EXE's on other user PC's (rights etc...)

NOTE: I said guestimate.

I'll have time this weekend to download, play with the assembly and hopefully post a working script. And if no one else is interested.......

JTaylor

I just realized I hadn't seen any posts from the WinBatch forum for a LONG time and discovered I just wasn't looking closely enough :)

Not that this really helps at all but....I ran across a C++ library that looked like it might work well for an Extender if anyone is interested I can dig it up and post a link.   Keep thinking I will tackle it but ran into a problem with the Extender SDK being old and the VS project is in a format that VS Express no longer recognizes.   Other projects keep hindering me overcoming that problem but happy to help someone else by pointing the way.  If things slow down maybe I will tackle this again.   Don't know much about C/C++ but guessing I can hack my way through it again like I did with the COM Control extender.   Maybe in the meantime the SDK will be updated with newer Example/Project files.

Jim