diff --git a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm index 5b2e4e2dc..7c1ddf215 100644 --- a/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm +++ b/lemonldap-ng-common/lib/Lemonldap/NG/Common/Conf/Serializer.pm @@ -3,6 +3,7 @@ package Lemonldap::NG::Common::Conf::Serializer; use strict; use utf8; use Data::Dumper; +use JSON::MaybeXS; use Lemonldap::NG::Common::Conf::Constants; our $VERSION = '1.9.0'; @@ -65,26 +66,15 @@ sub serialize { my ( $self, $conf ) = @_; my $fields; - # Data::Dumper options - local $Data::Dumper::Indent = 0; - local $Data::Dumper::Varname = "data"; - # Parse configuration while ( my ( $k, $v ) = each(%$conf) ) { # 1.Hash ref if ( ref($v) ) { - $fields->{$k} = $self->normalize( Dumper($v) ); + $fields->{$k} = JSON->encode($v); } - - # 2. Numeric values - elsif ( $v =~ /^\d+$/ ) { - $fields->{$k} = "$v"; - } - - # 3. Standard values else { - $fields->{$k} = $self->normalize($v); + $fields->{$k} = $v; } } @@ -99,6 +89,31 @@ sub unserialize { my ( $self, $fields ) = @_; my $conf; + # Parse fields + while ( my ( $k, $v ) = each(%$fields) ) { + if ( $k =~ $hashParameters ) { + unless ( utf8::is_utf8($v) ) { + $v = encode( 'UTF-8', $v ); + } + $conf->{$k} = ( $v =~ /./ ? eval { decode_json($v) } : {} ); + if ($@) { + $Lemonldap::NG::Common::Conf::msg .= + "Unable to decode $k, probably old format. Trying... ($@)"; + return oldUnserialize($fields); + } + } + else { + $conf->{$k} = $v; + } + } +} + +sub oldUnserialize { + use Carp; + cluck('coucou'); + my ( $self, $fields ) = @_; + my $conf; + # Parse fields while ( my ( $k, $v ) = each(%$fields) ) { diff --git a/lemonldap-ng-common/t/05-Common-Conf-LDAP.t b/lemonldap-ng-common/t/05-Common-Conf-LDAP.t index 2ea2cb8fa..92c87ce07 100644 --- a/lemonldap-ng-common/t/05-Common-Conf-LDAP.t +++ b/lemonldap-ng-common/t/05-Common-Conf-LDAP.t @@ -28,12 +28,7 @@ SKIP: { } ), "New object" - . ( - $Lemonldap::NG::Common::Conf::msg - ? " ($Lemonldap::NG::Common::Conf::msg)" - : '' - ) - ); + ) or print STDERR "Error reported: " . $Lemonldap::NG::Common::Conf::msg; ok( ref($h) and $h->can('ldap') ); }