Skip to content

Commit 9bc0c5f

Browse files
authored
Merge pull request #21 from somiaj/api-tweaks
Change JSON to Mojo::JSON, don't force static, use to_string.
2 parents fd307ca + 396446f commit 9bc0c5f

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

lib/WeBWorK/FormatRenderedProblem.pm

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ package WeBWorK::FormatRenderedProblem;
1010
use strict;
1111
use warnings;
1212

13-
use JSON;
14-
# use Digest::SHA qw(sha1_base64);
13+
use Mojo::JSON qw(encode_json);
1514
use Mojo::Util qw(xml_escape);
1615
use Mojo::DOM;
1716
use Mojo::URL;
@@ -88,9 +87,7 @@ sub formatRenderedProblem {
8887
);
8988

9089
# Get the requested format. (outputFormat or outputformat)
91-
# override to static mode if showCorrectAnswers has been set
92-
my $formatName = $inputs_ref->{showCorrectAnswers}
93-
&& !$inputs_ref->{isInstructor} ? 'static' : ($inputs_ref->{outputFormat} || 'default');
90+
my $formatName = $inputs_ref->{outputFormat} || 'default';
9491

9592
# Add JS files requested by problems via ADD_JS_FILE() in the PG file.
9693
my @extra_js_files;
@@ -203,7 +200,7 @@ sub formatRenderedProblem {
203200
$output->{third_party_js} = \@third_party_js;
204201

205202
# Convert to JSON and render.
206-
return $c->render(data => JSON->new->utf8(1)->encode($output));
203+
return $c->render(data => encode_json($output));
207204
}
208205

209206
# Setup and render the appropriate template in the templates/RPCRenderFormats folder depending on the outputformat.

lib/WeBWorK/RenderProblem.pm

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use Time::HiRes qw/time/;
88
use Proc::ProcessTable;
99
use Date::Format;
1010

11-
use JSON::XS;
12-
use Crypt::JWT qw( encode_jwt );
11+
use Mojo::JSON qw( encode_json );
12+
use Crypt::JWT qw( encode_jwt );
1313
use Digest::MD5 qw( md5_hex );
1414

1515
use lib "$ENV{PG_ROOT}/lib";
@@ -42,7 +42,7 @@ die "You must first create an output file at $path_to_log_file with permissions
4242
sub UNIVERSAL::TO_JSON {
4343
my ($self) = shift;
4444

45-
use Storable qw(dclone);
45+
use Storable qw(dclone);
4646
use Data::Structure::Util qw(unbless);
4747

4848
my $clone = unbless(dclone($self));
@@ -97,8 +97,7 @@ sub process_pg_file {
9797
$return_object->{tags} = WeBWorK::Utils::Tags->new($inputs_ref->{sourceFilePath}, $problem->source)
9898
if ($inputs_ref->{includeTags});
9999

100-
my $coder = JSON::XS->new->ascii->pretty->allow_unknown->convert_blessed;
101-
my $json = $coder->encode($return_object);
100+
my $json = encode_json($return_object);
102101
return $json;
103102
}
104103

lib/WeBWorK/Utils.pm

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use base qw(Exporter);
44
use strict;
55
use warnings;
66

7-
use JSON;
7+
use Mojo::JSON qw(decode_json);
88

99
our @EXPORT_OK = qw(
1010
wwRound
@@ -35,7 +35,7 @@ sub readJSON {
3535
my $data = <$fh>;
3636
close $fh;
3737

38-
return JSON->new->decode($data);
38+
return decode_json($data);
3939
}
4040

4141
sub getThirdPartyAssetURL {
@@ -49,7 +49,7 @@ sub getThirdPartyAssetURL {
4949
. substr($dependencies->{$_}, 1) . '/'
5050
. ($1 =~ s/(?:\.min)?\.(js|css)$/.min.$1/gr);
5151
} else {
52-
return Mojo::URL->new("${baseURL}$file")->query(version => $dependencies->{$_} =~ s/#/@/gr);
52+
return Mojo::URL->new("${baseURL}$file")->query(version => $dependencies->{$_} =~ s/#/@/gr)->to_string;
5353
}
5454
}
5555
}
@@ -99,15 +99,10 @@ sub getAssetURL {
9999
# If so, then either serve it from a CDN if requested, or serve it directly with the library version
100100
# appended as a URL parameter.
101101
if ($file =~ /^node_modules/) {
102-
my $wwFile = getThirdPartyAssetURL(
103-
$file, $thirdPartyWWDependencies,
104-
'',
105-
0
106-
);
102+
my $wwFile = getThirdPartyAssetURL($file, $thirdPartyWWDependencies, '', 0);
107103
return $wwFile if $wwFile;
108104

109-
my $pgFile =
110-
getThirdPartyAssetURL($file, $thirdPartyPGDependencies, 'pg_files/', 1);
105+
my $pgFile = getThirdPartyAssetURL($file, $thirdPartyPGDependencies, 'pg_files/', 1);
111106
return $pgFile if $pgFile;
112107
}
113108

@@ -117,7 +112,7 @@ sub getAssetURL {
117112
($language =~ /^(he|ar)/ && $file !~ /node_modules/ && $file =~ /\.css$/)
118113
? $file =~ s/\.css$/.rtl.css/r
119114
: undef;
120-
115+
121116
# First check to see if this is a file in the webwork htdocs location with a rtl variant.
122117
return "$staticWWAssets->{$rtlfile}"
123118
if defined $rtlfile && defined $staticWWAssets->{$rtlfile};

0 commit comments

Comments
 (0)