Merge branch 'deserialize'
This commit is contained in:
commit
066ec3dde6
@ -13,6 +13,16 @@ sub serialize ( $self, $type, $data, $extra_data = {} ) {
|
||||
return $self->type($type)->serialize( $data, $extra_data );
|
||||
}
|
||||
|
||||
sub deserialize ( $self, $data, $included = [] ) {
|
||||
|
||||
my $type =
|
||||
ref $data->{data} eq 'ARRAY'
|
||||
? $data->{data}[0]->{type}
|
||||
: $data->{data}{type};
|
||||
|
||||
return $self->type($type)->deserialize( $data, $included );
|
||||
}
|
||||
|
||||
has types => (
|
||||
is => 'ro',
|
||||
default => sub { +{} },
|
||||
|
@ -166,4 +166,58 @@ sub gen_item ( $item, $data, $extra_data ) {
|
||||
return $item->( $data, $extra_data );
|
||||
}
|
||||
|
||||
sub deserialize ( $self, $serialized, $included = [] ) {
|
||||
|
||||
my $data = $serialized->{data};
|
||||
my @included = ( ( $serialized->{included} // [] )->@*, @$included );
|
||||
|
||||
return $self->deserialize_data( $data, \@included );
|
||||
}
|
||||
|
||||
sub expand_object ( $obj, $included ) {
|
||||
|
||||
if ( ref $obj eq 'ARRAY' ) {
|
||||
return [ map { expand_object( $_, $included ) } @$obj ];
|
||||
}
|
||||
|
||||
for (@$included) {
|
||||
return $_ if $_->{type} eq $obj->{type} and $_->{id} eq $obj->{id};
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
sub deserialize_data ( $self, $data, $included ) {
|
||||
|
||||
if ( ref $data eq 'ARRAY' ) {
|
||||
return [ map { $self->deserialize_data( $_, $included ) } @$data ];
|
||||
}
|
||||
|
||||
my %obj = (
|
||||
( $data->{attributes} // {} )->%*,
|
||||
pairmap {
|
||||
$a =>
|
||||
$self->registry->type( $self->relationships->{$a}{type} )
|
||||
->deserialize_data( $b, $included )
|
||||
} pairmap { $a => expand_object( $b, $included ) }
|
||||
pairmap { $a => $b->{data} } ( $data->{relationships} // {} )->%*
|
||||
);
|
||||
|
||||
my $id_key = $self->id;
|
||||
if ( !ref $id_key ) {
|
||||
$obj{$id_key} = $data->{id};
|
||||
}
|
||||
|
||||
if ( $data->{type} eq 'photo' ) {
|
||||
|
||||
# die keys %$data;
|
||||
}
|
||||
|
||||
if ( 1 == keys %obj and exists $obj{id} ) {
|
||||
return $data->{id};
|
||||
}
|
||||
|
||||
return \%obj;
|
||||
}
|
||||
|
||||
1;
|
||||
|
@ -109,10 +109,11 @@ sub serialize {
|
||||
|
||||
Takes in the serialized C<$json_string> and recreate data out of it.
|
||||
|
||||
UNIMPLENTED
|
||||
|
||||
=cut
|
||||
|
||||
sub deserialize { ... }
|
||||
sub deserialize ( $self, $serialized, @ ) {
|
||||
$self->registry->deserialize(
|
||||
$self->json_serializer->deserialize($serialized) );
|
||||
}
|
||||
|
||||
1;
|
||||
|
11
t/example.t
11
t/example.t
@ -2,6 +2,8 @@ use 5.32.0;
|
||||
|
||||
use Test2::V0;
|
||||
|
||||
use Clone qw/ clone /;
|
||||
|
||||
use Dancer2::Plugin::JsonApi::Registry;
|
||||
|
||||
use experimental qw/ signatures /;
|
||||
@ -212,4 +214,13 @@ subtest 'comments only have the body attribute' => sub {
|
||||
}
|
||||
};
|
||||
|
||||
subtest 'deserialize' => sub {
|
||||
my $roundtrip = $registry->deserialize($output);
|
||||
|
||||
my $expected = clone($data);
|
||||
delete $_->{created} for $expected->[0]{comments}->@*;
|
||||
|
||||
like $roundtrip => $expected;
|
||||
};
|
||||
|
||||
done_testing;
|
||||
|
Loading…
Reference in New Issue
Block a user