diff --git a/lib/Zonemaster/Engine/DNSName.pm b/lib/Zonemaster/Engine/DNSName.pm index 3375e0bfa..45dc785e3 100644 --- a/lib/Zonemaster/Engine/DNSName.pm +++ b/lib/Zonemaster/Engine/DNSName.pm @@ -155,6 +155,12 @@ sub prepend { return $self->new( { labels => \@labels } ); } +sub TO_JSON { + my ( $self ) = @_; + + return $self->string; +} + 1; =head1 NAME @@ -234,6 +240,10 @@ See also L. Returns a new L object, representing the called one with the given label prepended. +=item TO_JSON + +Helper method for JSON encoding. + =back =cut diff --git a/lib/Zonemaster/Engine/Packet.pm b/lib/Zonemaster/Engine/Packet.pm index 3ec67f06b..8ee033ea9 100644 --- a/lib/Zonemaster/Engine/Packet.pm +++ b/lib/Zonemaster/Engine/Packet.pm @@ -157,6 +157,12 @@ sub answerfrom { return $from; } +sub TO_JSON { + my ( $self ) = @_; + + return { 'Zonemaster::Engine::Packet' => $self->packet }; +} + 1; =head1 NAME @@ -226,6 +232,10 @@ C and C, only RRs from those sections are returned. Wrapper for the underlying packet method, that replaces undefined values with the string CunknownE>. +=item TO_JSON + +Support method for L to be able to serialize these objects. + =back =head1 METHODS PASSED THROUGH diff --git a/t/logger.t b/t/logger.t index c462e86c1..61f420ca1 100644 --- a/t/logger.t +++ b/t/logger.t @@ -1,6 +1,7 @@ -use Test::More; -use Test::Fatal; use File::Slurp; +use JSON::PP; +use Test::Fatal; +use Test::More; BEGIN { use_ok( 'Zonemaster::Engine' ); @@ -36,6 +37,35 @@ ok( scalar( @{ Zonemaster::Engine->logger->entries } ) >= 2, 'expected number of like( "$entry", qr/System:Unspecified:TEST an=argument/, 'stringification overload' ); +$entry = info( 'TEST2', { an => 'argument', domain => Zonemaster::Engine::Util::name( 'logger.test.example' ) } ); + +# FIXME: Ideally, we really should instead have domain=logger.test.example +# without the double quotes around the domain name. +# +# Providing the domain name as a regular string gives us no double quotes +# around the domain name. Providing it directly as a +# Zonemaster::Engine::DNSName does give us double quotes, however. That’s +# because blessed objects get turned into their JSON representations, while +# regular strings are interpolated as they are. The TO_JSON method for DNSName +# objects returns a simple string, which is then converted into JSON in the +# usual double-quoted notation. +# +# It’s an annoying inconsistency, but any attempt at a fix might cause +# repercussions when translating a log entry to a human language, a string, or +# JSON. For now, we expect the broken behavior, but that should be fixed +# someday. +is( + "$entry", + 'System:Unspecified:TEST2 an=argument; domain="logger.test.example"', + 'stringification overload works with DNS names' ); +is_deeply( + (decode_json $log->json)->[-1]->{args}, + { + an => 'argument', + domain => 'logger.test.example' + }, + 'entry arguments serialize to JSON' ); + is( $entry->level, 'DEBUG', 'right level' ); my $example = Zonemaster::Engine::Logger::Entry->new({ module => 'Basic', tag => 'B02_NS_BROKEN', testcase => 'Basic02' } ); is( $example->level, 'ERROR', 'expected level' );