summary refs log tree commit diff
path: root/lib/MooseX/Net/API/Role/Serialize.pm
blob: f527928082a6fd2f5e61d46e6e1b24cb07afc88c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package MooseX::Net::API::Role::Serialize;

use Moose::Role;
use JSON::XS;
use YAML::Syck;
use XML::Simple;
use Try::Tiny;

sub _to_json {
    return encode_json( $_[1] );
}

sub _to_yaml {
    return Dump $_[1];
}

sub _to_xml {
    my $xml = XML::Simple->new( ForceArray => 0 );
    $xml->XMLin("$_[0]");
}

sub _do_serialization {
    my ( $caller, $content, $format ) = @_;

    my $format_content;
    my $method = '_to_' . $format;
    return if ( !$caller->meta->find_method_by_name($method) );
    try {
        $format_content = $caller->$method($content);
    };
    return $format_content if $format_content;
}

1;