Browse files

JSON: Decode from UTF8 on the way in only.

  • Loading branch information...
1 parent 646fd28 commit d4eb3dac44015815352ec5bd61bc4d14be2b6d76 @skington skington committed Feb 9, 2012
Showing with 19 additions and 6 deletions.
  1. +19 −6 lib/Dancer/Serializer/JSON.pm
View
25 lib/Dancer/Serializer/JSON.pm
@@ -36,7 +36,7 @@ sub serialize {
my $self = shift;
my $entity = shift;
- my $options = $self->_options_as_hashref(@_) || {};
+ my $options = $self->_serialize_options_as_hashref(@_) || {};
# Why doesn't $self->config have this?
my $config = setting('engines') || {};
@@ -56,19 +56,32 @@ sub deserialize {
my $self = shift;
my $entity = shift;
- my $options = $self->_options_as_hashref(@_);
+ my $options = $self->_deserialize_options_as_hashref(@_);
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(@_);
+}
+
+# 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};
+ return $options;
+}
+
sub _options_as_hashref {
my $self = shift;
- return { utf8 => 1 } if scalar @_ == 0;
+ return if scalar @_ == 0;
if ( scalar @_ == 1 ) {
- my $options = shift;
- $options->{utf8} = 1 unless exists $options->{utf8};
- return $options;
+ return shift;
}
elsif ( scalar @_ % 2 ) {
carp "options for to_json/from_json must be key value pairs (as a hashref)";

0 comments on commit d4eb3da

Please sign in to comment.