Hoppy::Formatter::JSON - IO formatter that can translate from or to JSON.


Hoppy documentation Contained in the Hoppy distribution.

Index


Code Index:

NAME

Top

Hoppy::Formatter::JSON - IO formatter that can translate from or to JSON.

SYNOPSIS

Top

  use Hoppy::Formatter::JSON;

  my $formatter = Hoppy::Formatter::JSON->new;

  # from perl data to JSON
  my $data = { method => "login", params => {user_id => "hoge"} };
  my $json = $formatter->serialize($data);

  # from JSON to perl data 
  $data = $formatter->deserialize($json);

DESCRIPTION

Top

IO formatter that can translate from or to JSON.

METHODS

Top

serialize

deserialize

AUTHOR

Top

Takeshi Miki <miki@cpan.org>

LICENSE

Top

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top


Hoppy documentation Contained in the Hoppy distribution.

package Hoppy::Formatter::JSON;
use strict;
use warnings;
use base qw( Hoppy::Base );
use Encode;
use JSON;

sub serialize {
    my ( $self, $data, $code ) = @_;
    my $json = JSON::to_json($data);
    if ( Encode::is_utf8($json) ) {
        utf8::decode($json);
    }
    return $json;
}

sub deserialize {
    my ( $self, $json, $code ) = @_;
    $json = decode( "utf8", $json );
    my $data = JSON::from_json($json);
    return $data;
}

1;
__END__