Dancer::Serializer::YAML - serializer for handling YAML data


Dancer documentation Contained in the Dancer distribution.

Index


Code Index:

NAME

Top

Dancer::Serializer::YAML - serializer for handling YAML data

SYNOPSIS

Top

DESCRIPTION

Top

METHODS

Top

serialize

Serialize a data structure to a YAML structure.

deserialize

Deserialize a YAML structure to a data structure

content_type

Return 'text/x-yaml'


Dancer documentation Contained in the Dancer distribution.

package Dancer::Serializer::YAML;

use strict;
use warnings;
use Carp;
use Dancer::ModuleLoader;
use base 'Dancer::Serializer::Abstract';

# helpers

sub from_yaml {
    my ($yaml) = @_;
    my $s = Dancer::Serializer::YAML->new;
    $s->deserialize($yaml);
}

sub to_yaml {
    my ($data) = @_;
    my $s = Dancer::Serializer::YAML->new;
    $s->serialize($data);
}

# class definition

sub loaded { Dancer::ModuleLoader->load('YAML') }

sub init {
    my ($self) = @_;
    croak 'YAML is needed and is not installed'
      unless $self->loaded;
}

sub serialize {
    my ($self, $entity) = @_;
    YAML::Dump($entity);
}

sub deserialize {
    my ($self, $content) = @_;
    YAML::Load($content);
}

sub content_type {'text/x-yaml'}

1;
__END__