It is currently April 18th, 2024, 11:28 am

Json Parser Plugin

Share and get help with Plugins and Addons
User avatar
e2e8
Posts: 8
Joined: January 5th, 2020, 8:18 am

Json Parser Plugin

Post by e2e8 »

I have created a simple Json Parser Plugin. I could not find one already existing and it seems like it should be broadly useful. It is essentially just a simple wrapper for Json.NET LINQtoJson and SelectToken https://www.newtonsoft.com/json/help/html/LINQtoJSON.htm

Repo here: https://github.com/e2e8/rainmeter-jsonparser

Download Build: https://github.com/e2e8/rainmeter-jsonparser/releases

Usage

The plugin requires a Source and a Query
  • Source: valid json string
Example:

Code: Select all

[Weather]
Measure=Plugin
Plugin=WebParser.dll
UpdateRate=600
Url=http://api.openweathermap.org/...
RegExp=(?siU)^(.*)$

[Current.Temp]
Measure=Plugin
Plugin=JsonParser.dll
Source=[Weather]
Query="main.temp"

XML Parser

Also I was having so much fun with this that I also created an Xml Parser Plugin just in case somebody is still using XML. https://github.com/e2e8/rainmeter-xmlparser
Last edited by e2e8 on January 11th, 2020, 5:34 am, edited 5 times in total.
User avatar
JelleDekkers
Posts: 127
Joined: September 27th, 2017, 6:32 pm
Location: Netherlands

Re: Json Parser Plugin

Post by JelleDekkers »

Nice plugin, but is there any way to include this with a skin without the user having to put the Newtonsoft.Json.dll file in Rainmeter's root folder?
User avatar
e2e8
Posts: 8
Joined: January 5th, 2020, 8:18 am

Re: Json Parser Plugin

Post by e2e8 »

Yeah I am looking into some of the ways to embed the dependency in the plugin dll. Have not gotten it to work yet.
Last edited by e2e8 on January 6th, 2020, 5:14 am, edited 1 time in total.
User avatar
e2e8
Posts: 8
Joined: January 5th, 2020, 8:18 am

Re: Json Parser Plugin

Post by e2e8 »

It is a single dll now. I have updated the build to pack the dependency dll into the main plugin dll with ilmerge. I have updated the files in the release on github.
User avatar
JelleDekkers
Posts: 127
Joined: September 27th, 2017, 6:32 pm
Location: Netherlands

Re: Json Parser Plugin

Post by JelleDekkers »

e2e8 wrote: January 6th, 2020, 1:48 am It is a single dll now. I have updated the build to pack the dependency dll into the main plugin dll with ilmerge. I have updated the files in the release on github.
Niiiice! I'm loving this plugin already, it's so much easier than using RegExp! :thumbup:
OfficerHalf
Posts: 3
Joined: January 6th, 2020, 4:52 pm

Re: Json Parser Plugin

Post by OfficerHalf »

This is so much easier than using regex, thank you. :thumbup:

I was seeing an error in the log:
Error reading JToken from JsonReader. Path '', line 0, position 0. (devops\Vertical\DevOpsVertical.ini - [MeasurePRCount])

My JSON & selector wasn't the issue; the measure seemed to be working just fine. Just for reference, here's my json/skin.

Rainmeter

Code: Select all

; PR Status
[PRStatus]
Measure=WebParser
URL=https://#CoreServer#/#Organization#/#Project#/_apis/git/repositories/#Repository#/pullrequests?api-version=5.1&searchCriteria.reviewerId=#UserId#
Header=Content-Type: application/json
Header2=Authorization: Basic #AccessToken#
RegExp=(?siU)^(.*)$

