| 1 | package Cikl::Codecs::JSON; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Cikl::Models::Event; | ||
| 6 | require JSON::XS; | ||
| 7 | use Mouse; | ||
| 8 | use Cikl::Codecs::CodecRole; | ||
| 9 | use namespace::autoclean; | ||
| 10 | |||
| 11 | our $JSON = JSON::XS->new()->utf8(1); | ||
| 12 | |||
| 13 | with 'Cikl::Codecs::CodecRole'; | ||
| 14 | |||
| 15 | sub content_type { | ||
| 16 | return "application/json"; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub encode_event { | ||
| 20 | my $self = shift; | ||
| 21 | my $event = shift; | ||
| 22 | return $JSON->encode($event->to_hash()); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub decode_event { | ||
| 26 | my $self = shift; | ||
| 27 | my $json = shift; | ||
| 28 | my $data = $JSON->decode($json); | ||
| 29 | return Cikl::Models::Event->from_hash($data); | ||
| 30 | } | ||
| 31 | |||
| 32 | __PACKAGE__->meta->make_immutable; | ||
| 33 | |||
| 34 | 1; |