[Rt-commit] rt branch, 4.0/json-helper, created. rt-4.0.4-77-g1536c6d

Thomas Sibley trs at bestpractical.com
Tue Nov 29 14:09:35 EST 2011


The branch, 4.0/json-helper has been created
        at  1536c6df265bb46199645b94a789ca549bf21266 (commit)

- Log -----------------------------------------------------------------
commit 1536c6df265bb46199645b94a789ca549bf21266
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Tue Nov 29 14:04:00 2011 -0500

    Refactor and standardize encoding Perl values to JSON
    
    We tell JSON to peddle in UTF8 and allow non-references so strings can
    be properly quoted for JavaScript.
    
    The JSON() utility function is available in Mason components for ease of
    use, while the EncodeJSON() function in RT::Interface::Web is available
    for RT's libraries.

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index c880115..92fd5d5 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -70,6 +70,7 @@ use RT::Interface::Web::Session;
 use Digest::MD5 ();
 use Encode qw();
 use List::MoreUtils qw();
+use JSON qw();
 
 =head2 SquishedCSS $style
 
@@ -146,7 +147,16 @@ sub EscapeURI {
     $$ref =~ s/([^a-zA-Z0-9_.!~*'()-])/uc sprintf("%%%02X", ord($1))/eg;
 }
 
+=head2 EncodeJSON SCALAR
 
+Encodes the SCALAR to JSON and returns a JSON string.  SCALAR may be a simple
+value or a reference.
+
+=cut
+
+sub EncodeJSON {
+    to_json(shift, { utf8 => 1, allow_nonref => 1 });
+}
 
 =head2 WebCanonicalizeInfo();
 
@@ -2823,6 +2833,16 @@ sub _NewScrubber {
     return $scrubber;
 }
 
+=head2 JSON
+
+Redispatches to L<RT::Interface::Web/EncodeJSON>
+
+=cut
+
+sub JSON {
+    RT::Interface::Web::EncodeJSON(@_);
+}
+
 package RT::Interface::Web;
 RT::Base->_ImportOverlays();
 
diff --git a/share/html/Admin/Tools/Theme.html b/share/html/Admin/Tools/Theme.html
index be651cb..595ba5e 100644
--- a/share/html/Admin/Tools/Theme.html
+++ b/share/html/Admin/Tools/Theme.html
@@ -121,7 +121,7 @@ my @sections = (
 );
 </%ONCE>
 <script type="text/javascript">
-var section_css_mapping = <% JSON::to_json(\@sections) |n%>;
+var section_css_mapping = <% JSON(\@sections) |n%>;
 
 jQuery(function($) {
 
@@ -191,7 +191,6 @@ unless ($session{'CurrentUser'}->HasRight( Object=> RT->System, Right => 'SuperU
     Abort(loc('This feature is only available to system administrators.'));
 }
 
-require JSON;
 use Digest::MD5 'md5_hex';
 
 my $text_threshold = 0.6;
diff --git a/share/html/Helpers/Autocomplete/CustomFieldValues b/share/html/Helpers/Autocomplete/CustomFieldValues
index 65e5170..453a836 100644
--- a/share/html/Helpers/Autocomplete/CustomFieldValues
+++ b/share/html/Helpers/Autocomplete/CustomFieldValues
@@ -46,7 +46,7 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 % $r->content_type('application/json');
-<% JSON::to_json( \@suggestions ) |n %>
+<% JSON( \@suggestions ) |n %>
 % $m->abort;
 <%INIT>
 # Only autocomplete the last value
@@ -89,6 +89,4 @@ while( my $value = $values->Next ) {
         : $value->Name,
       };
 }
-
-require JSON;
 </%INIT>
diff --git a/share/html/Helpers/Autocomplete/Groups b/share/html/Helpers/Autocomplete/Groups
index e41119d..008c63c 100644
--- a/share/html/Helpers/Autocomplete/Groups
+++ b/share/html/Helpers/Autocomplete/Groups
@@ -46,7 +46,7 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 % $r->content_type('application/json');
-<% JSON::to_json( \@suggestions ) |n %>
+<% JSON( \@suggestions ) |n %>
 % $m->abort;
 <%ARGS>
 $term => undef
@@ -55,8 +55,6 @@ $exclude => ''
 $op => 'LIKE'
 </%ARGS>
 <%INIT>
-require JSON;
-
 $m->abort unless defined $term
              and length $term;
 
diff --git a/share/html/Helpers/Autocomplete/Owners b/share/html/Helpers/Autocomplete/Owners
index 766c520..bb69ae9 100644
--- a/share/html/Helpers/Autocomplete/Owners
+++ b/share/html/Helpers/Autocomplete/Owners
@@ -46,7 +46,7 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 % $r->content_type('application/json');
-<% JSON::to_json( \@suggestions ) |n %>
+<% JSON( \@suggestions ) |n %>
 % $m->abort;
 <%ARGS>
 $return => 'Name'
@@ -55,8 +55,6 @@ $term => undef
 $max => 10
 </%ARGS>
 <%INIT>
-require JSON;
-
 # Only allow certain return fields
 $return = 'Name'
     unless $return =~ /^(?:EmailAddress|Name|RealName|id)$/;
diff --git a/share/html/Helpers/Autocomplete/Users b/share/html/Helpers/Autocomplete/Users
index cdf40eb..8ae1ae6 100644
--- a/share/html/Helpers/Autocomplete/Users
+++ b/share/html/Helpers/Autocomplete/Users
@@ -46,7 +46,7 @@
 %#
 %# END BPS TAGGED BLOCK }}}
 % $r->content_type('application/json');
-<% JSON::to_json( \@suggestions ) |n %>
+<% JSON( \@suggestions ) |n %>
 % $m->abort;
 <%ARGS>
 $return => ''
@@ -58,8 +58,6 @@ $exclude => ''
 $op => undef
 </%ARGS>
 <%INIT>
-require JSON;
-
 # Only allow certain return fields
 $return = 'EmailAddress'
     unless $return =~ /^(?:EmailAddress|Name|RealName)$/;

-----------------------------------------------------------------------


More information about the Rt-commit mailing list