From 52eba22e475827b5b8f2fccec15ef540b77dc7ee Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 10 Nov 2023 15:30:25 -0500 Subject: [PATCH] add the serializer --- lib/Dancer2/Plugin/JsonApi/Registry.pm | 15 ++++++------ lib/Dancer2/Serializer/JsonApi.pm | 33 ++++++++++++++++++++++++++ t/serializer.t | 22 +++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 lib/Dancer2/Serializer/JsonApi.pm create mode 100644 t/serializer.t diff --git a/lib/Dancer2/Plugin/JsonApi/Registry.pm b/lib/Dancer2/Plugin/JsonApi/Registry.pm index 92d8cb3..9b9e9ac 100644 --- a/lib/Dancer2/Plugin/JsonApi/Registry.pm +++ b/lib/Dancer2/Plugin/JsonApi/Registry.pm @@ -21,12 +21,12 @@ Returns the serialized form of C<$data>. =cut -sub serialize($self,$type,$data,$extra_data={}) { - return $self->type($type)->serialize($data,$extra_data); +sub serialize ( $self, $type, $data, $extra_data = {} ) { + return $self->type($type)->serialize( $data, $extra_data ); } has types => ( - is => 'ro', + is => 'ro', default => sub { +{} }, ); @@ -36,10 +36,10 @@ Adds a data type to the registry. =cut -sub add_type($self,$type,$definition={}) { +sub add_type ( $self, $type, $definition = {} ) { $self->{types}{$type} = Dancer2::Plugin::JsonApi::Registry::Schema->new( registry => $self, - type => $type, + type => $type, %$definition ); } @@ -51,8 +51,9 @@ error if the type does not exist. =cut -sub type($self,$type) { - return $self->types->{$type} || croak "type '$type' not found"; +sub type ( $self, $type ) { + return $self->types->{$type} //= + Dancer2::Plugin::JsonApi::Registry::Schema->new( type => $type ); } 1; diff --git a/lib/Dancer2/Serializer/JsonApi.pm b/lib/Dancer2/Serializer/JsonApi.pm new file mode 100644 index 0000000..cbe4c6d --- /dev/null +++ b/lib/Dancer2/Serializer/JsonApi.pm @@ -0,0 +1,33 @@ +use 5.38.0; + +package Dancer2::Serializer::JsonApi; + +use Dancer2::Plugin::JsonApi::Registry; +use Dancer2::Serializer::JSON; + +use Moo; + +has content_type => ( is => 'ro', default => 'application/vnd.api+json' ); + +with 'Dancer2::Core::Role::Serializer'; + +has registry => ( + is => 'rw', + default => sub { Dancer2::Plugin::JsonApi::Registry->new } +); + +has json_serializer => ( + is => 'ro', + default => sub { Dancer2::Serializer::JSON->new } +); + +sub serialize { + my ( $self, $data ) = @_; + + return $self->json_serializer->serialize( + $self->registry->serialize(%$data) ); +} + +sub deserialize { ... } + +1; diff --git a/t/serializer.t b/t/serializer.t new file mode 100644 index 0000000..46311c7 --- /dev/null +++ b/t/serializer.t @@ -0,0 +1,22 @@ +use Test2::V0; + +use JSON qw/ from_json /; +use Dancer2::Serializer::JsonApi; + +my $serializer = + Dancer2::Serializer::JsonApi->new( log_cb => sub { warn @_ } ); + +my $data = { 'thing' =>, { id => 2 } }; + +my $serialized = $serializer->serialize($data); + +like from_json($serialized), + { jsonapi => { version => '1.0' }, + data => { id => 2, type => 'thing' }, + }; + +todo 'not implemented yet' => sub { + is $serializer->deserialize($serialized) => $data; +}; + +done_testing;