Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json Utf8 behavior #964

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/Dancer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,11 @@ Deserializes a JSON structure. Can receive optional arguments. Those arguments
are valid L<JSON> arguments to change the behaviour of the default
C<JSON::from_json> function.

If no explicit I<utf8>
argument is passed, it'll default to C<true>, making the function
behave like I<decode_json>.


=head2 from_yaml ($structure)

Deserializes a YAML structure.
Expand Down Expand Up @@ -1767,6 +1772,10 @@ Serializes a structure to JSON. Can receive optional arguments. Thoses arguments
are valid L<JSON> arguments to change the behaviour of the default
C<JSON::to_json> function.

If no explicit I<utf8>
argument is passed, it'll default to C<true>, making the serialization
behave like I<encode_json>.

=head2 to_yaml ($structure)

Serializes a structure to YAML.
Expand Down
27 changes: 18 additions & 9 deletions lib/Dancer/Serializer/JSON.pm
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,19 @@ sub deserialize {
JSON::from_json( $entity, $options );
}

# Standard JSON behaviour is fine when serializing; we'll end up
# encoding as UTF8 later on.
sub _serialize_options_as_hashref {
return shift->_options_as_hashref(@_);
my $self = shift;
my $options = $self->_options_as_hashref(@_) || {};
# by default, behave like encode_json
$options->{utf8} = 1 unless exists $options->{utf8};
return $options;
}

# JSON should be UTF8 by default, so explicitly decode it as such
# on its way in.
sub _deserialize_options_as_hashref {
my $self = shift;
my $options = $self->_options_as_hashref(@_) || {};
$options->{utf8} = 1 if !exists $options->{utf8};
# by default, behave like decode_json
$options->{utf8} = 1 unless exists $options->{utf8};
return $options;
}

Expand Down Expand Up @@ -121,7 +122,7 @@ This can be done in your config.yml file or directly in your app code with the
B<set> keyword. This serializer will also be used when the serializer is set
to B<mutable> and the correct Accept headers are supplied.

The L<JSON> module will pass configuration variables straight through.
The L<JSON> module will pass configuration variables straight through.
Some of these can be useful when debugging/developing your app: B<pretty> and
B<canonical>, and others useful with ORMs like L<DBIx::Class>: B<allow_blessed>
and B<convert_blessed>. Please consult the L<JSON> documentation for more
Expand All @@ -139,11 +140,19 @@ settings to the B<engines> configuration to turn these on. For example:

=head2 serialize

Serialize a data structure to a JSON structure.
Serialize a data structure to a JSON structure.

If no explicit I<utf8>
option is passed, it'll default to C<true>, making the serialization
behave like I<encode_json>.

=head2 deserialize

Deserialize a JSON structure to a data structure
Deserialize a JSON structure to a data structure.

If no explicit I<utf8>
option is passed, it'll default to C<true>, making the deserialization
behave like I<decode_json>.

=head2 content_type

Expand Down
16 changes: 16 additions & 0 deletions t/14_serializer/19_json_utf8.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use strict;
use warnings;

use Test::More tests => 1;

use utf8;
use Dancer::Serializer::JSON;

my $data = { foo => 'café' };

is Dancer::Serializer::JSON::from_json(
Dancer::Serializer::JSON::to_json( $data )
)->{foo} => $data->{foo}, "encode/decode round-trip";