diff --git a/lib/Dancer.pm b/lib/Dancer.pm index 9dee6981b..dfab4c278 100644 --- a/lib/Dancer.pm +++ b/lib/Dancer.pm @@ -827,6 +827,11 @@ Deserializes a JSON structure. Can receive optional arguments. Those arguments are valid L arguments to change the behaviour of the default C function. +If no explicit I +argument is passed, it'll default to C, making the function +behave like I. + + =head2 from_yaml ($structure) Deserializes a YAML structure. @@ -1767,6 +1772,10 @@ Serializes a structure to JSON. Can receive optional arguments. Thoses arguments are valid L arguments to change the behaviour of the default C function. +If no explicit I +argument is passed, it'll default to C, making the serialization +behave like I. + =head2 to_yaml ($structure) Serializes a structure to YAML. diff --git a/lib/Dancer/Serializer/JSON.pm b/lib/Dancer/Serializer/JSON.pm index c75b2efe9..1df74597b 100644 --- a/lib/Dancer/Serializer/JSON.pm +++ b/lib/Dancer/Serializer/JSON.pm @@ -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; } @@ -121,7 +122,7 @@ This can be done in your config.yml file or directly in your app code with the B keyword. This serializer will also be used when the serializer is set to B and the correct Accept headers are supplied. -The L module will pass configuration variables straight through. +The L module will pass configuration variables straight through. Some of these can be useful when debugging/developing your app: B and B, and others useful with ORMs like L: B and B. Please consult the L documentation for more @@ -139,11 +140,19 @@ settings to the B 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 +option is passed, it'll default to C, making the serialization +behave like I. =head2 deserialize -Deserialize a JSON structure to a data structure +Deserialize a JSON structure to a data structure. + +If no explicit I +option is passed, it'll default to C, making the deserialization +behave like I. =head2 content_type diff --git a/t/14_serializer/19_json_utf8.t b/t/14_serializer/19_json_utf8.t new file mode 100644 index 000000000..fb3714ffe --- /dev/null +++ b/t/14_serializer/19_json_utf8.t @@ -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"; + + +