[MeasurePRCount]
Measure=Plugin
Plugin=JsonParser.dll
Source=[PRStatus]
Query="count"
IfCondition=MeasurePRCount > 0
IfTrueAction=[!SetOption MeterPRIcon FontColor #VFontColorWorking#]
IfFalseAction=[!SetOption MeterPRIcon FontColor #VFontColor#]
JSON Response

Code: Select all

{
    "value": [ ... // removed because this isn't data I can share...
    ],
    "count": 4
}
I realized the issue was that the plugin was throwing an error on the initial update before the WebParser measure had finished. I made this small change to get rid of the error:

Code: Select all

; PR Status
[PRStatus]
Measure=WebParser
URL=https://#CoreServer#/#Organization#/#Project#/_apis/git/repositories/#Repository#/pullrequests?api-version=5.1&searchCriteria.reviewerId=#UserId#
Header=Content-Type: application/json
Header2=Authorization: Basic #AccessToken#
RegExp=(?siU)^(.*)$
FinishAction=[!SetOption MeasurePRCount Disabled 0]

[MeasurePRCount]
Measure=Plugin
Plugin=JsonParser.dll
Source=[PRStatus]
Query="count"
IfCondition=MeasurePRCount > 0
IfTrueAction=[!SetOption MeterPRIcon FontColor #VFontColorWorking#]
IfFalseAction=[!SetOption MeterPRIcon FontColor #VFontColor#]
Disabled=1
It might be worth ignoring empty input.
User avatar
JelleDekkers
Posts: 127
Joined: September 27th, 2017, 6:32 pm
Location: Netherlands

Re: Json Parser Plugin

Post by JelleDekkers »

Just finished a skin using your plugin ;)
https://jelledekkers.nl/sienna/

Compared to WebParser, it's a dream to work with.
User avatar
e2e8
Posts: 8
Joined: January 5th, 2020, 8:18 am

Re: Json Parser Plugin

Post by e2e8 »

JelleDekkers wrote: January 9th, 2020, 10:56 pm Just finished a skin using your plugin ;)
https://jelledekkers.nl/sienna/

Compared to WebParser, it's a dream to work with.
Nice job. Glad you liked it. What motivated my to finally create this was that one of data sources I used in my skin serves different shaped json depending on the data and so regex kept breaking. I suppose there was some other way to deal with that (lua script?) but this seemed like the obvious solution. Really surprising that this did not exist before (probably does somewhere).
OfficerHalf wrote: January 9th, 2020, 3:20 pm I was seeing an error in the log:
Error reading JToken from JsonReader. Path '', line 0, position 0. (devops\Vertical\DevOpsVertical.ini - [MeasurePRCount])
...
I realized the issue was that the plugin was throwing an error on the initial update before the WebParser measure had finished.
Yeah. I am seeing this one also on first load. In my case I ignore the error. Not sure how to fix this for the general case because I certainly want to see the error most of the time. One of my biggest frustrations with Rainmeter is the lack of debugging tools and debug logging for all the dynamic values.


One thought for improving this plugin would be to make use of parent child measures. This could be more efficient because the json would parsed only once and potentially make the skin a tiny bit more concise. On the other hand I doubt performance is an issue and this could make it less straightforward to use.
User avatar
JelleDekkers
Posts: 127
Joined: September 27th, 2017, 6:32 pm
Location: Netherlands

Re: Json Parser Plugin

Post by JelleDekkers »

e2e8 wrote: January 11th, 2020, 5:30 am Yeah. I am seeing this one also on first load. In my case I ignore the error. Not sure how to fix this for the general case because I certainly want to see the error most of the time. One of my biggest frustrations with Rainmeter is the lack of debugging tools and debug logging for all the dynamic values.


One thought for improving this plugin would be to make use of parent child measures. This could be more efficient because the json would parsed only once and potentially make the skin a tiny bit more concise. On the other hand I doubt performance is an issue and this could make it less straightforward to use.
I just put Disabled=1 on every measure, as well as UpdateDivider=-1, which prevents any errors and only updates the measures when needed. Just take a look at the WeatherParser.inc file in my skin to see what I did.
User avatar
raiguard
Posts: 660
Joined: June 25th, 2015, 7:02 pm
Location: The Sky, USA

Re: Json Parser Plugin

Post by raiguard »

Oh man, this is great! At the moment I am using a JSON-to-table conversion function that is over a thousand lines long to get my data. This will be so much easier!

If wxdata really is gone, this plugin will become very popular, since the best alternatives both use JSON. :D
”We are pretty sure that r2922 resolves the regression in resolution caused by a reversion to a revision.” - jsmorley, 2017