From f3d712ea5acb9494ec06194e37a56ed302654eb8 Mon Sep 17 00:00:00 2001 From: Marko Oldenburg Date: Sat, 15 Jun 2019 08:58:14 +0200 Subject: [PATCH] try to use JSON::MaybeXS wrapper for chance of better performance + open code --- 73_GardenaSmartBridge.pm | 73 +++++++++++++++++++++++++++++++++++++- 74_GardenaSmartDevice.pm | 75 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 145 insertions(+), 3 deletions(-) diff --git a/73_GardenaSmartBridge.pm b/73_GardenaSmartBridge.pm index 0a3a590..8488a0e 100644 --- a/73_GardenaSmartBridge.pm +++ b/73_GardenaSmartBridge.pm @@ -112,9 +112,80 @@ use HttpUtils; eval "use Encode qw(encode encode_utf8 decode_utf8);1" or $missingModul .= "Encode "; -eval "use JSON;1" or $missingModul .= 'JSON '; +# eval "use JSON;1" or $missingModul .= 'JSON '; eval "use IO::Socket::SSL;1" or $missingModul .= 'IO::Socket::SSL '; +# try to use JSON::MaybeXS wrapper +# for chance of better performance + open code +eval { + require JSON::MaybeXS; + import JSON::MaybeXS qw( decode_json encode_json ); + 1; +}; + +if ($@) { + $@ = undef; + + # try to use JSON wrapper + # for chance of better performance + eval { + + # JSON preference order + local $ENV{PERL_JSON_BACKEND} = + 'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP' + unless ( defined( $ENV{PERL_JSON_BACKEND} ) ); + + require JSON; + import JSON qw( decode_json encode_json ); + 1; + }; + + if ($@) { + $@ = undef; + + # In rare cases, Cpanel::JSON::XS may + # be installed but JSON|JSON::MaybeXS not ... + eval { + require Cpanel::JSON::XS; + import Cpanel::JSON::XS qw(decode_json encode_json); + 1; + }; + + if ($@) { + $@ = undef; + + # In rare cases, JSON::XS may + # be installed but JSON not ... + eval { + require JSON::XS; + import JSON::XS qw(decode_json encode_json); + 1; + }; + + if ($@) { + $@ = undef; + + # Fallback to built-in JSON which SHOULD + # be available since 5.014 ... + eval { + require JSON::PP; + import JSON::PP qw(decode_json encode_json); + 1; + }; + + if ($@) { + $@ = undef; + + # Fallback to JSON::backportPP in really rare cases + require JSON::backportPP; + import JSON::backportPP qw(decode_json encode_json); + 1; + } + } + } + } +} + BEGIN { GP_Import( qw(readingsSingleUpdate diff --git a/74_GardenaSmartDevice.pm b/74_GardenaSmartDevice.pm index 74421cc..5f82842 100644 --- a/74_GardenaSmartDevice.pm +++ b/74_GardenaSmartDevice.pm @@ -59,7 +59,7 @@ use strict; use warnings; use FHEM::Meta; -my $version = "1.6.3"; +my $version = "1.6.4"; sub GardenaSmartDevice_Initialize($) { @@ -103,7 +103,78 @@ use FHEM::Meta; use Time::Local; -eval "use JSON;1" or $missingModul .= "JSON "; +# eval "use JSON;1" or $missingModul .= "JSON "; + +# try to use JSON::MaybeXS wrapper +# for chance of better performance + open code +eval { + require JSON::MaybeXS; + import JSON::MaybeXS qw( decode_json encode_json ); + 1; +}; + +if ($@) { + $@ = undef; + + # try to use JSON wrapper + # for chance of better performance + eval { + + # JSON preference order + local $ENV{PERL_JSON_BACKEND} = + 'Cpanel::JSON::XS,JSON::XS,JSON::PP,JSON::backportPP' + unless ( defined( $ENV{PERL_JSON_BACKEND} ) ); + + require JSON; + import JSON qw( decode_json encode_json ); + 1; + }; + + if ($@) { + $@ = undef; + + # In rare cases, Cpanel::JSON::XS may + # be installed but JSON|JSON::MaybeXS not ... + eval { + require Cpanel::JSON::XS; + import Cpanel::JSON::XS qw(decode_json encode_json); + 1; + }; + + if ($@) { + $@ = undef; + + # In rare cases, JSON::XS may + # be installed but JSON not ... + eval { + require JSON::XS; + import JSON::XS qw(decode_json encode_json); + 1; + }; + + if ($@) { + $@ = undef; + + # Fallback to built-in JSON which SHOULD + # be available since 5.014 ... + eval { + require JSON::PP; + import JSON::PP qw(decode_json encode_json); + 1; + }; + + if ($@) { + $@ = undef; + + # Fallback to JSON::backportPP in really rare cases + require JSON::backportPP; + import JSON::backportPP qw(decode_json encode_json); + 1; + } + } + } + } +} ## Import der FHEM Funktionen BEGIN {