rename serialize and top_level_serialize

This commit is contained in:
Yanick Champoux 2023-11-01 11:03:11 -04:00
parent a5e552b06e
commit 4d4c4ee838
3 changed files with 13 additions and 13 deletions

View File

@ -22,7 +22,7 @@ Returns the serialized form of C<$data>.
=cut
sub serialize($self,$type,$data,$extra_data={}) {
return $self->type($type)->top_level_serialize($data,$extra_data);
return $self->type($type)->serialize($data,$extra_data);
}
has types => (

View File

@ -45,13 +45,13 @@ JSON:API object.
=cut
sub top_level_serialize ( $self, $data, $extra_data = {} ) {
sub serialize ( $self, $data, $extra_data = {} ) {
my $serial = {};
$serial->{jsonapi} = { version => '1.0' };
$serial->{data} = $self->serialize($data,$extra_data);
$serial->{data} = $self->serialize_data($data,$extra_data);
$serial->{links} = gen_links($self->top_level_links,$data,$extra_data)
if $self->top_level_links;
@ -62,15 +62,15 @@ sub top_level_serialize ( $self, $data, $extra_data = {} ) {
}
=head2 serialize($data,$extra_data)
=head2 serialize_data($data,$extra_data)
Serializes C<$data> as a JSON:API object.
Serializes the inner C<$data>.
=cut
sub serialize ( $self, $data, $extra_data = {} ) {
sub serialize_data ( $self, $data, $extra_data = {} ) {
return [ map { $self->serialize($_,$extra_data) } @$data ] if ref $data eq 'ARRAY';
return [ map { $self->serialize_data($_,$extra_data) } @$data ] if ref $data eq 'ARRAY';
my $s = {
type => $self->type,

View File

@ -7,7 +7,7 @@ use experimental qw/ signatures /;
my $type =
Dancer2::Plugin::JsonApi::Registry::Schema->new( 'type' => 'thing' );
like $type->top_level_serialize( { attr1 => 'a', id => '123' },
like $type->serialize( { attr1 => 'a', id => '123' },
{ foo => 1 } ) => {
jsonapi => { version => '1.0' },
data => { type => 'thing', id => '123' } };
@ -15,7 +15,7 @@ like $type->top_level_serialize( { attr1 => 'a', id => '123' },
is( Dancer2::Plugin::JsonApi::Registry::Schema->new(
'type' => 'thing',
id => 'foo'
)->serialize( { foo => '123' } )->{id} => '123',
)->serialize( { foo => '123' } )->{data}{id} => '123',
'custom id'
);
@ -26,11 +26,11 @@ my $serialized = schema_serialize(
},
{ x => '1', y => '2' } );
is( $serialized->{id} => '12',
is( $serialized->{data}{id} => '12',
'custom id, function'
);
like $serialized, { links => { self => '/some/url' } }, "links";
like $serialized->{data}, { links => { self => '/some/url' } }, "links";
sub schema_serialize ( $schema, $data ) {
return Dancer2::Plugin::JsonApi::Registry::Schema->new(%$schema)
@ -46,7 +46,7 @@ like(
$xtra->{bar};
}
}
)->top_level_serialize( {}, { bar => 'yup' } ),
)->serialize( {}, { bar => 'yup' } ),
{ meta => { foo => 1, bar => 'yup' } } );
@ -56,7 +56,7 @@ subtest 'attributes' => sub {
type => 'thing',
)->serialize( { id => 1, foo => 'bar'});
is $serialized => {
is $serialized->{data} => {
type => 'thing',
id => 1,
attributes => {