| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 50 |  |  | 50 |  | 621 | use v5.14; | 
|  | 50 |  |  |  |  | 176 |  | 
| 2 | 50 |  |  | 50 |  | 243 | use warnings; | 
|  | 50 |  |  |  |  | 100 |  | 
|  | 50 |  |  |  |  | 1671 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 |  |  |  |  |  |  | =head1 NAME | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | Attean::API::Serializer - Serializer role | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | =head1 VERSION | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | This document describes Attean::API::Serializer version 0.032 | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | The Attean::API::Serializer role defines a common API for all serializers | 
| 15 |  |  |  |  |  |  | of typed objects to data (either a byte string or printed to a filehandle). | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =head1 REQUIRED METHODS | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | The following methods are required by the L<Attean::API::Serializer> role: | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =over 4 | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | =item C<< canonical_media_type >> | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | Returns the canonical media type string for the format of this serializer. | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =item C<< media_types >> | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | Returns an ARRAY reference of media type strings that also identify the format | 
| 30 |  |  |  |  |  |  | produced by this serializer. | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =item C<< handled_type >> | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | Returns a L<Type::Tiny> object representing the type of items that are consumed | 
| 35 |  |  |  |  |  |  | during serialization. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =item C<< file_extensions >> | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | Returns an ARRAY reference of file extensions commonly associated with the | 
| 40 |  |  |  |  |  |  | media types supported by the serializer (and returned by C<< media_types >>). | 
| 41 |  |  |  |  |  |  | File extensions should NOT include a leading dot. | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | =item C<< serialize_iter_to_io( $fh, $iterator ) >> | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | Serializes the elements from the L<Attean::API::Iterator> C<< $iterator >> to | 
| 46 |  |  |  |  |  |  | the L<IO::Handle> object C<< $fh >>. | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =item C<< serialize_iter_to_bytes( $fh ) >> | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | Serializes the elements from the L<Attean::API::Iterator> C<< $iterator >> | 
| 51 |  |  |  |  |  |  | and returns the serialization as a UTF-8 encoded byte string. | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | =back | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =head1 METHODS | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | This role provides default implementations of the following methods: | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | =over 4 | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | =item C<< serialize_list_to_io( $fh, @elements ) >> | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | Serializes the objects in C<< @elements >> to the L<IO::Handle> object | 
| 64 |  |  |  |  |  |  | C<< $fh >>. | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | =item C<< serialize_list_to_bytes( @elements ) >> | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | Serializes the objects in C<< @elements >> and returns the serialization as a | 
| 69 |  |  |  |  |  |  | UTF-8 encoded byte string. | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =back | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | =cut | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 50 |  |  | 50 |  | 239 | use Type::Tiny; | 
|  | 50 |  |  |  |  | 90 |  | 
|  | 50 |  |  |  |  | 1610 |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | use Moo::Role; | 
| 78 | 50 |  |  | 50 |  | 237 | use Carp qw(confess); | 
|  | 50 |  |  |  |  | 92 |  | 
|  | 50 |  |  |  |  | 291 |  | 
| 79 | 50 |  |  | 50 |  | 15937 |  | 
|  | 50 |  |  |  |  | 123 |  | 
|  | 50 |  |  |  |  | 15361 |  | 
| 80 |  |  |  |  |  |  | requires 'canonical_media_type'; # => (is => 'ro', isa => 'Str', init_arg => undef); | 
| 81 |  |  |  |  |  |  | requires 'media_types'; # => (is => 'ro', isa => 'ArrayRef[Str]', init_arg => undef); | 
| 82 |  |  |  |  |  |  | requires 'handled_type'; # => (is => 'ro', isa => 'Type::Tiny', init_arg => undef); | 
| 83 |  |  |  |  |  |  | requires 'file_extensions'; # => (is => 'ro', isa => 'ArrayRef[Str]', init_arg => undef); | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | requires 'serialize_iter_to_io';		# serialize_iter_to_io($io, $iter) | 
| 86 |  |  |  |  |  |  | requires 'serialize_iter_to_bytes';		# $data = serialize_iter_to_bytes($iter) | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | before 'serialize_iter_to_io' => sub { | 
| 89 |  |  |  |  |  |  | my $self	= shift; | 
| 90 |  |  |  |  |  |  | my $io		= shift || confess "No filehandle passed to serialize_iter_to_io"; | 
| 91 |  |  |  |  |  |  | my $iter	= shift || confess "No iterator passed to serialize_iter_to_io"; | 
| 92 |  |  |  |  |  |  | }; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | before 'serialize_iter_to_bytes' => sub { | 
| 95 |  |  |  |  |  |  | my $self	= shift; | 
| 96 |  |  |  |  |  |  | my $iter	= shift || confess "No iterator passed to serialize_iter_to_bytes"; | 
| 97 |  |  |  |  |  |  | }; | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | my $self	= shift; | 
| 101 |  |  |  |  |  |  | my $io		= shift; | 
| 102 | 4 |  |  | 4 | 1 | 403 | my $iter	= Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role ); | 
| 103 | 4 |  |  |  |  | 8 | return $self->serialize_iter_to_io($io, $iter); | 
| 104 | 4 |  |  |  |  | 20 | } | 
| 105 | 4 |  |  |  |  | 408 |  | 
| 106 |  |  |  |  |  |  | my $self	= shift; | 
| 107 |  |  |  |  |  |  | my $iter	= Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role ); | 
| 108 |  |  |  |  |  |  | return $self->serialize_iter_to_bytes($iter); | 
| 109 | 8 |  |  | 8 | 1 | 2252 | } | 
| 110 | 8 |  |  |  |  | 42 |  | 
| 111 | 8 |  |  |  |  | 879 | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | # Serializer that can make use of a base IRI and/or prefix IRI mappings | 
| 114 | 0 |  |  | 0 | 1 | 0 | use Types::Standard qw(InstanceOf ConsumerOf Maybe Bool); | 
| 115 |  |  |  |  |  |  | use Types::Namespace qw( NamespaceMap ); | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | use Moo::Role; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 50 |  |  | 50 |  | 353 | with 'Attean::API::Serializer'; | 
|  | 50 |  |  |  |  | 109 |  | 
|  | 50 |  |  |  |  | 404 |  | 
| 120 | 50 |  |  | 50 |  | 59949 |  | 
|  | 50 |  |  |  |  | 6717811 |  | 
|  | 50 |  |  |  |  | 395 |  | 
| 121 |  |  |  |  |  |  | has base		=> (is => 'ro', isa => ConsumerOf['Attean::API::IRI'], predicate => 'has_base'); | 
| 122 | 50 |  |  | 50 |  | 17564 | has namespaces	=> (is => 'ro', isa => Maybe[NamespaceMap], predicate => 'has_namespaces'); | 
|  | 50 |  |  |  |  | 106 |  | 
|  | 50 |  |  |  |  | 333 |  | 
| 123 |  |  |  |  |  |  | has omit_base => (is => 'ro', isa => Bool, default => 0); | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | # Serializer for a format that allows multiple serialization calls to be appended and remain syntactically valid | 
| 127 |  |  |  |  |  |  | use Moo::Role; | 
| 128 |  |  |  |  |  |  | with 'Attean::API::Serializer'; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | use Moo::Role; | 
| 132 |  |  |  |  |  |  | with 'Attean::API::Serializer'; | 
| 133 | 50 |  |  | 50 |  | 23295 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Term'); | 
|  | 50 |  |  |  |  | 120 |  | 
|  | 50 |  |  |  |  | 218 |  | 
| 134 |  |  |  |  |  |  | return $ITEM_TYPE; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 50 |  |  | 50 |  | 15625 | use Moo::Role; | 
|  | 50 |  |  |  |  | 124 |  | 
|  | 50 |  |  |  |  | 186 |  | 
| 139 |  |  |  |  |  |  | with 'Attean::API::Serializer'; | 
| 140 |  |  |  |  |  |  | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Triple'); | 
| 141 | 0 |  |  | 0 | 1 | 0 | return $ITEM_TYPE; | 
| 142 | 0 |  |  |  |  | 0 | } | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | use Moo::Role; | 
| 146 |  |  |  |  |  |  | with 'Attean::API::Serializer'; | 
| 147 | 50 |  |  | 50 |  | 18035 |  | 
|  | 50 |  |  |  |  | 134 |  | 
|  | 50 |  |  |  |  | 202 |  | 
| 148 |  |  |  |  |  |  | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Quad'); | 
| 149 |  |  |  |  |  |  | return $ITEM_TYPE; | 
| 150 | 8 |  |  | 8 | 1 | 380 | } | 
| 151 | 8 |  |  |  |  | 414 | } | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | use Moo::Role; | 
| 154 |  |  |  |  |  |  | with 'Attean::API::Serializer'; | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 50 |  |  | 50 |  | 18846 | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::TripleOrQuad'); | 
|  | 50 |  |  |  |  | 123 |  | 
|  | 50 |  |  |  |  | 226 |  | 
| 157 |  |  |  |  |  |  | return $ITEM_TYPE; | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  | } | 
| 160 | 0 |  |  | 0 | 1 | 0 |  | 
| 161 | 0 |  |  |  |  | 0 | use Moo::Role; | 
| 162 |  |  |  |  |  |  | with 'Attean::API::Serializer'; | 
| 163 |  |  |  |  |  |  | state $ITEM_TYPE = Type::Tiny::Role->new(role => 'Attean::API::Result'); | 
| 164 |  |  |  |  |  |  | return $ITEM_TYPE; | 
| 165 |  |  |  |  |  |  | } | 
| 166 | 50 |  |  | 50 |  | 18831 |  | 
|  | 50 |  |  |  |  | 118 |  | 
|  | 50 |  |  |  |  | 205 |  | 
| 167 |  |  |  |  |  |  | around 'serialize_list_to_io' => sub { | 
| 168 |  |  |  |  |  |  | my $orig	= shift; | 
| 169 |  |  |  |  |  |  | my $self	= shift; | 
| 170 | 2 |  |  | 2 | 1 | 7 | my $io		= shift; | 
| 171 | 2 |  |  |  |  | 122 | my @vars; | 
| 172 |  |  |  |  |  |  | if (scalar(@_)) { | 
| 173 |  |  |  |  |  |  | @vars	= $_[0]->variables; | 
| 174 |  |  |  |  |  |  | } | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 50 |  |  | 50 |  | 18061 | my $iter	= Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role, variables => \@vars ); | 
|  | 50 |  |  |  |  | 129 |  | 
|  | 50 |  |  |  |  | 203 |  | 
| 177 |  |  |  |  |  |  | return $self->serialize_iter_to_io($io, $iter); | 
| 178 |  |  |  |  |  |  | }; | 
| 179 | 0 |  |  | 0 | 1 |  |  | 
| 180 | 0 |  |  |  |  |  | around 'serialize_list_to_bytes' => sub { | 
| 181 |  |  |  |  |  |  | my $orig	= shift; | 
| 182 |  |  |  |  |  |  | my $self	= shift; | 
| 183 |  |  |  |  |  |  | my @vars; | 
| 184 |  |  |  |  |  |  | if (scalar(@_)) { | 
| 185 |  |  |  |  |  |  | @vars	= $_[0]->variables; | 
| 186 |  |  |  |  |  |  | } | 
| 187 |  |  |  |  |  |  | my $iter	= Attean::ListIterator->new( values => [@_], item_type => $self->handled_type->role, variables => \@vars ); | 
| 188 |  |  |  |  |  |  | return $self->serialize_iter_to_bytes($iter); | 
| 189 |  |  |  |  |  |  | }; | 
| 190 |  |  |  |  |  |  | } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | 1; | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =head1 BUGS | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | Please report any bugs or feature requests to through the GitHub web interface | 
| 198 |  |  |  |  |  |  | at L<https://github.com/kasei/attean/issues>. | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | =head1 AUTHOR | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | Gregory Todd Williams  C<< <gwilliams@cpan.org> >> | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | Copyright (c) 2014--2022 Gregory Todd Williams. | 
| 211 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it under | 
| 212 |  |  |  |  |  |  | the same terms as Perl itself. | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | =cut |