mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-01-31 06:39:11 +00:00
fhem.pl: add toJSON (by betateilchen for fheminfo, slightly modified)
git-svn-id: https://svn.fhem.de/fhem/trunk@14348 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
e976200de3
commit
5c59529605
30
fhem/fhem.pl
30
fhem/fhem.pl
@ -134,6 +134,7 @@ sub redirectStdinStdErr();
|
||||
sub rejectDuplicate($$$);
|
||||
sub setGlobalAttrBeforeFork($);
|
||||
sub setReadingsVal($$$$);
|
||||
sub toJSON($);
|
||||
sub utf8ToLatin1($);
|
||||
|
||||
sub CommandAttr($$);
|
||||
@ -4603,6 +4604,34 @@ sub escapeLogLine($) {
|
||||
return $s;
|
||||
}
|
||||
|
||||
sub
|
||||
toJSON($)
|
||||
{
|
||||
my $val = shift;
|
||||
|
||||
if(not defined $val) {
|
||||
return "null";
|
||||
|
||||
} elsif (not ref $val) {
|
||||
$val =~ s/([\x00-\x1f\x22\x5c\x7f])/sprintf '\u%04x', ord($1)/ge;
|
||||
|
||||
return '"' . $val . '"';
|
||||
|
||||
} elsif (ref $val eq 'ARRAY') {
|
||||
return '[' . join(',', map toJSON($_), @$val) . ']';
|
||||
|
||||
} elsif (ref $val eq 'HASH') {
|
||||
return '{' . join(',',
|
||||
map { toJSON($_).":".toJSON($val->{$_}) }
|
||||
sort keys %$val) . '}';
|
||||
|
||||
} else {
|
||||
return toJSON("toJSON: Cannot encode $val");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sub
|
||||
Debug($) {
|
||||
my $msg= shift;
|
||||
@ -5056,4 +5085,5 @@ getPawList($)
|
||||
return @dob;
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
|
Loading…
Reference in New Issue
Block a user