use v6.d.PREVIEW;
use Cro::HTTP::BodySerializers;
use XML::Writer;
class Ultramarine::Serialiser::XML
does Cro::HTTP::BodySerializer {
method is-applicable(Cro::HTTP::Message $message, $body --> Bool) {
with $message.content-type {
(.type eq 'application'|'text' && .subtype eq 'xml' || .suffix eq 'xml') &&
($body ~~ Map || $body ~~ List)
}
else {
False
}
}
method serialize(Cro::HTTP::Message $message, $body --> Supply) {
my $xml = (
qq{<?xml version="1.0" encoding="UTF-8"?>\n} ~
XML::Writer.serialize(
subsonic-response => [
:xmlns<http://subsonic.org/restapi>,
:version<1.13.0>,
|$body,
],
)
).encode('utf-8');
self!set-content-length($message, $xml.bytes);
supply { emit $xml }
}
}