Philippe's recent post about Crypto reminded me of this.
I'd like something that would allow me to specify json in a similar way to how XPath does for XML.
I think this would allow me to remove code I have that checks for certain elements being present in a received JSON document and instead move it out to a configuration file that checks an XPath-like definition.
In my messy code I have a bunch of if/then/else statements testing the received json from various radio stations to determine which format it is in so that I can subsequently parse it correctly.
I have made the subsequent parsing code simpler than it used to be by having song & programme field names in a config file per station group.
I could put real Perl code in the config files and then eval it but I don't think that is a good way of doing things.
If I could put a XPath like statement in there as well then I could simplify things further.
Perhaps JSON::Path ... it looks to me like it has a lot of dependencies that are currently not met in standard LMS installation and I think some are not pure Perl so would mean compiling and packaging binaries for the various platforms which is something I don't really want to get into.
Results 1 to 5 of 5
-
2022-01-24, 03:14 #1
- Join Date
- Apr 2005
- Location
- UK/London
- Posts
- 5,479
More JSON handling routines in LMS builds?
Paul Webster
Author of "Now Playing" plugins covering Radio France (FIP etc), PlanetRadio (Bauer - Kiss, Absolute, Scala, JazzFM etc), KCRW, ABC Australia and CBC/Radio-Canada
and, via the extra "Radio Now Playing" plugin lots more - see https://forums.slimdevices.com/showt...Playing-plugin
-
2022-01-24, 03:28 #2
More JSON handling routines in LMS builds?
> I'd like something that would allow me to specify json in a similar way
> to how XPath does for XML.
I'm not familiar with XPath. Can you elaborate what you're trying to do?
-
2022-01-24, 04:51 #3
- Join Date
- Apr 2005
- Location
- UK/London
- Posts
- 5,479
I would like to replace lots of bits of code that are variants on
Code:if (ref($perl_data) ne "ARRAY" && exists $perl_data->{"data"} && exists $perl_data->{"data"}->{"TitleDiffusions"} && ref($perl_data->{"data"}->{"TitleDiffusions"}) eq "ARRAY" )
my @paths = [ '$root/TitleDiffusions[0]' ];
Then the if statement becomes something like ...
Code:my $matched = true; foreach my $path ( @paths ){ my $jpath = JSON::Path->new($path); $matched = false if $jpath->values($perl_data) == 0; } if ( $matched ){ // process it etc etc }
Code:if (ref($perl_data) ne "ARRAY" && exists $perl_data->{"id_playlst"} && exists $perl_data->{"update_tm"} && exists $perl_data->{"itms"} && ref($perl_data->{"itms"}) eq "ARRAY" )
Code:$paths = [ '$root/id_playlst', '$root/update_tm', '$root/itms[0]', ];
Last edited by Paul Webster; 2022-01-24 at 05:07. Reason: Edited to put in ->values (yes - I know mh won't see it but it is not supposed to be real code anyway!)
Paul Webster
Author of "Now Playing" plugins covering Radio France (FIP etc), PlanetRadio (Bauer - Kiss, Absolute, Scala, JazzFM etc), KCRW, ABC Australia and CBC/Radio-Canada
and, via the extra "Radio Now Playing" plugin lots more - see https://forums.slimdevices.com/showt...Playing-plugin
-
2022-01-24, 05:41 #4
More JSON handling routines in LMS builds?
Ah, ok, that makes a lot of sense. I've been thinking about how to
simplify this, too. But I was looking at it from a JS perspective, where
lodash is a very popular library (see eg.
https://lodash.com/docs/4.17.15#get). The problem I'm seeing with this
approach is that a user would have to have the latest LMS in order to
run your plugin.
-
2022-01-24, 07:10 #5
- Join Date
- Apr 2005
- Location
- UK/London
- Posts
- 5,479
I could make my own mini-parser that only handles the specifics that I care about rather than using an all encompassing one.
Then I could have my own syntax and possibly include an OR operator which I could make use of but would not get from the module I referred to.
So ignore this for now and I'll experiment.Paul Webster
Author of "Now Playing" plugins covering Radio France (FIP etc), PlanetRadio (Bauer - Kiss, Absolute, Scala, JazzFM etc), KCRW, ABC Australia and CBC/Radio-Canada
and, via the extra "Radio Now Playing" plugin lots more - see https://forums.slimdevices.com/showt...Playing-plugin