Skip to content

Instantly share code, notes, and snippets.

@akzhan
Created December 28, 2016 12:17
Show Gist options
  • Save akzhan/e18ae6f967d2da6af46e336604a0decf to your computer and use it in GitHub Desktop.
Save akzhan/e18ae6f967d2da6af46e336604a0decf to your computer and use it in GitHub Desktop.
package BUX::Util::JSON;
=encoding utf-8
=head1 NAME
BUX::Util::JSON
=head1 SYNOPSYS
use BUX::Util::JSON qw( data_to_json data_from_json );
=head1 DESCRIPTION
Получает на вход JSON в виде строки с флагом utf-8.
Или возвращает JSON в виде строки с флагом utf-8.
=cut
use strict;
use warnings;
use utf8;
use JSON::XS ();
use Encode qw( encode_utf8 decode_utf8 );
use Exporter qw( import );
my $jsoner = JSON::XS->new->utf8->allow_nonref;
=head1 METHODS
=head2 data_to_json
my $json_str = data_to_json( $scalar_or_ref );
=cut
sub data_to_json {
return decode_utf8( $jsoner->encode( $_[0] ) );
}
=head2 data_from_json
my $scalar_or_ref = data_from_json( $json_str );
=cut
sub data_from_json {
return $jsoner->decode( encode_utf8( $_[0] ) );
}
our @EXPORT_OK = qw(
&data_to_json
&data_from_json
);
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment