[Jifty-commit] r427 - in jifty/trunk: . lib/Jifty lib/Jifty/JSON
lib/Jifty/Web/Form t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Thu Dec 29 16:23:58 EST 2005
Author: alexmv
Date: Thu Dec 29 16:23:57 2005
New Revision: 427
Removed:
jifty/trunk/lib/Jifty/JSON/
Modified:
jifty/trunk/ (props changed)
jifty/trunk/META.yml
jifty/trunk/lib/Jifty/JSON.pm
jifty/trunk/lib/Jifty/Web/Form/Element.pm
jifty/trunk/lib/Jifty/Web/PageRegion.pm
jifty/trunk/t/99-pod-coverage.t
Log:
r8511 at zoq-fot-pik: chmrr | 2005-12-29 16:23:24 -0500
* The patch to JSON.pm didn't get accepted; and possibly rightly so,
as, looking at the JSON spec, it technically only allows double
quotes. Instead, conditionally replace the JSON::Converter's string
output to use single quotes.
* We now no longer need to skip some files for POD testing
* Remove forked JSON.pm
Modified: jifty/trunk/META.yml
==============================================================================
--- jifty/trunk/META.yml (original)
+++ jifty/trunk/META.yml Thu Dec 29 16:23:57 2005
@@ -1,5 +1,5 @@
name: Jifty
-version: 0.51225_01
+version: 0.51228
license: unknown
distribution_type: module
tests: t/*.t t/*/t/*.t
Modified: jifty/trunk/lib/Jifty/JSON.pm
==============================================================================
--- jifty/trunk/lib/Jifty/JSON.pm (original)
+++ jifty/trunk/lib/Jifty/JSON.pm Thu Dec 29 16:23:57 2005
@@ -1,227 +1,78 @@
-package Jifty::JSON;
-
+use warnings;
use strict;
-use base qw(Exporter);
-use Jifty::JSON::Parser;
-use Jifty::JSON::Converter;
-
- at Jifty::JSON::EXPORT = qw(objToJson jsonToObj);
-
-use vars qw($AUTOCONVERT $VERSION $UnMapping $BareKey $QuotApos
- $ExecCoderef $SkipInvalid $Pretty $Indent $Delimiter
- $KeySort $ConvBlessed);
-
-$VERSION = '1.02';
-
-$AUTOCONVERT = 1;
-$SkipInvalid = 0;
-$ExecCoderef = 0;
-$Pretty = 0; # pretty-print mode switch
-$Indent = 2; # (for pretty-print)
-$Delimiter = 2; # (for pretty-print) 0 => ':', 1 => ': ', 2 => ' : '
-$UnMapping = 0; #
-$BareKey = 0; #
-$QuotApos = 0; #
-$KeySort = undef; # Code-ref to provide sort ordering in converter
-
-my $parser; # JSON => Perl
-my $conv; # Perl => JSON
-
-##############################################################################
-# CONSTRCUTOR - JSON objects delegate all processes
-# to Jifty::JSON::Converter and Jifty::JSON::Parser.
-##############################################################################
-
-sub new {
- my $class = shift;
- my %opt = @_;
- bless {
- conv => undef, # Jifty::JSON::Converter [perl => json]
- parser => undef, # Jifty::JSON::Parser [json => perl]
- # below fields are for Jifty::JSON::Converter
- autoconv => $AUTOCONVERT,
- skipinvalid => $SkipInvalid,
- execcoderef => $ExecCoderef,
- pretty => $Pretty ,
- indent => $Indent ,
- delimiter => $Delimiter ,
- keysort => $KeySort ,
- convblessed => $ConvBlessed,
- # below fields are for Jifty::JSON::Parser
- unmapping => $UnMapping,
- quotapos => $QuotApos ,
- barekey => $BareKey ,
- # overwrite
- %opt,
- }, $class;
-}
+package Jifty::JSON;
-##############################################################################
-# METHODS
-##############################################################################
-
-sub jsonToObj {
- my $self = shift;
- my $js = shift;
+=head1 NAME
- if(!ref($self)){ # class method
- my $opt = __PACKAGE__->_getParamsForParser($_[0]);
- $js = $self;
- $parser ||= new Jifty::JSON::Parser;
- $parser->jsonToObj($js, $opt);
- }
- else{ # instance method
- my $opt = $self->_getParamsForParser($_[0]);
- $self->{parser} ||= ($parser ||= Jifty::JSON::Parser->new);
- $self->{parser}->jsonToObj($js, $opt);
- }
-}
+Jifty::JSON -- Wrapper around L<JSON>
+=head1 DESCRIPTION
-sub objToJson {
- my $self = shift || return;
- my $obj = shift;
+Provides a wrapper around the L<JSON> library. The JSON specification
+at L<http://www.crockford.com/JSON/> states that only double-quotes
+are possible for specifying strings. However, for the purposes of
+embedding Javascript-compatible objects in XHTML attributes (which use
+double-quotes), we sometimes want to provide strings in single quotes.
+This provides a version of L<JSON/objToJson> which allows
+single-quoted string output.
- if(ref($self) !~ /Jifty::JSON/){ # class method
- my $opt = __PACKAGE__->_getParamsForConverter($obj);
- $obj = $self;
- $conv ||= Jifty::JSON::Converter->new();
- $conv->objToJson($obj, $opt);
- }
- else{ # instance method
- my $opt = $self->_getParamsForConverter($_[0]);
- $self->{conv}
- ||= Jifty::JSON::Converter->new( %$opt );
- $self->{conv}->objToJson($obj, $opt);
- }
-}
+=head1 METHODS
+=cut
-#######################
-
+use JSON ();
-sub _getParamsForParser {
- my ($self, $opt) = @_;
- my $params;
-
- if(ref($self)){ # instance
- my @names = qw(unmapping quotapos barekey);
- my ($unmapping, $quotapos, $barekey) = @{$self}{ @names };
- $params = {
- unmapping => $unmapping, quotapos => $quotapos, barekey => $barekey,
- };
- }
- else{ # class
- $params = {
- unmapping => $UnMapping, barekey => $BareKey, quotapos => $QuotApos,
- };
- }
-
- if($opt and ref($opt) eq 'HASH'){
- for my $key ( keys %$opt ){
- $params->{$key} = $opt->{$key};
- }
- }
+=head2 jsonToObj JSON, [ARGUMENTS]
- return $params;
-}
+For completeness, C<Jifty::JSON> provides a C<jsonToObj>. It is
+identical to L<JSON/jsonToObj>.
+=cut
-sub _getParamsForConverter {
- my ($self, $opt) = @_;
- my $params;
-
- if(ref($self)){ # instance
- my @names = qw(pretty indent delimiter autoconv keysort convblessed quotapos);
- my ($pretty, $indent, $delimiter, $autoconv, $keysort, $convblessed, $quotapos)
- = @{$self}{ @names };
- $params = {
- pretty => $pretty, indent => $indent,
- delimiter => $delimiter, autoconv => $autoconv,
- keysort => $keysort, convblessed => $convblessed,
- quotapos => $quotapos,
- };
- }
- else{ # class
- $params = {
- pretty => $Pretty, indent => $Indent, delimiter => $Delimiter,
- keysort => $KeySort, convbless => $ConvBlessed,
- quotapos => $QuotApos,
- };
- }
-
- if($opt and ref($opt) eq 'HASH'){
- for my $key ( keys %$opt ){
- $params->{$key} = $opt->{$key};
- }
- }
-
- return $params;
+sub jsonToObj {
+ return JSON::jsonToObj(@_);
}
-##############################################################################
-# ACCESSOR
-##############################################################################
-
-sub autoconv { $_[0]->{autoconv} = $_[1] if(defined $_[1]); $_[0]->{autoconv} }
-
-sub pretty { $_[0]->{pretty} = $_[1] if(defined $_[1]); $_[0]->{pretty} }
-
-sub indent { $_[0]->{indent} = $_[1] if(defined $_[1]); $_[0]->{indent} }
+=head2 objToJson OBJECT, [ARGUMENTS]
-sub delimiter { $_[0]->{delimiter} = $_[1] if(defined $_[1]); $_[0]->{delimiter} }
+This method is identical to L<JSON/objToJson>, except it has an
+additional possible option. The C<singlequote> option, if set to a
+true value in the C<ARGUMENTS> hashref, overrides L<JSON::Converter>'s
+string output method to output single quotes as delimters instead of
+double quotes.
-sub unmapping { $_[0]->{unmapping} = $_[1] if(defined $_[1]); $_[0]->{unmapping} }
-
-sub keysort { $_[0]->{keysort} = $_[1] if(defined $_[1]); $_[0]->{keysort} }
-
-sub convblessed { $_[0]->{convblessed} = $_[1] if(defined $_[1]); $_[0]->{convblessed} }
-
-##############################################################################
-# NON STRING DATA
-##############################################################################
-
-# See Jifty::JSON::Parser for Jifty::JSON::NotString.
-
-sub Number {
- my $num = shift;
-
- return undef if(!defined $num);
-
- if($num =~ /^-?(?:0|[1-9][\d]*)(?:\.[\d]*)?$/
- or $num =~ /^0[xX](?:[0-9a-zA-Z])+$/)
- {
- return bless {value => $num}, 'Jifty::JSON::NotString';
- }
- else{
- return undef;
- }
-}
-
-sub True {
- bless {value => 'true'}, 'Jifty::JSON::NotString';
-}
+=cut
-sub False {
- bless {value => 'false'}, 'Jifty::JSON::NotString';
-}
+# We should escape double-quotes somehow, so that we can guarantee
+# that double-quotes *never* appear in the JSON string that is
+# returned.
+sub objToJson {
+ my ($obj, $args) = @_;
-sub Null {
- bless {value => undef}, 'Jifty::JSON::NotString';
+ # Unless we're asking for single-quoting, just do what JSON.pm
+ # does
+ return JSON::Converter::objToJson($obj)
+ unless delete $args->{singlequote};
+
+ # Otherwise, insert our own stringify sub
+ no warnings 'redefine';
+ my %esc = (
+ "\n" => '\n',
+ "\r" => '\r',
+ "\t" => '\t',
+ "\f" => '\f',
+ "\b" => '\b',
+ "'" => '\\\'',
+ "\\" => '\\\\',
+ );
+ local *JSON::Converter::_stringfy = sub {
+ my $arg = shift;
+ $arg =~ s/([\\\n'\r\t\f\b])/$esc{$1}/eg;
+ $arg =~ s/([\x00-\x07\x0b\x0e-\x1f])/'\\u00' . unpack('H2',$1)/eg;
+ return "'" . $arg ."'";
+ };
+ return JSON::Converter::objToJson($obj, $args);
}
-##############################################################################
1;
-__END__
-
-=pod
-
-=head1 NAME
-
-Jifty::JSON - parse and convert to JSON (JavaScript Object Notation).
-This is a temporary fork of the L<JSON>.pm code to allow for outputting
-single quotes. I expect that the single-quote patch will go into
-JSON.pm core in not too long, eliminating the need for this fork.
-
-=cut
Modified: jifty/trunk/lib/Jifty/Web/Form/Element.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/Form/Element.pm (original)
+++ jifty/trunk/lib/Jifty/Web/Form/Element.pm Thu Dec 29 16:23:57 2005
@@ -123,7 +123,7 @@
push @fragments, \%args;
}
- $response .= qq| $trigger="update( @{[ Jifty::JSON::objToJson( {actions => \@actions, fragments => \@fragments }, {quotapos => 1}) ]} );|;
+ $response .= qq| $trigger="update( @{[ Jifty::JSON::objToJson( {actions => \@actions, fragments => \@fragments }, {singlequote => 1}) ]} );|;
$response .= qq|return false;"|;
}
return $response;
Modified: jifty/trunk/lib/Jifty/Web/PageRegion.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Web/PageRegion.pm (original)
+++ jifty/trunk/lib/Jifty/Web/PageRegion.pm Thu Dec 29 16:23:57 2005
@@ -232,7 +232,7 @@
if ($self->region_wrapper) {
$result .= qq|<script type="text/javascript">\n|;
$result .= qq|new Region('|. $self->qualified_name .qq|',|;
- $result .= Jifty::JSON::objToJson(\%arguments, {quotapos => 1});
+ $result .= Jifty::JSON::objToJson(\%arguments, {singlequote => 1});
$result .= qq|,'|. $self->path . qq|');\n|;
$result .= qq|</script>|;
$result .= qq|<div id="region-| . $self->qualified_name . qq|">|;
Modified: jifty/trunk/t/99-pod-coverage.t
==============================================================================
--- jifty/trunk/t/99-pod-coverage.t (original)
+++ jifty/trunk/t/99-pod-coverage.t Thu Dec 29 16:23:57 2005
@@ -1,15 +1,7 @@
use Test::More;
eval "use Test::Pod::Coverage 1.00";
-die $@ if $@;
plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@;
-
-my %skips = map { $_ => 1 } qw( Jifty::JSON Jifty::JSON::Converter Jifty::JSON::Parser);
-my @files = grep { not exists $skips{$_} } Test::Pod::Coverage::all_modules();
-
-plan tests => scalar @files;
-
-Test::Pod::Coverage::pod_coverage_ok( $_, { nonwhitespace => 1 } )
- for @files;
+all_pod_coverage_ok( {nonwhitespace => 1 } );
# Workaround for dumb bug (fixed in 5.8.7) where Test::Builder thinks that
# certain "die"s that happen inside evals are not actually inside evals,
@@ -17,3 +9,4 @@
#
# (I mean, if we've gotten to this line, then clearly the test didn't die, no?)
Test::Builder->new->{Test_Died} = 0;
+
More information about the Jifty-commit
mailing list