################################################################ # $Id: 98_JsonList2.pm 4128 2013-10-29 06:51:24Z rudolfkoenig $ ################################################################ package main; use strict; use warnings; use POSIX; sub CommandJsonList2($$); ##################################### sub JsonList2_Initialize($$) { my %lhash = ( Fn=>"CommandJsonList2", Hlp=>"[],list definitions as JSON" ); $cmds{jsonlist2} = \%lhash; } ##################################### sub JsonList2_Escape($) { my $a = shift; return "null" if(!defined($a)); my %esc = ( "\n" => '\n', "\r" => '\r', "\t" => '\t', "\f" => '\f', "\b" => '\b', "\"" => '\"', "\\" => '\\\\', "\'" => '\\\'', ); $a =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/eg; return $a; } sub JsonList2_dumpHash($$$$$) { my ($name, $h, $isReading, $si, $next) = @_; my $ret = ""; $ret .= " \"$name\": {\n"; my @arr = grep { $si || $_ !~ m/^\./ } sort keys %{$h}; @arr = grep { !ref($h->{$_}) } @arr if(!$isReading); for(my $i2=0; $i2 < @arr; $i2++) { my $k = $arr[$i2]; $ret .= " \"".JsonList2_Escape($k)."\": "; if($isReading) { $ret .= "{ \"Value\":\"".JsonList2_Escape($h->{$k}{VAL})."\","; $ret .= " \"Time\":\"".JsonList2_Escape($h->{$k}{TIME})."\" }"; } else { $ret .= "\"".JsonList2_Escape($h->{$k})."\""; } $ret .= "," if($i2 < int(@arr)-1); $ret .= "\n"; } $ret .= " }".($next ? ",":"")."\n"; return $ret; } ##################################### sub CommandJsonList2($$) { my ($cl, $param) = @_; my @d; my $ret; my $cnt=0; my $si = AttrVal("global", "showInternalValues", 0); if($param) { @d = devspec2array($param); } else { @d = keys %defs; $param=""; } $ret = "{\n"; $ret .= " \"Arg\":\"".JsonList2_Escape($param)."\",\n", $ret .= " \"Results\": [\n"; for(my $i1 = 0; $i1 < int(@d); $i1++) { my $d = $d[$i1]; next if(IsIgnored($d)); $cnt++; my $h = $defs{$d}; my $n = $h->{NAME}; next if(!$h || !$n); $ret .= " {\n"; $ret .= " \"Name\":\"".JsonList2_Escape($n)."\",\n"; $ret .= " \"PossibleSets\":\"".JsonList2_Escape(getAllSets($n))."\",\n"; $ret .= " \"PossibleAttrs\":\"".JsonList2_Escape(getAllAttr($n))."\",\n"; $ret .= JsonList2_dumpHash("Internals", $h, 0, $si, 1); $ret .= JsonList2_dumpHash("Readings", $h->{READINGS}, 1, $si, 1); $ret .= JsonList2_dumpHash("Attributes",$attr{$d}, 0, $si, 0); $ret .= " }"; $ret .= "," if($i1 < int(@d)-1); $ret .= "\n"; } $ret .= " ],\n"; $ret .= " \"totalResultsReturned\":$cnt\n"; $ret .= "}\n"; return $ret; } 1; =pod =begin html

JsonList2

=end html =begin html_DE

JsonList2

=end html_DE =cut