deserialize
This commit is contained in:
parent
cf0a37de7f
commit
73b20e91b3
@ -13,6 +13,16 @@ sub serialize ( $self, $type, $data, $extra_data = {} ) {
|
|||||||
return $self->type($type)->serialize( $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 => (
|
has types => (
|
||||||
is => 'ro',
|
is => 'ro',
|
||||||
default => sub { +{} },
|
default => sub { +{} },
|
||||||
|
@ -166,4 +166,58 @@ sub gen_item ( $item, $data, $extra_data ) {
|
|||||||
return $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;
|
1;
|
||||||
|
@ -109,10 +109,11 @@ sub serialize {
|
|||||||
|
|
||||||
Takes in the serialized C<$json_string> and recreate data out of it.
|
Takes in the serialized C<$json_string> and recreate data out of it.
|
||||||
|
|
||||||
UNIMPLENTED
|
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub deserialize { ... }
|
sub deserialize ( $self, $serialized, @ ) {
|
||||||
|
$self->registry->deserialize(
|
||||||
|
$self->json_serializer->deserialize($serialized) );
|
||||||
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
11
t/example.t
11
t/example.t
@ -2,6 +2,8 @@ use 5.32.0;
|
|||||||
|
|
||||||
use Test2::V0;
|
use Test2::V0;
|
||||||
|
|
||||||
|
use Clone qw/ clone /;
|
||||||
|
|
||||||
use Dancer2::Plugin::JsonApi::Registry;
|
use Dancer2::Plugin::JsonApi::Registry;
|
||||||
|
|
||||||
use experimental qw/ signatures /;
|
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;
|
done_testing;
|
||||||
|
Loading…
Reference in New Issue
Block a user