From eeccedf095df8ffbc0f4fb2def6513992b41cc06 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 22 Sep 2013 13:13:50 -0400 Subject: [PATCH 1/3] json encoding test --- t/14_serializer/19_json_utf8.t | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 t/14_serializer/19_json_utf8.t 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"; + + + From 18c5517b47d41fb0da64c34d6865baa22269035c Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 22 Sep 2013 13:14:05 -0400 Subject: [PATCH 2/3] encode/decode as binary utf8 Deals with the funny stuff seen in #771. The documentation should also make clear what we do turn on the utf8 flag on by default. We should also make sure this doesn't break other stuff at a distance -- I'm quite weary of the comment in 'serialize' that we don't utf8 the thing there because it's done "later on"... --- lib/Dancer/Serializer/JSON.pm | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/Dancer/Serializer/JSON.pm b/lib/Dancer/Serializer/JSON.pm index c75b2efe9..95f063f73 100644 --- a/lib/Dancer/Serializer/JSON.pm +++ b/lib/Dancer/Serializer/JSON.pm @@ -65,7 +65,10 @@ sub deserialize { # 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(@_) || {}; + $options->{utf8} = 1 if !exists $options->{utf8}; + return $options; } # JSON should be UTF8 by default, so explicitly decode it as such From cb0dbc7b5387ba8e0a7180a0aefd3e5b2c1ec860 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Sun, 22 Sep 2013 13:26:41 -0400 Subject: [PATCH 3/3] amend documentation for the utf8 behavior --- lib/Dancer.pm | 9 +++++++++ lib/Dancer/Serializer/JSON.pm | 24 +++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) 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 95f063f73..1df74597b 100644 --- a/lib/Dancer/Serializer/JSON.pm +++ b/lib/Dancer/Serializer/JSON.pm @@ -62,21 +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 { my $self = shift; my $options = $self->_options_as_hashref(@_) || {}; - $options->{utf8} = 1 if !exists $options->{utf8}; + # 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; } @@ -124,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 @@ -142,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